class ConstructorTry {
public static void main(String[] args) {
ConstructorTry02 cons01 = new ConstructorTry02("lord");
System.out.println(cons01.name);
System.out.println(cons01.age);
System.out.println("**********");
ConstructorTry02 cons02 = new ConstructorTry02(18);
System.out.println(cons02.name);
System.out.println(cons02.age);
System.out.println("**********");
ConstructorTry02 cons03 = new ConstructorTry02("lord", 18);
System.out.println(cons03.name);
System.out.println(cons03.age);
System.out.println("**********");
ConstructorTry02 cons04 = new ConstructorTry02();
System.out.println(cons04.name);
System.out.println(cons04.age);
System.out.println("**********");
/*
lord
-1
**********
anoymous
18
**********
lord
18
**********
anoymous
-1
**********
*/
}
}
class ConstructorTry02 {
String name;
int age;
ConstructorTry02(String name, int age){
this.name = name;
this.age = age;
}
ConstructorTry02(int age){
this("anoymous", age);
}
ConstructorTry02(String name){
this(name, -1);
}
ConstructorTry02(){
this("anoymous", -1);
}
}
Java Constructor
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- org.springframework.beans.factory.UnsatisfiedDependencyEx...
- Constructor类理解 这里Constructor,我们知道是构造函数为什么是数组形式的呢?因为可能有多个构...
- 这篇文章总结了Java使用构造函数中最常遇到的五个问题! 1 为什么调用子类的构造方法的时候,默认会调用父类的构造...