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

New to Java, and trying to solve the main part of the exercise for the OldMacdonald Had a Farm song

我正在尝试使用多态性对歌曲进行编码,但我对 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

}

}

如果我没理解错的话,在您的 main() 方法中,您只需创建 Song 的实例并在其上调用 lyrics(),不是吗?这将用动物填充 Farm 数组,并在打印歌词时调用适当的方法。

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

public static void main(String[] args)

看起来 Song 是您的 class,它实际上启动了所有内容,因此您需要创建一个实例:

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

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

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

您还需要调整 Song() 构造方法:

public Song() {

您可能也想更新 animal classes 中的构造函数,您正在将参数分配给自身(在参数和私有字段中使用相同名称的变量时很容易完成):

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