Java 构造器
原文: https://javabeginnerstutorial.com/core-java-tutorial/constructors-in-java/
Java 中的构造器可以视为类中的方法。 但是构造器和方法之间有很大的区别。 可以根据目的,语法和调用来定义这些差异。
Java 构造器的目的(Vs 方法)
构造器只有一个目的,创建一个类的实例。 该实例包括内存分配和成员初始化(可选)。
相比之下,不能使用方法创建类的实例。
构造器的语法(Vs 方法)
/** Here Class name is ConstructorExample, So constructor name needs to be the same.*/public class ConstructorExample {/** As below signature has the name as Class name and it doesn't contain any* return value so it will be treated as Constructor of the class*/public ConstructorExample() {System.out.println("Inside Constructor");}/** Below method will be invoked only when it is invoked implicitly.* Method has return type along with Non Access Modifier*/static void method() {System.out.println("This is in method");}}
构造器的语法不同于下面描述的方法。
- 构造器不能具有非访问修饰符,而方法可以。
- 构造器不能具有返回类型(包括
void),而方法需要它。 - 构造器名称必须与类名称相同,而方法不受限制。
- 根据 Java 命名约定,方法名称应为驼峰式,而构造方法名称应以大写字母开头。
方法可以具有与类名称相同的名称。
构造器的调用(Vs 方法)
构造器和方法的调用方式有所不同。 构造器不能显式调用,在生成类的实例时将隐式调用构造器(使用new关键字)
构造器调用示例
/** Here Class name is ConstructorExample, So constructor name needs to be the same.*/public class ConstructorExample {/** As below signature has the name as Class name and it doesn't contain any* return value so it will be treated as Constructor of the class*/public ConstructorExample() {System.out.println("Inside Constructor");}public static void main(String args[]){ConstructorExample cls = new ConstructorExample();}}//Output will be//Inside Constructor
方法调用示例
/** Here Class name is ConstructorExample, So constructor name needs to be the same.*/public class ConstructorExample {/** As below signature has the name as Class name and it doesn't contain any* return value so it will be treated as Constructor of the class*/public ConstructorExample() {System.out.println("Inside Constructor");}/** Below method will be invoked only when it is invoked implicitly.*/void method() {System.out.println("This is in method");}public static void main(String args[]) {ConstructorExample cls = new ConstructorExample();/** Now method will be called explicitly as below. It will execute the* code within method.*/cls.method();}}//The output would beInside ConstructorThis is in method
类中的构造器必须与给定的类具有相同的名称。 构造器的语法不包含返回类型,因为构造器从不返回值。 构造器还可以包括各种类型的参数。 使用new运算符调用构造器时,类型必须与构造器定义中指定的类型匹配。
如果未提供显式构造器,则 Java 提供一个默认构造器,其中不带参数并且不执行任何特殊操作或初始化。 隐式默认构造器执行的唯一操作是使用super()调用来调用超类构造器。
Java 构造器规则
- 构造器不能具有返回类型。
- 构造器必须与类具有相同的名称。
- 构造器无法标记为静态
- 构造器不能标记为抽象
- 构造器不能被覆盖。
- 构造器不能是最终的。
如果类定义了显式构造器,则它不再具有默认的构造器来设置对象的状态。 如果此类需要默认构造器(不带参数的构造器),则必须提供其实现。
如果在这种情况下未提供显式的默认构造器,则任何调用默认构造器的尝试都将是编译时错误。
构造器重载:
像方法一样,构造器也可以重载。 由于类中的所有构造器都具有与该类相同的名称,因此它们的签名通过其参数列表来区分。
可以使用this()构造在类中实现构造器的本地链接。 构造器中的this()调用使用同一类中的相应参数列表来调用其他构造器。 Java 要求任何this()调用都必须在构造器中作为第一条语句发生。
构造器链接:
每个构造器中都包含一个隐式super()调用,该构造器不包含this()或显式super()调用作为第一个调用语句。 super()语句用于调用超类的构造器。
隐式super()可以由显式super()代替。 超级语句必须是构造器的第一条语句。 显式超类允许将参数值传递给其超类的构造器,并且必须具有匹配的参数类型。 子类的构造器中的super()调用将基于调用的签名,导致超类中相关构造器的调用。 这称为构造器链接。
super()或this()构造:如果在构造器中使用,则它必须作为构造器中的第一条语句出现,并且只能在构造器声明中使用。 这意味着this()和super()调用不能同时出现在同一构造器中。 就像this()构造导致同一类中的构造器链接一样, super()构造也导致了子类构造器与超类构造器的链接。 如果构造器既没有this()也没有super()构造作为其第一条语句,则调用超类默认构造器的super()被隐式插入。
如果一个类仅定义非默认构造器,则其子类将不包含隐式
super()调用。 这将被标记为编译时错误。然后,子类必须使用带有正确参数的
super()构造与超类的适当构造器进行匹配,来显式调用超类构造器。
备忘单
- 创建新的对象时,调用构造器。
- 构造器也可以重载,但不能将覆盖。
- 每个类至少有一个构造器。 如果用户不提供任何内容,则 JVM 将提供默认的无参数构造器。
- 抽象类也具有构造器。
- 构造器必须与该类具有相同名称。
- 构造器不能具有返回类型。
- 如果与类同名的方法具有返回类型,则将其视为普通成员方法,而不是构造器。
- 构造器可以具有任何访问修饰符(全部)。
- 默认构造器是一个无参构造器,它调用超类的无参构造器。 如果超类没有任何无参数构造器,则它将抛出运行时异常。
- 在类具有默认构造器的情况下,其超类需要具有无参数构造器。
- 构造器的第一个语句可以是
this或super,但不能同时使用。 - 如果编码器未编写任何
this()或super()调用,则编译器将添加super()调用。 super()用于从超类调用构造器。this()用于从同一类调用构造器。- 实例成员仅在超类构造器运行后才能访问。
- 接口没有构造器。
- 构造器是未继承的。 因此,无法将覆盖。
- 构造器不能直接调用。 当创建新对象或由其他构造器调用新对象时,将被调用(隐式)。
https://www.youtube.com/embed/l0b2qWuty2E?start=1&feature=oembed