day06-对象内存图-面向对象的练习和内存图解(手机)

This commit is contained in:
2026-05-10 11:48:33 +08:00
parent e7a934d315
commit e026c0bc5d
2 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
package com.inmind.object01;
/*
注意:一定是先有类,才能有对象
需求:
1.定义一个手机类,属性有品牌,颜色,价格,大小 ,行为:打电话,发短信
2.创建手机对象,设置属性值,和调用手机方法
*/
public class Demo03 {
public static void main(String[] args) {
//买了个手机
Phone p1 = new Phone();
//对象的属性操作
p1.color="黑色";
p1.brand="华为";
p1.price=5999;
p1.size=6.5;
/*System.out.println(p1.brand);
System.out.println(p1.color);
System.out.println(p1.price);
System.out.println(p1.size);*/
p1.show();
//对象的行为操作
p1.call("10086");
p1.sendMsg("1388888888");
System.out.println("-----------------------------------");
//再买了个手机
Phone p2 = new Phone();
//对象的属性操作
p2.color="白色";
p2.brand="苹果";
p2.price=8999;
p2.size=6.1;
/*System.out.println(p2.brand);
System.out.println(p2.color);
System.out.println(p2.price);
System.out.println(p2.size);*/
p2.show();
p2.call("10000");
p2.sendMsg("13666666666");
}
}

View File

@@ -0,0 +1,29 @@
package com.inmind.object01;
public class Phone {
//属性
String brand;
String color;
double price;
double size;
//行为
//展示自己的功能
public void show(){
System.out.println("品牌是"+brand+",颜色是"+color+",价格是"+price+"的手机");
}
//打电话
public void call(String phoneNumber) {
System.out.println(""+phoneNumber+"打电话");
}
//发短信
public void sendMsg(String phoneNumber) {
System.out.println(""+phoneNumber+"发短信");
}
}