gpt4 book ai didi

Java——继承和多态

转载 作者:行者123 更新时间:2023-12-02 10:13:34 24 4
gpt4 key购买 nike

说明如下:创建一个包含 main 方法的 Main 类。实现main 方法中包含以下内容:

– 使用重载构造函数从常规类创建对象

– 测试所有实例方法(即 setter、getter、重载、覆盖)

——分配给子对象的父引用

——传递多态对象的方法调用

我被困在我的家长类(class),即雨林中。接下来我应该做什么?

雨林类

//INHERITANCE                                                      
package com.ancient;
public class Rainforest
{
public static void main(String[] args)
}

Rainforest Class - Inherits Parent and Child

哺乳动物类(父类)

//PARENT CLASS                                                     
package com.ancient;
public class Mammal
{
//INSTANCE VARIABLES
private String type;
private double speed;

//CONSTRUCTOR
public Mammal(String type, double speed)
{
this.type = type;
this.speed = speed;
}

//SETTERS
public void setType(String type)
{
this.type = type;
}

public void setSpeed(double speed)
{
this.speed = speed;
}

//GETTERS
public String getType()
{
return type;
}

public double getSpeed()
{
return speed;
}

//OVERRIDDEN
public void hunt(int Food)//int Food is the quantity - how many does the eagle he hunts?
{
System.out.println("The Bald eagle preys " + Food + "animals within a day to survive.");
}

}

Mammal Class - Part 1 Mammal Class - Part 2

鸟(子类)

package com.ancient;                                                
public class Bird extends Mammal
{
public Bird(String type, double speed)
{
super(type, speed);
}

//OVERRIDDING
public void hunt(int Food)//int Food is the quantity - how many does the eagle he hunts?
{
System.out.println("The Bald eagle will prey " + Food + "animals within a day to survive.");
}
}

Bird - Child Class

我的项目内容 Project content > src > package (library) > regular (2) and main classes (1)

现在,这里的问题是:我的构造函数未定义。我应该制作一个新包并向其中插入 Rainforest 类吗?或者我的哺乳动物和鸟类类中的对象有问题?

最佳答案

您的 Mammal 类中有一个接受参数的构造函数,但您尝试在不传递参数的情况下实例化对象。

关于Java——继承和多态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54824842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com