ITPub博客

首页 > 应用开发 > Java > Java 中的构造器链如何实现?

Java 中的构造器链如何实现?

原创 Java 作者:mingtian66 时间:2024-11-06 12:02:28 0 删除 编辑

在 Java 中,构造器链指的是一个构造器调用另一个构造器的机制。这种调用通常通过关键字 this 来实现,以简化构造器代码并避免重复代码。构造器链分为以下两种情况:

    类内部的构造器链:一个构造器调用同一类中的其他构造器。
    父子类的构造器链:子类的构造器调用父类的构造器(通过 super 实现)。

1. 类内部的构造器链

通过 this 关键字,一个构造器可以调用类中的另一个构造器。在同一个构造器中调用其他构造器必须是构造器的第一条语句,这样可以确保构造器按顺序正确地初始化。
示例

class Person {
    String name;
    int age;
    String address;

    // 构造器 1:无参数
    public Person() {
        this("Unknown", 0, "Not specified");  // 调用构造器 3
        System.out.println("Default constructor called");
    }

    // 构造器 2:两个参数
    public Person(String name, int age) {
        this(name, age, "Not specified");  // 调用构造器 3
        System.out.println("Constructor with name and age called");
    }

    // 构造器 3:三个参数
    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
        System.out.println("Constructor with name, age, and address called");
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + ", address='" + address + "'}";
    }
}

public class Main {
    public static void main(String[] args) {
        Person p1 = new Person();
        System.out.println(p1);

        Person p2 = new Person("Alice", 25);
        System.out.println(p2);

        Person p3 = new Person("Bob", 30, "New York");
        System.out.println(p3);
    }
}

输出

Constructor with name, age, and address called
Default constructor called
Person{name='Unknown', age=0, address='Not specified'}

Constructor with name, age, and address called
Constructor with name and age called
Person{name='Alice', age=25, address='Not specified'}

Constructor with name, age, and address called
Person{name='Bob', age=30, address='New York'}

解释

    无参构造器调用了this("Unknown", 0, "Not specified"),这会触发三个参数的构造器,实现默认值初始化。
    两个参数的构造器调用了三个参数的构造器,指定部分默认值。
    这样可以通过构造器链简化代码并复用初始化逻辑。

2. 父子类的构造器链

在继承关系中,子类构造器通常需要调用父类构造器,以确保父类的属性能够正确初始化。可以通过 super 关键字在子类构造器中调用父类构造器。
示例

class Animal {
    String type;

    public Animal() {
        this("Unknown");
        System.out.println("Animal default constructor called");
    }

    public Animal(String type) {
        this.type = type;
        System.out.println("Animal constructor with type called");
    }
}

class Dog extends Animal {
    String breed;

    public Dog() {
        this("Mixed");
        System.out.println("Dog default constructor called");
    }

    public Dog(String breed) {
        super("Dog");  // 调用父类构造器
        this.breed = breed;
        System.out.println("Dog constructor with breed called");
    }

    @Override
    public String toString() {
        return "Dog{type='" + type + "', breed='" + breed + "'}";
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog1 = new Dog();
        System.out.println(dog1);

        Dog dog2 = new Dog("Golden Retriever");
        System.out.println(dog2);
    }
}

输出

Animal constructor with type called
Dog constructor with breed called
Dog default constructor called
Dog{type='Dog', breed='Mixed'}

Animal constructor with type called
Dog constructor with breed called
Dog{type='Dog', breed='Golden Retriever'}

解释

    子类的无参构造器调用了带 breed 参数的构造器。
    带 breed 参数的构造器通过 super("Dog") 调用了父类的带 type 参数的构造器。
    通过这种方式,可以确保父类和子类都完成初始化,构成了构造器链。

注意事项

    在构造器中使用 this 或 super 调用其他构造器时,必须是构造器的第一行语句。
    如果子类构造器没有显式调用父类构造器,Java 编译器会自动插入 super() 调用父类的无参构造器。
    如果父类没有无参构造器,子类构造器必须显式调用父类的其他构造器,否则编译会报错。

小结

    构造器链有助于代码复用,减少重复的初始化代码。
    使用 this 实现类内部的构造器链,通过 super 实现父类构造器链。
    合理使用构造器链可以提升代码的可读性和维护性

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70040412/viewspace-3045368/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2024-08-07

  • 博文量
    589
  • 访问量
    603025