zoukankan      html  css  js  c++  java
  • Java 关于类的构造方法的一点认识

    2019年4月21日 星期天

    在ORACLE官网上提供的The Java™ Tutorials中,有一节课Providing Constructors for Your Classes(为你的类提供构造方法)中提到:

    You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

    首先,看:“You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.”

    这段话的大意是:“你不必为你的类提供任何构造方法,但是当这样做的时候,你必须小心。编译器会自动为没有构造方法的任何类提供一个无参的、默认的构造方法。这个默认的构造方法将会调用父类的无参构造方法。”

    我们通过一个代码示例来理解这段话吧!

    以下提供的示例分别在Eclipse、IntelliJ IDEA运行。

     1 public class Main {
     2 
     3     public static void main(String[] args) {
     4         MySubclass mySubclass = new MySubclass();
     5     }
     6 }
     7 
     8 class MySuperclass {
     9     // 无参构造方法
    10     public MySuperclass() {
    11         System.out.println("I am your superclass.");
    12     }
    13     
    14     // 带参数构造方法
    15     public MySuperclass(String today) {
    16         System.out.println(today);
    17     }
    18 }
    19 
    20 class MySubclass extends MySuperclass {
    21     
    22 }

    运行结果,输出:

    I am your superclass.

    奇怪,为什么运行这个程序之后,会输出“I am your superclass.”呢?在main(String[] args)方法中,并没有见到任何打印方法!

    其实,这正是上文提到的:“编译器会自动为没有构造方法的任何类提供一个无参的、默认的构造方法。这个默认的构造方法将会调用父类的无参构造方法。”

    在第20~22行,声明了一个MySubclass类,而且这个类是继承自MySuperclass类。因为在MySubclass中没有明确地声明任何构造方法,所以,编译器会自动为这个类添加一个无参的、默认的构造方法。而这个无参的、默认的构造方法,则是来自于父类(MySuperclass)。

    在第4行,首先创建一个MySubclass对象(new MySubclass();),随后,编译器发现MySubclass类自身没有声明任何构造方法,但它是继承自MySuperclass类。于是,编译器检查一下MySuperclass类,发现MySuperclass类中声明了两个构造方法,其中一个是无参的构造方法(MySuperclass())。唉!自己(MySubclass类)没有构造方法,那只能调用父类的无参的构造方法了。

    所以,运行这个程序,就会在控制台输出:“I am your superclass.”。这正是因为MySubclass类的对象调用MySuperclass类的无参的构造方法的缘故。(好绕!)

    接着,看:“In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does.”

    这段话的大意是:“在这种情况下,如果父类中没有一个无参的构造方法,编译器将会控诉(发出错误提示),因此你必须检查父类是否提供一个无参的构造方法。”

    我们修改上文的代码示例,看看编译器会有什么反应吧!

    将第10~12行的代码注释掉之后,既MySuperclass类不提供无参的、默认的构造方法。

    Eclipse在第20行给出这样的错误提示:“Implicit super constructor MySuperclass() is undefined for default constructor. Must define an explicit constructor”,这句话的大意是:没有定义一个默认的构造方法MySuperclass(),必须定义一个明确的构造方法。

    其实,这就是上文提到:“如果父类中没有一个无参的构造方法,编译器将会控诉(发出错误提示)。”

    理解能力有限,有一些翻译可能存在歧义而误导大家,欢迎指正!

    未完待续。

  • 相关阅读:
    [LeetCode] 735. Asteroid Collision
    [LeetCode] 14. Longest Common Prefix
    [LeetCode] 289. Game of Life
    [LeetCode] 73. Set Matrix Zeroes
    [LeetCode] 59. Spiral Matrix II
    [LeetCode] 54. Spiral Matrix
    [LeetCode] 48. Rotate Image
    [LeetCode] 134. Gas Station
    [LeetCode] 70. Climbing Stairs
    [LeetCode] 71. Simplify Path
  • 原文地址:https://www.cnblogs.com/Satu/p/10744221.html
Copyright © 2011-2022 走看看