构造方法的重载

/**

* 测试构造方法的重载

*/

public class User {

int id;

String name;

String pwd;

public User(int id){

this.id = id;

//用this.标明对象的” id ” 和形参的” id “区分

}

public User(){}

//通过形参列表的不同来构成构造方法的重载

public User(int id,String name){

this.id = id;

this.name = name;

}

/*不能再定义User(int id,String pwd) 形参的名字不指代类的属性

pwd和name类型相同名字不同,在构造方法执行时无法区分输入的String型是name属性还是pwd属性

*/

public User(int id, String name, String pwd) {

this.id = id;

this.name = name;

this.pwd = pwd;

}

public static void main(String[] args) {

User u1 = new User();

User u2 = new User(001);

User u3 = new User(002,”n1″);

User u4 = new User(003,”n2″,”111111″);

}

}

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
上一篇 2022年6月13日 15:27
下一篇 2022年6月13日 15:27

相关推荐

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息