Java多态代码“找不到构造函数”
我试图向自己解释 java 多态性,所以我简单地创建了一个项目,显示 Family
是 SuperClass
和 SubClasses
Brothers Sisters`
问题是,当我编译时,我收到一条错误消息: 找不到构造函数姐妹
找不到构造函数兄弟
有人能给我解释一下吗?
谢谢你们。
class Family {
private String name,age;
public Family(String name,String age){
this.name = name;
this.age = age;
}
public String toString(){
return "name : " + name + "\tage " + age ;
}
}
class Brothers extends Family{
public Brothers(String name, String age){
super(name,age);
}
}
class Sisters extends Family{
public Sisters(String name, String age){
super(name,age);
}
}
class FamilyTest{
public static void main(String[] args){
Family[] Member= new Family[3];
Member[1] = new Sisters("LALA",22);
Member[2] = new Brothers("Mike",18);
}
}
I'm trying to explain java Polymorphism to my self so I've simply created a project showing that Family
is the SuperClass
and SubClasses are
BrothersSisters`
The thing is when I compile I receive an error saying thatCannot find the Constructor Sisters
Cannot find the Constructor Brothers
Could someone explain to me?
Thanks guys.
class Family {
private String name,age;
public Family(String name,String age){
this.name = name;
this.age = age;
}
public String toString(){
return "name : " + name + "\tage " + age ;
}
}
class Brothers extends Family{
public Brothers(String name, String age){
super(name,age);
}
}
class Sisters extends Family{
public Sisters(String name, String age){
super(name,age);
}
}
class FamilyTest{
public static void main(String[] args){
Family[] Member= new Family[3];
Member[1] = new Sisters("LALA",22);
Member[2] = new Brothers("Mike",18);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已将
age
定义为 String,但向其传递了一个整数。应该可以,但我建议您将
age
从 String 更改为 int。You have
age
definded as String but you pass an integer to it.should work but I would advice you to change
age
from String to int.将 main() 替换为以下代码,
错误是:
sisters
和brothers
的构造函数的参数是 String,但您将age
传递为一个整数。建议:你可以将age的类型改为
int
,这样更正确。Replace the main() with this code,
The error was : arguments for the constructors of
sisters
andbrothers
were String, but you passedage
as anInteger
.Sugggestion : you may change the type of age to
int
, which is more correct.请注意,这只是 Java 中可以使用的多态类型之一,其他类型还有泛型和函数重载
Please note that this is just one of the types of polymorphism you can use in Java, others are
Generics
andFunction overloading