gpt4 book ai didi

java - 如何将来自多个接口(interface)和父类的对象存储在一个数组中

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:12 24 4
gpt4 key购买 nike

我被作业问题困住了。

它要求我设计界面 Drawable、Rotatable、Resizable 和 Sounds。开发一个执行以下操作的程序。

一个 Animal 类,它将具有名称属性和名称的 setter/getter 方法。 Animal 可以发出声音,并且可以绘制、旋转和调整大小。具有名称和年龄属性以及名称和年龄的 setter/getter 方法的 Vehicle 类。车辆还可以发出声音,并且可以绘制、旋转和调整大小。创建存储在同一数组中的 2 辆汽车和 2 只动物的集合。

我已经创建了接口(interface):

interface Drawable
{
void drawObject();
}
interface Rotatable
{
void rotateObject();
}
interface Resizable
{
void resizeObject();
}
interface Sounds
{
void playSound();
}

父类(动物和车辆)和子类(动物的 Pandas 和老虎,车辆的本田和丰田)。

abstract class Animal implements Drawable, Rotatable, Resizable, Sounds
{
String name;
public abstract void setName();
public abstract String getName();
public abstract void drawObject();
public abstract void rotateObject();
public abstract void resizeObject();
public abstract void playSound();
}

class Panda extends Animal
{
public void setName()
{
System.out.println("Panda's name is Bao Bao");
}
public String getName()
{
return name;
}
public void drawObject()
{
System.out.println("Drawing a Panda");
}
public void rotateObject()
{
System.out.println("Rotating a Panda");
}
public void resizeObject()
{
System.out.println("Resizing a Panda");
}
public void playSound()
{
System.out.println("Panda sound");
}
}

class Tiger extends Animal
{
public void setName()
{
System.out.println("Tiger's name is Si Si");
}
public String getName()
{
return name;
}
public void drawObject()
{
System.out.println("Drawing a Tiger");
}
public void rotateObject()
{
System.out.println("Rotating a Tiger");
}
public void resizeObject()
{
System.out.println("Resizing a Tiger");
}
public void playSound()
{
System.out.println("Tiger sound");
}
}

abstract class Vehicle implements Drawable, Rotatable, Resizable, Sounds
{
String name;
int age;
public abstract void setName();
public abstract String getName();
public abstract void setAge();
public abstract int getAge();
public abstract void drawObject();
public abstract void rotateObject();
public abstract void resizeObject();
public abstract void playSound();
}

class Honda extends Vehicle
{
public void setName()
{
System.out.println("Honda's name is Civic");
}
public String getName()
{
return name;
}
public void setAge()
{
System.out.println("Honda is 10 years old");
}
public int getAge()
{
return age;
}
public void drawObject()
{
System.out.println("Drawing a Honda");
}
public void rotateObject()
{
System.out.println("Rotating a Honda");
}
public void resizeObject()
{
System.out.println("Resizing a Honda");
}
public void playSound()
{
System.out.println("Honda sound");
}
}

class Toyota extends Vehicle
{
public void setName()
{
System.out.println("Toyota's name is Camry");
}
public String getName()
{
return name;
}
public void setAge()
{
System.out.println("Honda is 1 year old");
}
public int getAge()
{
return age;
}
public void drawObject()
{
System.out.println("Drawing a Toyota");
}
public void rotateObject()
{
System.out.println("Rotating a Toyota");
}
public void resizeObject()
{
System.out.println("Resizing a Toyota");
}
public void playSound()
{
System.out.println("Toyota sound");
}
}

在我的主要方法中,我能够创建 2 个数组并循环遍历每个数组以显示输出。我的问题是如何将 2 个不同的类组合到一个数组中并运行循环。

import java.util.Arrays;

public class ManipulateAnimals
{
public static void main(String[] args)
{
Animal[] animal = {new Panda(), new Tiger()};
Vehicle[] vehicle = {new Honda(), new Toyota()};

for(int i = 0; i < animal.length; i++)
{
animal[i].setName();
animal[i].getName();
animal[i].drawObject();
animal[i].rotateObject();
animal[i].resizeObject();
animal[i].playSound();
System.out.println(" ");
}


for(int i = 0; i < vehicle.length; i++)
{
vehicle[i].setName();
vehicle[i].getName();
vehicle[i].setAge();
vehicle[i].getAge();
vehicle[i].drawObject();
vehicle[i].rotateObject();
vehicle[i].resizeObject();
vehicle[i].playSound();
System.out.println(" ");
}
}
}

最佳答案

通常这类问题是通过引入一个新的上层接口(interface)来解决的。

因此,您将拥有 Drawable extends EditableRotatable extends Editable 等等。

之后您将能够创建一个Editable 数组。

关于java - 如何将来自多个接口(interface)和父类的对象存储在一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58426480/

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