构造方法的调用-要点
2009-11-21 14:41
120 查看
package com.rk.www.TestForInterView; /** * 测试构造方法的调用(this) * 要点: * 1,构造方法只能由构造方法调用,只能一次调用一个 * 2,构造方法调用只能在第一句 * * @author jacky * */ public class TestForConstructor { /** * int型成员变量 */ int petalCount = 0; /** * String型成员变量 */ String s = "initial value"; /** * 只接收int型构造方法 * * @param petals */ TestForConstructor(int petals) { petalCount = petals; System.out .println("Construnctor w/ int arg only,petalCount=" + petalCount); } /** * 只接收String型成员变量 * * @param ss */ TestForConstructor(String ss) { System.out .println("Construnctor w/ int String only,s=" + ss); } /** * 两个参数的构造方法 * * @param s * @param petals */ TestForConstructor(String s, int petals) { this(petals); /** * 不能在构造方法里面调用两次其他构造方法 */ // this(s); this.s = s; System.out .println("String & int args"); } /** * 无参数构造方法 */ TestForConstructor() { this("hi", 47); System.out .println("default constructor (no args)"); } /** * 成员方法 */ void printPetalCount() { /** * Constructor call must be the first statement in a constructor */ // this(11); System.out .println("petalCount=" + petalCount + " s=" + s); } public static void main( String[] args) { TestForConstructor testForConstr = new TestForConstructor(); testForConstr.printPetalCount(); } }
相关文章推荐
- Java中子类与父类的构造方法的调用关系
- 构造方法的重载要点
- 【C#基础】方法及其调用、构造方法、out与ref参数及其返回值、方法重载、静态方法等简单介绍
- java中关于子类实例化时调用父类无参构造方法的问题
- JAVA中super()作用:调用父类构造方法和构造方法链
- parent 调用父类的构造方法与被覆写的方法
- java中构造方法的调用顺序
- OC 方法调用方式,new 方法实现原理,oc自定义构造方法:
- 【C#基础】方法及其调用、构造方法、out与ref参数及其返回值、方法重载、静态方法等简单介绍
- 自定控件时 三个构造方法的调用
- java父类构造方法的调用
- C++对象数组调用构造/析构函数的方法
- 反射机制构造实例调用方法
- java 构造方法 super 隐式调用 显式调用
- 调用另一个构造方法
- Java中构造方法调用另一个构造方法
- 10. JavaSE-子类实例化过程 & 构造方法间调用
- 反射深入 通过反射中的Consrtuctor类调用有参构造方法 普通方法Method
- Java反射之调用构造方法and操作数组
- Java语言深入:构造方法调用浅谈