java 构造器中调用构造器
来源:互联网 发布:kik软件下载 编辑:程序博客网 时间:2024/06/07 20:35
//:object/Practice.java
/**Calling constructors with "this"
* @author Female_
* */
public class Practice {
int petalCount=0;
String s ="initals value";
Practice(int petals){
petalCount = petals;
System.out.println("Constructor w/ int arg only, petalCount= "+petalCount);
}
Practice(String ss){
System.out.println("Constructor w/ String arg only ,s= "+ss);
s=ss;
}
Practice(String s,int petals){
this(petals);
// !this(s); 不能用this调用两个构造器,且必须将构造器置于最起始处
this.s=s;
System.out.println("String & int args");
}
Practice(){
this("hi",47);
System.out.println("default constructor (no args)");
}
void printPetalCount() {
//! this(11); 除构造器之外,编译器禁止在其他任何方法中调用构造器
System.out.println("petalCount= "+petalCount+" s= "+s);
}
public static void main(String[] args) {
Practice a=new Practice();
a.printPetalCount();
}
}/*Ouput:
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount= 47 s= hi
*/
/**Calling constructors with "this"
* @author Female_
* */
public class Practice {
int petalCount=0;
String s ="initals value";
Practice(int petals){
petalCount = petals;
System.out.println("Constructor w/ int arg only, petalCount= "+petalCount);
}
Practice(String ss){
System.out.println("Constructor w/ String arg only ,s= "+ss);
s=ss;
}
Practice(String s,int petals){
this(petals);
// !this(s); 不能用this调用两个构造器,且必须将构造器置于最起始处
this.s=s;
System.out.println("String & int args");
}
Practice(){
this("hi",47);
System.out.println("default constructor (no args)");
}
void printPetalCount() {
//! this(11); 除构造器之外,编译器禁止在其他任何方法中调用构造器
System.out.println("petalCount= "+petalCount+" s= "+s);
}
public static void main(String[] args) {
Practice a=new Practice();
a.printPetalCount();
}
}/*Ouput:
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount= 47 s= hi
*/
阅读全文
0 0
- java 构造器中调用构造器
- Java中构造器的调用顺序
- Java中构造器间的调用
- java 中构造器的调用顺序
- this 构造器中调用构造器
- 在构造器中调用构造器
- java的构造器调用
- java 构造器调用顺序
- Java构造器调用规范
- java 构造方法的相互调用(在一个构造器中,通过this调用另一个构造器)
- Java构造函数中调用构造函数
- Java构造器:级联调用,调用兄弟构造器
- 构造中调用构造
- java中在类的构造器中调用另一个构造器
- Java中this的用法及在构造器中调用构造器
- 初学Java,在构造器中调用另一个构造器(十)
- 初学Java,在构造器中调用另一个构造器(十)
- JAVA编程思想(第4版) 在构造器中调用构造器
- Spring security搭建与使用
- BZOJ 1494 [NOI2007]生成树计数
- Android图片加载框架最全解析(一),Glide的基本用法
- 13-2Map集合
- Linux 文件 IO
- java 构造器中调用构造器
- 21、访问者模式(Visitor)
- tslib库编译
- C# WPF动点任意移动气泡画法(解决方案使用到数学勾股定理、正弦定理、向量知识)。
- Kafka集群的测试和简单试用
- 适配 iPhoneX & iOS11 新特性
- 单例之单例的六种写法
- Charles使用 (抓取HTTPS包)
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等