刚接触Java,试图解决《老麦当劳有个农场》歌曲的主要部分练习。

huangapple go评论77阅读模式
英文:

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) {
            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() + ", everywhere 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
        OldMacdonald.Song song = new OldMacdonald().new Song();
        song.lyrics();
    }
}

注意:上面的翻译可能在技术术语方面存在一些差异,但尽可能保持了原始代码的结构和逻辑。

英文:

I am trying to code a song using polymorphism, but I am quite new to Java coding and have a really basic experience. I could use some help with the classes, but my biggest problem is that I do not know how to write the main part of the code, I'd be very grateful if someone could help me with it.

Here is what I have so far

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(&quot;dog&quot;, &quot;woof&quot;);
animal[1] = new Cat(&quot;cat&quot;, &quot;meow&quot;);
animal[2] = new Duck(&quot;duck&quot;, &quot;quack&quot;);
animal[3] = new Cow(&quot;cow&quot;, &quot;moo&quot;);
animal[4] = new Pig(&quot;pig&quot;, &quot;oink&quot;);
}
public void lyrics() {
int i;
for(i=0; i&lt;animal.length; i++) {
System.out.println(&quot;Old MacDonald had a farm, E I E I O,\r\n&quot; + 
&quot;And on his farm he had a &quot; + animal[i].getName() + &quot;, E I E I O.\r\n&quot; + 
&quot;With a &quot; + animal[i].getNoise() + &quot; &quot; + animal[i].getNoise() + &quot; here and a &quot; + animal[i].getNoise() + &quot; &quot; + animal[i].getNoise() + &quot; there,\r\n&quot; + 
&quot;Here a &quot; + animal[i].getNoise() + &quot;, there a &quot; + animal[i].getNoise() + &quot;, evrywhere a &quot; + animal[i].getNoise() + &quot; &quot; + animal[i].getNoise() + &quot;.\r\n&quot; + 
&quot;Old MacDonald had a farm, E I E I O.\r\n\r\n&quot;);
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}

}

答案1

得分: 2

你需要以某种方式"启动"你的代码,在一个类似这样的Java独立程序中,这可以通过主方法来完成:

public static void main(String[] args)

看起来Song是你的类,实际上它启动了一切,所以你需要创建一个该类的实例:

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() {

你可能还想要更新动物类的构造方法,之前你把参数赋值给了自己(当参数和私有字段的变量名相同时容易出现这种情况):

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

You need to "launch" your code somehow, in a Java standalone program like this that would be done from the main method:

public static void main(String[] args)

It looks like Song is your class that actually starts everything off so you need to create an instance of that:

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

The lyrics are output by the lyrics() method, so we need to call that:

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

You'll want to tweak your Song() constructor method too:

public Song() {

You might want to update your constructors in animal classes too, you were assigning the parameter to itself (easily done when using same names for variables in params and private fields):

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

答案2

得分: 1

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

英文:

If I understood you correctly, in your main()method you just have to create an instance of Song and call lyrics() on it, no? That will populate the Farm array with the animals and call the appropriate methods when printing the lyrics.

huangapple
  • 本文由 发表于 2020年8月28日 17:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63631498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定