//类
public class Car1 {
//属性
String brand;
String color;
int length;
int width;
double emissions;
int price;
//构造方法
public Car1(){//无参构造
}
public Car1(String brand, String color, int length, int width,
double emissions, int price) {
super();
this.brand = brand;
this.color = color;
this.length = length;
this.width = width;
this.emissions = emissions;
this.price = price;
}
// public Car1(String brand,String color,int length,int width,double emissions,int price){
// this.brand = brand;
// this.color = color;
// this.length = length;
// this.width = width;
// this.emissions = emissions;
// this.price = price;
// }//有参构造ss
//方法
public void start(){
System.out.println(this.brand+"出发");
}
public void stop(){
System.out.println("停");
}
public String toString(){
return this.brand+","+this.color+","+this.emissions+","+this.length+","+this.price+","+this.width;
}
}
public class TestCar1 {
public static void main(String[] args) {
//创建对象
Car1 mycar1 = new Car1();
//new Car后会在内存中分配Car类型所需属性的存储空间
//无参构造内存中的值为默认值
//变量名mycar1放到了栈中,就是Car类型的对象
//mycar1中的属性在堆中
//mycar1存放的是堆中的地址
System.out.println(mycar1);
Car1 mycar2 = new Car1("xx","白色",4555,1788,2.0f,600000);
mycar2.start();
System.out.println(mycar2);
}
}
总结:
1.类
2.构造方法 有参 无参
3.成员方法 以“.”的形式在主函数中调用