《JAVA编程思想》学习备忘(第155页:Initialization & Cleanup)-3

 续《JAVA编程思想》学习备忘(第155页:Initialization & Cleanup)-2
Calling constructors from constructors
When you write several constuctors for a class,there are times when you'd like to call one constructor from another to avoid duplicating code.You can make such a call by using the this keyword.
Normally,when you say this,it is in the sense of"this object"or"the current object,"and by itself it produces the reference to the current object.In a constructor,the this keyword takes on a different meaning when you give it an argument list.It makes an explicit call to the constructor that matches that argument list.Thus you have a straightforward way to call other constructors:
实例:
import static staticPrint.Print.print;
public class Flower{
    int petalCount = 0;
    String s = "initial value";
    Flower(int petals){
        petalCount = petals;
        print("Constructor w/ int arg only, petalCount = " + petalCount);
    }
    Flower(String ss){
        print("Constructor w/ String arg only, s = " + ss);
        s = ss;
    }
    Flower(String s, int petals){
        this(petals);
// !  this(s); // Can't call two!
        this.s = s; // Another use of "this"
        print("String & int args");
    }
    Flower(){
        this("hi",47);
        print("default constructor (no args)");
    }
    void printPetalCount(){
// ! this(11); // not inside non-constructor!
        print("petalCount = " + petalCount + " s = " + s);
    }
    public static void main(String[] args){
        Flower x = new Flower();
        x.printPetalCount();
    }
}
以上输出:
Constructor w/ int arg only, petalCount = 47
String & int args
default constructor (no args)
petalCount = 47 s = hi
注意程序轨迹:在 main 方法中实例化类的对象名为 x ,调用默认构造方法 Flower() ,在进入默认无参构造方法 Flower() 后,用关键字 this 传两个参数:"hi", 47 以调用双参构造方法 Flower(String s, int petals),进入双参构造方法Flower(String s, int petals)后,又用关键字 this 传整形参 petals (注意,此时 petals 的值为 47), 以调用单参构造方法 Flower(int petals),进入单参构造方法 Flower(int petals)后,首先给petalCount 付值 47,然后打印出第1条语句,至此 Flower(int petals)内程序全部执行完毕,宣告双参构造方法Flower(String s, int petals)中执行完了第一行this(petals)的调用,接下来用传过来的字符串参数 "hi",将字符串 s 的初始值 "initial value" 覆盖掉,接下来打印出第二条语句,至此 Flower(String s, int petals)内程序全部执行完毕,同理也宣告了默认构造方法 Flower() 中 this("hi", 47) 调用程序执行完毕,接下来程序执行下一行打印码输出了第3条语句,至此,类 Flower 的对象 x 实例化完结,程序该执行下一行代码了:对象 x 调用方法 printPetalCount() 。程序进入 printPetalCount() 中那条打印代码输出最后一条语句,可以看到 petalCount 已被付值为 47 ,字符串 s 已被付值为 hi 。
The constructor Flower(String s,int petals)shows that,while you can call one constructor using this,you cannot call two.In addition,the constructor call must be the first thing you do,or you'll get a compiler error message.
This example also shows another way you'll see this used.Since the name of the argument s and the name of the member data s are the same,there's an ambiguity.You can resolve it using this.s,to say that you're referring to the member data.You'll often see this form used in Java code,and it's used in numerous places in this book.
In printPetalCount() you can see that the compiler won't let you call a constructor from inside any method other than a constructor.
(待续)
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值