Java 关于构造函数重载
我正在研究构造函数重载,下面是我的代码Java 关于构造函数重载,java,Java,我正在研究构造函数重载,下面是我的代码 class Philosopher { Philosopher(String s) { System.out.print(s + " "); } } public class Kant extends Philosopher { // insert code here Kant() { this("Bart"); //constructor overloading //su
class Philosopher {
Philosopher(String s) {
System.out.print(s + " ");
}
}
public class Kant extends Philosopher {
// insert code here
Kant() {
this("Bart"); //constructor overloading
//super("Bart"); //-->can also write this
}
Kant(String s) {
super(s);
}
public static void main(String[] args) {
new Kant("Homer");
new Kant();
}
}
现在我的问题是,在Kant类中,在它的默认构造函数中,我们正在写这个(“Bart”);它正在调用类本身中的另一个构造函数,我们不能使用super(“Bart”)吗?它也可以实现相同的功能,请建议。在您的情况下,您也可以调用super(“Bart”)
但是,请考虑<代码> KANT(String S)构造函数调用某些附加代码的情况。在这种情况下,
super(“Bart”)
不会执行Kant(String s)
,因此不会调用该构造函数中的附加代码。在您的情况下,您也可以调用super(“Bart”)
但是,请考虑<代码> KANT(String S)构造函数调用某些附加代码的情况。在这种情况下,
super(“Bart”)
将不会执行Kant(String s)
,因此不会调用该构造函数中的附加代码。在这种特殊情况下,是的,您可以只使用super(“Bart”)
-这两种情况下都不是构造函数链接。重载只是有多个构造函数
但是,通常最好将一个类中的所有构造函数链接到该类的“主”构造函数,该类是唯一具有
super(…)
调用的构造函数。这样,无论您在该构造函数中放入什么逻辑,都将在调用哪个构造函数时执行。在这种情况下,它是不相关的,因为构造函数中没有任何其他逻辑,但通常情况下并非如此。在这种特殊情况下,是的,您可以只使用super(“Bart”)
-这两种情况下都不是构造函数链接。重载只是有多个构造函数
但是,通常最好将一个类中的所有构造函数链接到该类的“主”构造函数,该类是唯一具有
super(…)
调用的构造函数。这样,无论您在该构造函数中放入什么逻辑,都将在调用哪个构造函数时执行。在本例中,这是不相关的,因为构造函数中没有任何其他逻辑,但通常情况下不是这样。我认为这更多地是在您所看到的内容之后进行的,这就是构造函数中父类和子类中的this(…)
和super(…)
之间的区别
首先,我做了这个全面的测试程序并运行了它。我讨厌在建造时提出不正确的主张
public class Philosopher {
Philosopher(String s) {
System.out.println(s);
System.out.print("from Philosopher(String s){System.out.println(s);}... ");
}
Philosopher() {
this("whatev... ");
System.out.print("from Philosopher(){this(\"whatev... \");}... ");
}
}
class Kant extends Philosopher {
Kant(String s) {
System.out.println(s);
System.out.print("from Kant(String s){System.out.println(s);}... ");
}
Kant() {
this("whatev... ");
System.out.println("from Kant(){this(\"whatev... \");}... ");
}
Kant(int i) {
super("whatev... ");
System.out.println("from Kant(int i){super(\"whatev... \");}... ");
}
public static void main(String[] args) {
String s = "String \"s\" InMain";
System.out.print("\n\n new Philosopher(s); produces:\n");
new Philosopher(s);
System.out.print("\n\n new Philosopher(); produces:\n");
new Philosopher();
System.out.print("\n\n new Kant(s); produces:\n");
new Kant(s);
System.out.print("\n\n new Kant(); produces:\n");
new Kant();
System.out.print("\n\n new Kant(69); produces:\n");
new Kant(69);
}
}
注释输出
当我运行它时,我得到这个输出。我在输出的每一行插入我自己的注释
new Philosopher(s);` produces
String "s" InMain
from Philosopher(String s){System.out.println(s);}...
毫不奇怪,在这里调用超类的构造函数,它不引用this()
或super()
,只运行超类的构造函数
new Philosopher(); produces:
whatev...
from Philosopher(String s){System.out.println(s);}... from Philosopher(){this("whatev... ");}...
没什么好惊讶的this()
使用this(“whatev…”)
,而this()
引用哲学家(字符串s)
构造函数。因此,在这个构造中调用了scholator()
和scholator(字符串s)
new Kant(s); produces:
whatev...
from Philosopher(String s){System.out.println(s);}... from Philosopher(){this("whatev... ");}... String "s" InMain
from Kant(String s){System.out.println(s);}...
这个让我很惊讶!一个简单的构造函数Kant(String s)
不使用this()
或super()
调用任何东西,但它在构造Kant
时访问了三个构造函数每个没有显式调用父类构造函数的子类构造函数都会神奇地调用零参数父类构造函数强>
因此,任何Kant
构造函数,如果没有引用super
,其行为就像super()代码>是它的第一行!因此,我们有从父代到子代的结构连续性
new Kant(); produces:
whatev...
from Philosopher(String s){System.out.println(s);}... from Philosopher(){this("whatev... ");}... whatev...
from Kant(String s){System.out.println(s);}... from Kant(){this("whatev... ");}...
Kant()
中有一个this
,但没有super
。因此,Java神奇地表现得就像在我们的Kant()
构造函数中有一个super()
一样,即使没有
new Kant(69); produces:
whatev...
from Philosopher(String s){System.out.println(s);}... from Kant(int i){super("whatev... ");}...
因此,Kant(inti)
确实有一个对父类构造函数的显式引用。因此它的输出相对简单,因为我们直接引用了哲学家(String s)
,所以我们不需要通过隐式父构造函数调用的哲学家()
走额外的弯路
结论
子类构造函数将始终调用父类构造函数。如果未指定要调用的父类构造函数,则将在子类构造函数中的任何其他代码之前调用零参数构造函数。如果您指定了要调用的父类构造函数,那么您就有了更好的控制
使用this
和super
的构造函数可以链接在一起,在一个显然简单的new Kant()
对象构造过程中,最多可以访问4个构造函数
如果你一直读到这一点,你应该得到某种奖励!请对我发表评论…我认为这更符合您所看到的内容,这就是父类和子类中构造函数中的this(…)
和super(…)
之间的区别
首先,我做了这个全面的测试程序并运行了它。我讨厌在建造时提出不正确的主张
public class Philosopher {
Philosopher(String s) {
System.out.println(s);
System.out.print("from Philosopher(String s){System.out.println(s);}... ");
}
Philosopher() {
this("whatev... ");
System.out.print("from Philosopher(){this(\"whatev... \");}... ");
}
}
class Kant extends Philosopher {
Kant(String s) {
System.out.println(s);
System.out.print("from Kant(String s){System.out.println(s);}... ");
}
Kant() {
this("whatev... ");
System.out.println("from Kant(){this(\"whatev... \");}... ");
}
Kant(int i) {
super("whatev... ");
System.out.println("from Kant(int i){super(\"whatev... \");}... ");
}
public static void main(String[] args) {
String s = "String \"s\" InMain";
System.out.print("\n\n new Philosopher(s); produces:\n");
new Philosopher(s);
System.out.print("\n\n new Philosopher(); produces:\n");
new Philosopher();
System.out.print("\n\n new Kant(s); produces:\n");
new Kant(s);
System.out.print("\n\n new Kant(); produces:\n");
new Kant();
System.out.print("\n\n new Kant(69); produces:\n");
new Kant(69);
}
}
注释输出
当我运行它时,我得到这个输出。我在输出的每一行插入我自己的注释
new Philosopher(s);` produces
String "s" InMain
from Philosopher(String s){System.out.println(s);}...
毫不奇怪,在这里调用超类的构造函数,它不引用this()
或super()
,只运行超类的构造函数
new Philosopher(); produces:
whatev...
from Philosopher(String s){System.out.println(s);}... from Philosopher(){this("whatev... ");}...
没什么好惊讶的this()
使用this(“whatev…”)
,而this()
引用哲学家(字符串s)
构造函数。因此,在这个构造中调用了scholator()
和scholator(字符串s)
new Kant(s); produces:
whatev...
from Philosopher(String s){System.out.println(s);}... from Philosopher(){this("whatev... ");}... String "s" InMain
from Kant(String s){System.out.println(s);}...
这个让我很惊讶!一个简单的构造函数Kant(Str