构造方法
特点
- 没有返回类型,也不能定义void;
- 名称同本类相同;
- 主要作用:完成对象的初始化工作,能把对应对象的参数传给对象成员。
class B{
public B(){ //无参构造方法
}
public B(int a,int b){ //有参构造方法
}
}
public class Cellphone {
/*public Cellphone() {
System.out.println("智能手机的默认语言为英文");
}
public Cellphone(String defaultLanguage) {
System.out.println("将智能手机的默认语言设置为" + defaultLanguage);
}
public static void main(String[] args) {
Cellphone cellphone1 = new Cellphone();
Cellphone cellphone2 = new Cellphone("中文");
}*/
public static void main(String[] args) {
String lan = "中文";
Cellphone cp = new Cellphone();
Cellphone cp2 = new Cellphone(lan);
}
public Cellphone(String lan){
System.out.println("智能手机的语言可以设置为:"+lan);
}
public Cellphone(){
System.out.println("智能手机的默认语言为英文");
}
}
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/272798.html