设计类和对象、定义类注意事项
![image-20211101212632119](https://img-blog.csdnimg.cn/img_convert/e283dd9d1fec9a3e8ae4fc646957b0dd.png)
![image-20211101212912845](https://img-blog.csdnimg.cn/img_convert/4ded690685d8703132047013f5d2b1ed.png)
package com.xu.createobject;
public class Car {
String name;
double price;
public void start(){
System.out.println(name+"价格是:"+price+"启动");
}
public void run(){
System.out.println(name+"价格是:"+price+"跑的快");
}
}
package com.xu.createobject;
public class Test {
public static void main(String[] args) {
Car c = new Car();
c.name = "奔驰GLC";
c.price=39.78;
System.out.println(c.name);
System.out.println(c.price);
c.start();
c.run();
Car c2 = new Car();
c2.name ="小鹏汽车";
c2.price = 40.98;
System.out.println(c2.name);
System.out.println(c2.price);
c2.start();
c2.run();
}
}
![image-20211101214905658](https://img-blog.csdnimg.cn/img_convert/4c97e8fbc1bffa5c91d2eb9721ee2815.png)
定义类补充
![image-20211101215640427](https://img-blog.csdnimg.cn/img_convert/4f6bafca24c6795c7cf2173858b4de7d.png)
成员变量初始默认值
![image-20211101215612646](https://img-blog.csdnimg.cn/img_convert/ef7ea8b52dbdca247bfe3bd5a85a7c40.png)
总结
![image-20211101215841815](https://img-blog.csdnimg.cn/img_convert/2cbe318b0f778aef8750a33ed619e719.png)