在JAVA中创建构造函数时,它是重载的还是被覆盖的?

huangapple go评论79阅读模式
英文:

When we create a constructor in JAVA, is it overloaded or overridden?

问题

在Java中,
重载是指创建具有相同名称但不同签名的方法,而重写是指创建具有相同名称和相同签名的方法。

那么,在Java中的子类中创建构造函数时会发生重载还是重写呢?

英文:

In Java,
Overloading is creating methods with the same name but different signature and Overriding is creating methods with the same name and the same signature.

So what happens, overloading or overriding, when we create a constructor in child class in JAVA?

答案1

得分: 4

Sure, here's the translation:

"两者都不是。构造函数与方法不同。您通过在同一类中编写多个构造函数来重载构造函数,而不是在继承类中重载。构造函数不受覆盖的影响。如果我调用 new X("Hi"),我知道我正在调用在 X 中定义和实现的构造函数,而不是调用在 X 的某个子类中覆盖的构造函数,或者继承自 X 的某个超类中的构造函数。"

英文:

Neither. Constructors are different from methods. You overload a constructor by writing multiple constructors in the same class, not in inherited classes. And constructors aren't subject to overriding. If I call new X("Hi"), I know I'm calling a constructor defined and implemented in X, not a constructor overridden in some subclass of X, or inherited from some superclass of X.

答案2

得分: 1

根据 JLS §8.8

> 构造函数声明不是成员。它们永远不会被继承,因此不受隐藏或覆盖的影响

因此,这不是覆盖。并且由于构造函数不会被继承,所以在子类中声明构造函数时也不会重载超类中的构造函数。然而,它是:

  • 重载同一类中声明的其他构造函数(如果有的话),或者;

  • 替换默认构造函数(如果之前没有声明任何构造函数)。

英文:

According to the JLS §8.8,

> Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

So it's not overriding. And since constructors are not inherited, it's not overloading the constructor in the super either, when you declare a constructor in the subclass. It is however:

  • overloading other constructors declared in the same class, if any, or;

  • replacing the default constructors if no constructors have been declared before.

答案3

得分: 0

A类由成员(字段或方法)和构造函数组成 - 作为完全不同的事物。但是您想要进行概念上的比较。

class A {
    A() { }
}

class B extends A {
    B() {
        System.out.println("Bar");
    }
}

在JVM(Java虚拟机)内部,构造函数的签名是A.<init>()

B()在内部的实现如下:

int field1;
String field2;
String field3 = null;
int field4 = 42;

B() {
    // 1. 显式或隐式的父类构造函数:
    super(); // A.<init>();

    // 2. 所有带初始化的字段:
    field3 = null;
    field4 = 42;

    // 3. 剩余的代码:
    System.out.println("Bar");
}

这看起来像是重写了A.<init>()(尽管不允许使用@Override),但基础方法被隐式地调用(无参数),或者可以显式地使用super(...)调用。

重载意味着在B中有不止一个构造函数。

英文:

A class consists of members (fields or methods) and constructors - as entirely different things. But you want to do a conceptual comparison.

class A {
    A() { }
}

class B extends A {
    B() {
        System.out.println(&quot;Bar&quot;);
    }
}

Internally in the JVM (java virtual machine) the constructor signatures are A.&lt;init&gt;()

And B() is implemented internally as:

int field1;
String field2;
String field3 = null;
int field4 = 42;

B() {
    // 1. Explicit or implicit super constructor:
    super(); // A.&lt;init&gt;();

    // 2. All fields with initialisation:
    field3 = null;
    field4 = 42;

    // 3. The remaining code:
    System.out.println(&quot;Bar&quot;);
}

This looks like overriding A.&lt;init&gt;() (though @Override is not allowed), but the base method is called implicitly (parameterless), or explicitly as super(...).

Overloading means having more than 1 constructor in B.

huangapple
  • 本文由 发表于 2020年8月14日 19:07:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63411597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定