知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

      • Theory:Introduction OOP
      • Theory:Defining classes
      • Theory:Instance methods
      • Thoery:Constructor
      • Theory:Package
      • Theory:Access modifiers
      • Theory:Getters and setters
      • Theory:Inheritance
      • Theory:Protected modifier
      • Theory:Referencing subclass objects
      • Theory:The Object class
      • Theory:Objects
      • Theory:Interface
      • Theory:Multiple constructors
        • Constructor overloading
        • Invoking constructors from other constructors
        • Conclusion
      • Theory:The keyword super
      • Theory:Static members
      • Theory:Hiding and overriding
      • Theory:Polymorphism
      • Theory:Abstract class
      • Theory:Abstract class vs interface
      • Theory:Anonymous classes
      • Theory:Anonymous classes properties
    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Java OOP
Jim
2022-05-16
目录

Theory:Multiple constructors

Sometimes we need to initialize all fields of an object when creating it, but there are cases in which it might be appropriate to initialize only one or several fields. Fortunately, for this purpose, a class can have several constructors that assign values to the fields in different ways. In this topic, you will learn how to work with multiple constructors and define the way they interact with each other.

# Constructor overloading

You can define as many constructors as you need. Each constructor should have a name that matches the class name but the parameters should be different. The situation when a class contains multiple constructors is known as constructor overloading.

Here is an example:

public class Robot {
    String name;
    String model;

    public Robot() {
        this.name = "Anonymous";
        this.model = "Unknown";
    }

    public Robot(String name, String model) {
        this.name = name;
        this.model = model;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

The class Robot has two constructors:

  • Robot() is a no-argument constructor that initializes fields with default values;
  • Robot(String name, String model) takes two parameters and assigns them to the corresponding fields.

To create an instance of the class Robot we can use either of the two constructors:

Robot anonymous = new Robot(); // name is "Anonymous", model is "Unknown"
Robot andrew = new Robot("Andrew", "NDR-114"); // name is "Andrew", model is "NDR-114"
1
2

注意

Bear in mind that you cannot define two constructors with the same number, types, and order of the parameters!

# Invoking constructors from other constructors

We can also invoke a constructor from another one. It allows you to initialize one part of an object by one constructor and another part by another constructor.

Calling a constructor inside another one is done using this. For example:

this(); // calls a no-argument constructor
1

If you called a constructor that has parameters you can pass some arguments:

this("arg1", "arg2"); // calls a constructor with two string arguments
1

注意

Remember, the statement for invoking a constructor should be the first statement in the body of a caller constructor.

Here is an extended example of the Robot class:

public class Robot {
    String name;
    String model;
    int lifetime;

    public Robot() {
        this.name = "Anonymous";
        this.model = "Unknown";
    }

    public Robot(String name, String model) {
        this(name, model, 20);
    }

    public Robot(String name, String model, int lifetime) {
        this.name = name;
        this.model = model;
        this.lifetime = lifetime;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Now, the class has three constructors:

  • Robot() is a no-argument constructor;
  • Robot(String name, String model) is a two-argument constructor that invokes another constructor;
  • Robot(String name, String model, int lifetime) is a three-argument constructor that fills all fields.

The second constructor invokes the third one and passes name, model, and lifetime = 20 to it. The third constructor, in its turn, initializes all fields of the created object.

Let's add an output to the third constructor and see the result:

public Robot(String name, String model, int lifetime) {
    this.name = name;
    this.model = model;
    this.lifetime = lifetime;
    System.out.println("The third constructor is invoked");
}
1
2
3
4
5
6

Let's now create an instance using the two-argument constructor.

Robot andrew = new Robot("Andrew", "NDR-114");
1

The program outputs:

The third constructor is invoked
1

# Conclusion

In this topic, we've covered constructor overloading — creating multiple constructors for the class. Constructor overloading allows us to create an object of the class in different ways depending on the circumstances.

We can also invoke constructors inside other constructors. All in all, Java provides many useful features for writing constructors and defining interactions between them.

编辑 (opens new window)
#Java#OOP
上次更新: 2022/09/26, 16:55:15
Theory:Interface
Theory:The keyword super

← Theory:Interface Theory:The keyword super→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式