?
package com.itheima.constructor;
public class Test {
public static void main(String[] args) {
Car c = new Car();
System.out.println(c.name);
System.out.println(c.price);
Car c2 = new Car("宝马",80);
System.out.println(c.name);
System.out.println(c.price);
}
}
?
package com.itheima.constructor;
public class Car {
String name;
double price;
//无参数构造器
public Car(){
System.out.println("无参数构造器被触发执行~~~");
}
//有参数构造器
public Car(String n,double p){
System.out.println("有参数构造器被触发执行~~~");
name = n;
price = p;
}
}
?
?
?
|