Thinking in Java 构造器,初始化

用构造器确保初始化

可以假想为编写的每个类都定义一个initialize()方法。该方法的名称提醒你在使用其对象之前,应首先调用initialize()。然而,这同时意味着用户必须得自己去调用此方法。在Java中,通过提供构造器,类的设计者可确保每个对象都得到初始化。创建对象时,如果其类具有构造器,Java就会在用户有能力操作对象之前自动调用相应的构造器,确保初始化
关于命名,第一:所取的任何名字都有可能与类的某个成员名称相冲突;第二,调用构造器时编译器的责任,所以必须让编译器直到调用哪个方法。构造器采用与类相同的名称。

class Rock{
    Rock(){
        //构造器
        System.out.print("Rock ");
    }
}
/**
 * SimpleConstructor
 */
public class SimpleConstructor {

    public static void main(String[] args) {
        for(int i=0;i<10;i++){
            new Rock();
        }
    }
}

输出结果:Rock Rock Rock Rock Rock Rock Rock Rock Rock Rock

现在,在创建对象时:

new Rock();

将会为对象分配存储空间,并调用相应的构造器。这就确保了在你能操作对象之前,他已经被恰当的初始化了。
不接受任何参数的构造器叫做默认构造器,和其他方法一样,构造器也能带有形式参数,以便指定如何创建对象。对上述例子稍加修改,即可使构造器接受一个参数

class Rock2{
    Rock2(int i){
        System.out.print("Rock"+i+" ");
    }
}
public class SimpleConstructor2 {
    public static void main(String[] args) {
        for(int i=0;i<8;i++){
            new Rock2(i);
        }
    }
}

输出结果:Rock0 Rock1 Rock2 Rock3 Rock4 Rock5 Rock6 Rock7
有了构造器形式参数,就可以在初始化对象时提供实际参数。例如,假设类Tree有一个构造器,它接受一个整形变量来接受树的高度,就可以这样创建一个Tree对象:

Tree t=new Tree(12);

构造器有助于减少错误,并使代码更容易阅读。从概念上来讲,“初始化”与“创建”时彼此独立的,然而在上面的代码中,你却找不到对initialize()方法的明确调用。在Java中,“初始化”和“创建”捆绑在一起,两者不能分离。
构造器时一种特殊类型的方法,因为它没有返回值。(与返回空不相同)

关于方法重载

在Java中,构造器是强制重载方法名的另一个原因。既然构造器的名字已经由类名决定,就只能有一个构造器名。那么想用多种方式创建一个对象该怎么办呢?假设你要创建一个类,既可以用标准方式初始化,也可以从文件里读取信息来初始化。这就需要两个构造器:一个默认构造器,另一个取字符串作为形式参数——该字符串表示初始化对象所需的文件名称。由于都是构造器,所以它们必须由相同的名字,即类名。为了让方法名相同而形式参数不同的构造器同时存在,必须用到方法重载。同时,尽管方法重载是构造器所必需的,但也可用于其他方法。

下面这个例子同时示范了重载的构造器和重载的方法

class Tree{
    int height;
    Tree(){
        System.out.println("planting a seeding");
        height=0;
    }
    Tree(int initialHeight){
        height=initialHeight;
        System.out.println("creating new tree that is"+height+"feet tall");
    }
    void info(){
        System.out.println("tree is "+height+"feet tall");
    }
    void info(String s){
        System.out.println(s+": tree is"+height+"feet tall");
    }
}
/**
 * Overloading
 */
public class Overloading {

    public static void main(String[] args) {
        for(int i=0;i<5;i++){
            Tree t=new Tree(i);
            t.info();
            t.info("overloaded method");
        }
        //构造器重载
        new Tree();
    }
}

结果:

creating new tree that is0feet tall
tree is 0feet tall
overloaded method: tree is0feet tall
creating new tree that is1feet tall
tree is 1feet tall
overloaded method: tree is1feet tall
creating new tree that is2feet tall
tree is 2feet tall
overloaded method: tree is2feet tall
creating new tree that is3feet tall
tree is 3feet tall
overloaded method: tree is3feet tall
creating new tree that is4feet tall
tree is 4feet tall
overloaded method: tree is4feet tall
planting a seeding

创建Tree对象的时候,既可以不含参数,也可以用树的高度当参数。前者表示一颗树苗,后者表示有一定高度的树木。要支持这种创建方式,得有一个默认构造器和一个采用现有高度作为参数的构造器。
或许你还想通过多种方式调用info()方法。例如,你想显示额外信息,可以用info(String)方法;没有的话就用info()。好在有了方法重载,可以为两者使用相同的名字。

默认构造器

如前所述,默认构造器是没有形式参数的——它的作用是创建一个“默认对象”。如果你写的类中没有构造器,编译器会自动帮你创建一个默认构造器。
例如:

class Bird{

}
public class DefaultConstructor {

    public static void main(String[] args) {
        Bird b=new Bird();//Default
    }
}

new Bird()
创建了一个新对象,并调用其默认构造器——即使你没有明确定义它。没有它的话,就没有方法可调用,就无法创建对象。但是,如果已经定义了一个构造器(无论是否有参数),编译器就不会帮你自动创建默认构造器:

class Bird2{
    Bird2(int i){}
    Bird2(double d){}
}
public class DefaultConstructor {

    public static void main(String[] args) {
        // Bird2 b=new Bird2();//这么写是错误,因为没有匹配的构造器
        Bird2 b2=new Bird2(1);
        Bird2 b3=new Bird2(1.0);
    }
}

要是这样写

new Bird2()

编译器就会报错:没有找到匹配的构造器。这就好比,要是你没有提供任何构造器,编译器会认为“你需要一个构造器,让我给你制造一个把”,但如果你已经写了一个构造器,编译器则会认为“啊,你已经写了一个构造器;所以知道你在做什么;你是刻意省略了默认构造器。”

在构造器中调用构造器

可能为一个类写了多个构造器,有时可能想在一个构造器中调用另一个构造器,以避免重复代码。可用this关键字做到这一点
通常写this的时候,都是指“这个对象”或者“当前对象”,而且它本身表示对当前对象的引用。在构造器中,如果为this添加了参数列表,那么就有了不同的含义。这将产生对符合此参数列表的某个构造器的明确调用;这样,调用其他构造器就有了直接的途径

public class Flower {
    int petalCount=0;
    String s="initial value";
    Flower(int petals){
        petalCount=petals;
        System.out.println("constructor w/ arg only.petalCount= "+petalCount);
    }
    Flower(String ss){
        System.out.println("constructor w/string arg only.s="+ ss);
        s=ss;
    }
    Flower(String s,int petals){
        this(petals);
        this.s=s;
        System.out.println("String & int args");
    }
    Flower(){
        this("hi", 47);
        System.out.println("dafault constructor (no args)");
    }
    void printPeataCount(){
        System.out.println("patalCount= "+petalCount+" "+"s= "+s);
    }
    public static void main(String[] args) {
        Flower x=new Flower();
        x.printPeataCount();
    }
}

结果

constructor w/ arg only.petalCount= 47
String & int args
dafault constructor (no args)
patalCount= 47 s= hi

构造器Flower(String s,int petals)表明,尽管可以用this调用一个构造器,但却不能调用两个。此外,必须将构造器调用置与最起始处,否则编译器会报错
这个例子也展示了this的另一种用法。由于参数s的名称和数据成员s的名字相同,所以会产生歧义。使用this.s来代表数据成员就能解决这个问题。在Java程序代码中精创出现这种写法
printPeataCount()也表明了,除构造器之外,编译器禁止在其他任何方法中调用构造器。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值