为什么这个程序显示错误?类a中的构造函数a不能应用于给定类型 [英] Why this program showing an error? Constructor a in class a cannot be applied to given types
本文介绍了为什么这个程序显示错误?类a中的构造函数a不能应用于给定类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class A
{
int i,j;
A(int i,int j)
{
this.i=i;
this.j=j;
}
void show()
{
System.out.println("i="+i+" "+"j="+j);
}
}
class B extends A
{
int k;
B(int k)
{
this.k=k;
}
void showsub()
{
System.out.println("i+j+k= "+(i+j+k));
}
}
class inherit
{
public static void main(String args[])
{
A obj1=new A(3,5);
B obj2=new B(7);
obj1.show();
obj2.showsub();
}
}
我的尝试:
我试过这段代码,但是给出了编译时错误。
What I have tried:
I have tried this code but giving compile time error.
推荐答案
B类隐含地调用super()
但是A类中没有定义无参数构造函数。
您可以:
- 调用例如,
super(0,0);
在B
类构造函数
The B class implicitely callssuper()
however no argument-less constructor is defined in the A class.
You might either:
- call, for instance,
super(0,0);
insideB
class constructor
- 在
A
类中定义无参数构造函数
- define a argument-less constructor in the
A
class
这篇关于为什么这个程序显示错误?类a中的构造函数a不能应用于给定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文