【java】程序修改 Test类在实例化SubClass的一个对象时,会出现错误,请尝试改正这个错误。
classSuperClass{publicSuperClass(Stringmsg){System.out.println("SuperClassConstructor...
class SuperClass
{
public SuperClass(String msg)
{
System.out.println("SuperClass Constructor"+msg);
}
}
class Subclass extends SuperClass
{
public Subclass()
{
System.out.println("Subclass Constructor");
}
}
public class Test
{
public static void main (String[] args)
{
Subclass subclass=new Subclass();
}
} 展开
{
public SuperClass(String msg)
{
System.out.println("SuperClass Constructor"+msg);
}
}
class Subclass extends SuperClass
{
public Subclass()
{
System.out.println("Subclass Constructor");
}
}
public class Test
{
public static void main (String[] args)
{
Subclass subclass=new Subclass();
}
} 展开
2个回答
展开全部
请给父类添加无参构造器,或在子类的构造方法中显式调用父类的那个带参数的构造器,如下所示:
class SuperClass {
//public SuperClass() {
//System.out.println("这里是无参构造器,当定义了有参构造器时,需要显式定义");
//}
public SuperClass(String msg) {
System.out.println("SuperClass Constructor" + msg);
}
}
class Subclass extends SuperClass {
public Subclass() {
super("显式调用父类有参构造器");
System.out.println("Subclass Constructor");
}
}
public class Test {
public static void main(String[] args) {
Subclass subclass = new Subclass();
}
}
class SuperClass {
//public SuperClass() {
//System.out.println("这里是无参构造器,当定义了有参构造器时,需要显式定义");
//}
public SuperClass(String msg) {
System.out.println("SuperClass Constructor" + msg);
}
}
class Subclass extends SuperClass {
public Subclass() {
super("显式调用父类有参构造器");
System.out.println("Subclass Constructor");
}
}
public class Test {
public static void main(String[] args) {
Subclass subclass = new Subclass();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询