![](/img/trans.png)
[英]Why is constructor of super class invoked when we declare the object of sub class? (Java)
[英]Why constructor is not invoked after creating Object of the class?
public class A {
void A() {
System.out.println("Class A");
}
public static void main(String[] args) {
new A();
}
}
运行该程序后,输出new A();
并不意味着什么new A();
void A(){}
不被调用。
void A(){}
不是构造函数吗?
void A()
是常规方法,而不是构造函数。
A()
(无返回类型)是构造函数。
A()
{
System.out.println("Class A");
}
您没有在类中编写构造函数,而是编写了一个普通方法,但是编译器为您提供了默认构造函数(无参数的构造函数)。 阅读更多
创建构造函数:
例如: -
A()
{
System.out.println("Class A");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.