【问题标题】:Java Beginner Help Virtual Methods [closed]Java初学者帮助虚拟方法[关闭]
【发布时间】:2013-05-20 18:10:48
【问题描述】:

我是 OOP 课程的学生,这是我第一次真正使用 Java 进行编码,我的脑袋有点晕。

我的项目不完整,只是因为我似乎无法找到我的导师在寻找什么,所以希望你们能给我一些正确的方向。

我感到困惑的作业点是这个语句。

使用虚拟方法 displayInfo() 创建一个名为“Animal”的类。

以下是我当前的代码,我使用的是 NetBeans 7.3

public class AnimalInfo {
    public class Animal{}
    public class Cow extends Animal{}
    public class Lion extends Animal{}
    public class Human extends Animal{}


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Please select an animal for a brief description of each:\n\nEnter 'A' for Cow.\nEnter 'B' for Lion.\nEnter 'C' for Human.\n\nEnter 'X' to Exit the application.");
        // TODO code application logic here
    }
}

为了符合displayInfo(),我需要对公共类 Animal 的实现进行哪些更改?

另外,JOptionPane 是允许用户输入的唯一方法吗?因为我需要让用户的选择拉出所选动物的信息(尚未在上面的代码中实现),所以我看不到任何允许我接受用户输入以存储为字符串的内容。

再次,我们将不胜感激任何帮助。提前致谢!

【问题讨论】:

  • 向我们表明您已尝试完成任务
  • 如果我理解了任务,如果你来这里是为了激怒我或试图放下不理解的人,我会继续前进,不需要你的意见。谢谢^.^

标签: java netbeans methods virtual


【解决方案1】:

这是一个可以帮助你的例子

public abstract class Animal {

    public void displayInfo() {
        System.out.println("Im animal");
    }

}

public class Cow extends Animal {
    @Override
    public void displayInfo() {
        System.out.println("I am a Cow");
    }

}

public class Tiger extends Animal {
    @Override
    public void displayInfo() {
        System.out.println("I am a Tiger");
    }

}

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Animal animal = new Tiger();
        animal.displayInfo();

    }

}

输出:

 I am a Tiger

【讨论】:

  • 哦,我想我现在明白 displayInfo 是什么意思了,谢谢!我现在感觉有点傻。
  • 然后点赞并接受作为答案。
  • 无法竖起大拇指,我已经尝试过了,显然我需要 15 次代表才能 +1
  • ^ 没问题,谢谢。
猜你喜欢
  • 2017-09-25
  • 1970-01-01
  • 2018-02-17
  • 2014-06-06
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多