使用接口、多态性、抽象将多个对象类型转换为数组、Java
基本任务: 一个名为draw()的方法将用于绘制对象 名为rotate()的方法将用于旋转对象,该方法称为 resizeObject()将用于调整对象的大小,以及播放声音() 方法将用于播放声音使用接口、多态性、抽象将多个对象类型转换为数组、Java,java,interface,polymorphism,abstract,Java,Interface,Polymorphism,Abstract,基本任务: 一个名为draw()的方法将用于绘制对象 名为rotate()的方法将用于旋转对象,该方法称为 resizeObject()将用于调整对象的大小,以及播放声音() 方法将用于播放声音 设计可绘制、可旋转、可调整大小和声音的接口类 开发一个名为“操纵动物”的程序,该程序可执行以下操作:将使用动物类对动物的一些基本特征进行建模 动物的应用。动物将具有名称属性 和方法来设置名称并获取名称。动物可以制造食物 声音和声音可绘制、旋转和调整大小 车辆类别将用于模拟车辆的一些基本特性 车辆。车辆将
public class Main {
public interface Drawable{
void draw();
}
public interface Rotatable{
void rotate();
}
public interface Resizable{
void resize();
}
public interface Sounds{
void sounds();
}
public static class Animal implements Drawable, Rotatable, Resizable, Sounds{
String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("Drawing an Animal");
}
@Override
public void rotate() {
System.out.println("Rotating an Animal");
}
@Override
public void resize() {
System.out.println("Resizing an Animal");
}
@Override
public void sounds() {
System.out.println("Animal Sounds");
}
}
public static class Vehicle implements Drawable, Rotatable, Resizable, Sounds{
String name;
int age;
public Vehicle(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void draw() {
System.out.println("Drawing a Vehicle");
}
@Override
public void rotate() {
System.out.println("Rotating a Vehicle");
}
@Override
public void resize() {
System.out.println("Resizing a Vehicle");
}
@Override
public void sounds() {
System.out.println("Vehicle Sounds");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public static void main(String[] args) {
Animal animal1 = new Animal("Rex");
Animal animal2 = new Animal("Dharma");
Vehicle vehicle1 = new Vehicle("Maia", 11);
Vehicle vehicle2 = new Vehicle("Lilith", 3);
/*animal1.draw();
animal1.rotate();
animal1.resize();
animal1.sounds();
what I need to do for all the things*/
Array things[] = {animal1, animal2, vehicle1, vehicle2};
for(int i = 0; i < things.length; i++ ){
things[i].draw();
things[i].rotate();
things[i].resize;
things[i].sounds;
}
}
}
公共类主{
公共接口可绘制{
无效抽取();
}
可旋转的公共接口{
void rotate();
}
可调整大小的公共接口{
void resize();
}
公共界面声音{
虚无的声音();
}
公共静态类动物实现可绘制、可旋转、可调整大小的声音{
字符串名;
公共动物(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@凌驾
公众抽签(){
System.out.println(“绘制动物”);
}
@凌驾
公共空间(英文){
System.out.println(“旋转动物”);
}
@凌驾
public void resize(){
System.out.println(“调整动物大小”);
}
@凌驾
公共图书馆{
System.out.println(“动物声音”);
}
}
公共静态类车辆实现了可拉伸、可旋转、可调整大小的声音{
字符串名;
智力年龄;
公共车辆(字符串名称,整数){
this.name=名称;
这个。年龄=年龄;
}
@凌驾
公众抽签(){
System.out.println(“绘制车辆”);
}
@凌驾
公共空间(英文){
System.out.println(“旋转车辆”);
}
@凌驾
public void resize(){
System.out.println(“调整车辆大小”);
}
@凌驾
公共图书馆{
系统输出打印项次(“车辆声音”);
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
}
公共静态void main(字符串[]args){
动物1=新动物(“Rex”);
动物2=新动物(“法”);
车辆1=新车(“Maia”,11);
车辆2=新车(“Lilith”,3);
/*动物1.画();
动物1.旋转();
动物1.调整大小();
动物1.声音();
我需要为所有的事情做些什么*/
数组事物[]={animal1,animal2,vehicle1,vehicle2};
for(int i=0;i
我不知道您为什么要在这里处理抽象类
这里唯一的问题是它的命名。如何命名一个包括可绘制、可旋转、可调整大小、声音的概念并不明显
这是你应该考虑的
这里只是为了举例说明,让我们将抽象命名为AbstractActions
因此,可以使用它的计算机将如下所示:
abstract class AbstractActions implements Drawable, Rotatable, Resizable, Sounds{}
class Vehicle extends AbstractActions {
// Skip for brevity
}
class Animal extends AbstractActions {
// Skip for brevity
}
还有后记,您可以在抽象操作
中进行迭代
另外,请注意,如果抽象类中没有实现,最好将其更改为聚合其他接口的接口。请阅读:并且。我不希望有人编写我的代码。我提供了信息。我将非常高兴能够朝着正确的方向前进。事实上,我正在为我的教育付费。我会的我不太可能按时上交,同样是因为一些原因。但我很确定这些主题会出现在考试中。我不会在任何地方的网络上发布这些主题。我保证,我出生在你父母结婚之前。别担心。这些都不是我链接到这些文章的原因。你的问题似乎遗漏了一个特定的程序撞击问题。你已经给出了你的作业描述和当前代码,但在这个过程中,你忽略了问一个问题,并且对你所坚持的内容含糊不清。非常感谢你!我今晚回家后会尝试的!我交的不完整,但必须让它为我自己的a型心灵平静工作!@bubblyBibliophile如果这个答案对您有效,请随意接受itabstract类AbstractActions实现D..、R..、R..、S..{}类动物扩展AbstractActions{}公共静态void main(String[]args){Animal animal1=new Animal();AbstractActions things[]={animal1};for…;}所以,我正在构造新的抽象动作或