Java多态代码“找不到构造函数”

发布于 2024-12-09 04:16:08 字数 857 浏览 0 评论 0原文

我试图向自己解释 java 多态性,所以我简单地创建了一个项目,显示 FamilySuperClassSubClassesBrothers 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 areBrothersSisters`

The thing is when I compile I receive an error saying that
Cannot 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2024-12-16 04:16:08

您已将 age 定义为 String,但向其传递了一个整数。

Member[1] = new Sisters("LALA", "22");
Member[2] = new Brothers("Mike", "18");

应该可以,但我建议您将 age 从 String 更改为 int。

You have age definded as String but you pass an integer to it.

Member[1] = new Sisters("LALA", "22");
Member[2] = new Brothers("Mike", "18");

should work but I would advice you to change age from String to int.

記憶穿過時間隧道 2024-12-16 04:16:08
public static void main(String[] args)
{
Family[] Member= new Family[3];

Member[1] = new Sisters("LALA","22");
Member[2] = new Brothers("Mike","18");

}

将 main() 替换为以下代码,

错误是:sistersbrothers 的构造函数的参数是 String,但您将 age 传递为一个整数。

建议:你可以将age的类型改为int,这样更正确。

public static void main(String[] args)
{
Family[] Member= new Family[3];

Member[1] = new Sisters("LALA","22");
Member[2] = new Brothers("Mike","18");

}

Replace the main() with this code,

The error was : arguments for the constructors of sisters and brothers were String, but you passed age as an Integer .

Sugggestion : you may change the type of age to int, which is more correct.

美人骨 2024-12-16 04:16:08

请注意,这只是 Java 中可以使用的多态类型之一,其他类型还有泛型和函数重载

Please note that this is just one of the types of polymorphism you can use in Java, others are Generics and Function overloading

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文