编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E。

2024-11-11 18:39:45
推荐回答(2个)
回答1:

    public class Program
    {
        static void Main(string[] args)
        {
            Monkey monkey = new Monkey();
            monkey.Speak();

         脊含   People people = new People();
            people.Speak();
            people.Think();
        }

        public class Monkey
        {
            public Monkey(string s)
            {
                
            }

            public Monkey()
            {

            }

            public virtual void Speak()
            {
                Console.WriteLine("咿咿呀呀......");
            樱带笑}
        }

        public class People:Monkey
        {
            public override void Speak()
            行缓{
                Console.WriteLine("小样,不错嘛,会说话了!");
            }

            public void Think()
            {
                Console.WriteLine("别说话,认真思考!");
            }
        }
    }

回答2:

  public class Test {
  public static void main(String[] args) {
  知枝稿People p=new People("1");
  p.speak();
  p.think();
  Monkey m=new Monkey("2");
  //该命名方式会导致m.speak()调用People的speak方法
  //Monkey m=new People("2");
  m.speak();
  }
  }
  class Monkey{
  public Monkey (String s){
  System.out.println(s);
  }
  public void speak() {
  System.out.println("咿搭宽咿呀呀......");
  }
  }
  class People extends Monkey{
  搭孝public People(String s) {
  super(s);
  }
  @Override
  public void speak() {
  System.out.println("小样的,不错嘛!会说话了!");
  }
  public void think(){
  System.out.println("别说话!认真思考!");
  }

}