目录
思考:在Car类中没有定义构造方法的时候,我们也可以这么使用,为什么?
Java?SE文章参考:Java SE入门及基础知识合集-CSDN博客
//[] 中内容可有可无访问修饰符 类名 ([ 参数列表 ]){}
public class Car {????????public String brand ; // 品牌????????public String type ; // 型号????????public double price ; // 价格????????public Car (){????????????????this . brand = " 默认品牌 " ;????????????????this . type = " 默认型号 " ;????????????????this . price = 5000 ;????????}????????public void start (){????????????????System . out . println ( " 汽车启动 " );????????}????????public void speedUp (){????????????????System . out . println ( " 汽车加速 " );????????}????????public void stop (){????????????????System . out . println ( " 汽车刹车 " );????????}????????public void show (){ // 展示????????????????//因此局部变量的作用范围更小,就在局部变量所定义的方法内,????????????????//因此局部变量在方法内的优先级要高于成员变量????????????????String brand = " 奔驰 " ;????????????????System . out . println ( this . brand + "\t" + type + "\t" + price );????????}}
public class Car {????????public String brand ; // 品牌????????public String type ; // 型号????????public double price ; // 价格????????public Car (){????????}????????public void start (){????????System . out . println ( " 汽车启动 " );????????}????????public void speedUp (){????????????????System . out . println ( " 汽车加速 " );????????}????????public void stop (){????????????????System . out . println ( " 汽车刹车 " );????????}????????????????public void show (){ // 展示????????????????//因此局部变量的作用范围更小,就在局部变量所定义的方法内,????????????????//因此局部变量在方法内的优先级要高于成员变量????????????????String brand = " 奔驰 " ;????????????????System . out . println ( this . brand + "\t" + type + "\t" + price );????????}}
You don't have to provide any constructors for your class, but you must becareful when doing this. The compiler automatically provides a no-argument,default constructor for any class without constructors.你不必为类提供任何构造方法,但是在执行此操作时必须要小心。 编译器会自动为任何没有构造方法的类提供无参数的默认构造方法。