Java的新手,试图解决OldMacdonald的练习的主要部分Had Farm歌曲

问题描述

我正在尝试使用多态性对歌曲进行编码,但是我对Java编码非常陌生,并且具有非常基础的经验。我可以在类上使用一些帮助,但是我最大的问题是我不知道如何编写代码的主要部分,如果有人可以帮助我,我将非常感激。

这是我到目前为止的

public class OldMacdonald {
public interface Farm{
    public String getName();
    public String getNoise();
}

class Dog implements Farm{
    String name;
    String noise;
    
    public Dog(String name,String noise) {
        name=name;
        noise=noise;
    }
    
    public String getName() {
        return name;
    }
    
    public String getNoise() {
        return noise;
    }
}

class Cat implements Farm{
    String name;
    String noise;
    
    public Cat(String name,String noise) {
        name=name;
        noise=noise;
    }
    
    public String getName() {
        return name;
    }
    
    public String getNoise() {
        return noise;
    }
}
    
class Duck implements Farm{
    String name;
    String noise;
        
    public Duck(String name,String noise) {
        name=name;
        noise=noise;
    }
        
    public String getName() {
        return name;
    }
        
    public String getNoise() {
        return noise;
    }

}
    
class Cow implements Farm{
    String name;
    String noise;
        
    public Cow(String name,String noise) {
        name=name;
        noise=noise;
    }
        
    public String getName() {
        return name;
    }
        
    public String getNoise() {
        return noise;
    }

}
    
class Pig implements Farm{
    String name;
    String noise;
        
    public Pig(String name,String noise) {
        name=name;
        noise=noise;
    }
        
    public String getName() {
        return name;
    }
        
    public String getNoise() {
        return noise;
    }

}
    
class Song{
    private Farm [] animal = new Farm[5];
        
    Song() {
        animal[0] = new Dog("dog","woof");
        animal[1] = new Cat("cat","meow");
        animal[2] = new Duck("duck","quack");
        animal[3] = new Cow("cow","moo");
        animal[4] = new Pig("pig","oink");
    }
        
    public void lyrics() {
        int i;
            
        for(i=0; i<animal.length; i++) {
            System.out.println("Old MacDonald had a farm,E I E I O,\r\n" + 
            "And on his farm he had a " + animal[i].getName() + ",E I E I O.\r\n" + 
            "With a " + animal[i].getNoise() + " " + animal[i].getNoise() + " here and a " + animal[i].getNoise() + " " + animal[i].getNoise() + " there,\r\n" + 
            "Here a " + animal[i].getNoise() + ",there a " + animal[i].getNoise() + ",evrywhere a " + animal[i].getNoise() + " " + animal[i].getNoise() + ".\r\n" + 
            "Old MacDonald had a farm,E I E I O.\r\n\r\n");
            }
        }
    }

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

}

解决方法

您需要在Java独立程序中以某种方式“启动”您的代码,这可以通过main方法完成:

_id

Song似乎是您的课程,它实际上开始了所有事情,因此您需要创建一个实例:

public static void main(String[] args)

歌词是通过lyrics()方法输出的,因此我们需要调用它:

public static void main(String[] args) {
   Song mySong = new Song();
}

您也将要调整Song()构造函数方法:

public static void main(String[] args) {
   Song mySong = new Song();
   mySong.lyrics();
}

您可能还想更新动物类中的构造函数,您正在为其分配参数(在params和private字段中对变量使用相同名称时很容易做到):

public Song() {
,

如果我对您的理解正确,那么在您的main()方法中,您只需要创建Song的实例并对其调用lyrics(),不是吗?这样会将动物填充到Farm数组中,并在打印歌词时调用适当的方法。

,

您的代码存在三个问题:

  1. 您不需要声明类OldMacdonald中的所有接口和类。
  2. 您尚未将任何参数值分配给实例变量,例如您需要使用this.name = name来将参数name的值分配给实例变量name
  3. 您需要实例化类Song以便在实例上调用方法lyrics

以下是包含这些注释的代码:

Farm.java:

public interface Farm {
    public String getName();

    public String getNoise();
}

OldMacdonald.java:

class Dog implements Farm {
    String name;
    String noise;

    public Dog(String name,String noise) {
        this.name = name;
        this.noise = noise;
    }

    public String getName() {
        return name;
    }

    public String getNoise() {
        return noise;
    }
}

class Cat implements Farm {
    String name;
    String noise;

    public Cat(String name,String noise) {
        this.name = name;
        this.noise = noise;
    }

    public String getName() {
        return name;
    }

    public String getNoise() {
        return noise;
    }
}

class Duck implements Farm {
    String name;
    String noise;

    public Duck(String name,String noise) {
        this.name = name;
        this.noise = noise;
    }

    public String getName() {
        return name;
    }

    public String getNoise() {
        return noise;
    }

}

class Cow implements Farm {
    String name;
    String noise;

    public Cow(String name,String noise) {
        this.name = name;
        this.noise = noise;
    }

    public String getName() {
        return name;
    }

    public String getNoise() {
        return noise;
    }

}

class Pig implements Farm {
    String name;
    String noise;

    public Pig(String name,String noise) {
        this.name = name;
        this.noise = noise;
    }

    public String getName() {
        return name;
    }

    public String getNoise() {
        return noise;
    }

}

class Song {
    private Farm[] animal = new Farm[5];

    Song() {
        animal[0] = new Dog("dog","woof");
        animal[1] = new Cat("cat","meow");
        animal[2] = new Duck("duck","quack");
        animal[3] = new Cow("cow","moo");
        animal[4] = new Pig("pig","oink");
    }

    public void lyrics() {
        int i;

        for (i = 0; i < animal.length; i++) {
            System.out.println("Old MacDonald had a farm,E I E I O,\r\n" + "And on his farm he had a "
                    + animal[i].getName() + ",E I E I O.\r\n" + "With a " + animal[i].getNoise() + " "
                    + animal[i].getNoise() + " here and a " + animal[i].getNoise() + " " + animal[i].getNoise()
                    + " there,\r\n" + "Here a " + animal[i].getNoise() + ",there a " + animal[i].getNoise()
                    + ",evrywhere a " + animal[i].getNoise() + " " + animal[i].getNoise() + ".\r\n"
                    + "Old MacDonald had a farm,E I E I O.\r\n\r\n");
        }
    }
}

public class OldMacdonald {
    public static void main(String[] args) {
        new Song().lyrics();
    }
}

输出:

Old MacDonald had a farm,And on his farm he had a dog,E I E I O.
With a woof woof here and a woof woof there,Here a woof,there a woof,evrywhere a woof woof.
Old MacDonald had a farm,E I E I O.


Old MacDonald had a farm,And on his farm he had a cat,E I E I O.
With a meow meow here and a meow meow there,Here a meow,there a meow,evrywhere a meow meow.
Old MacDonald had a farm,And on his farm he had a duck,E I E I O.
With a quack quack here and a quack quack there,Here a quack,there a quack,evrywhere a quack quack.
Old MacDonald had a farm,And on his farm he had a cow,E I E I O.
With a moo moo here and a moo moo there,Here a moo,there a moo,evrywhere a moo moo.
Old MacDonald had a farm,And on his farm he had a pig,E I E I O.
With a oink oink here and a oink oink there,Here a oink,there a oink,evrywhere a oink oink.
Old MacDonald had a farm,E I E I O.

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...