java编程思想练习题-第5章练习9-在构造器中调用构造器

题目:编写具有两个构造器的类,并且在第一个构造器中调用第二个构造器。

分析:在类中通过this调用构造器有很多需要遵守的规则

  1. 不能调用两个构造器
  2. 必须将构造器调用置于方法的起始位置
  3. 禁止在除构造器的其他地方调用构造器
public class test {
	public test(){this("default");}
	public test(String s){System.out.println(s);}
	public static void main(String[] args)  {
	test t=new test();
	}
}

 

猜你喜欢

转载自buptchj.iteye.com/blog/2247572
今日推荐