weixin_46852977
「已注销」
采纳率53.8%
2020-04-21 09:16 阅读 550
已采纳

定义一个类Person,在该类中定义方法work(); (2)定义类Doctor,继承自Person,覆盖父类的work()方法,在该方法中输出“我是医生”;........

编写一个Java程序,要求:
(1)定义一个类Person,在该类中定义方法work();
(2)定义类Doctor,继承自Person,覆盖父类的work()方法,在该方法中输出“我是医生”;
(3)定义类Teacher,继承自Person,覆盖父类的work()方法,在该方法中输出“我是教师”;
(4)定义主类Test,在主方法中创建Doctor和Teacher类的对象,测试work()方法。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2020-04-21 09:38

    问题解决的话,请点下采纳

    class Test {
        public static void main(String[] args) {
            Person p = new Doctor();
            p.work();
            p = new Teacher();
            p.work();
        }
    }
    class Person
    {
        public void work() {}
    }
    class Doctor extends Person
    {
        @Override
        public void work() { System.out.println("我是医生"); }
    }
    class Teacher extends Person
    {
        @Override
        public void work() { System.out.println("我是教师"); }
    }
    
    点赞 2 评论 复制链接分享
  • as_csdn 代码的灵魂是bug! 2020-04-21 09:37

    Person类:

    public class Person {
        public void work(){}
    }
    

    Doctor类:

    public class Doctor extends Person {
        public Doctor() {
            // TODO Auto-generated constructor stub
        }
        @Override
        public void work() {
            // TODO Auto-generated method stub
            super.work();
            System.out.println("我是医生");
        }
    }
    
    

    Teacher类:

    public class Teacher extends Person {
        public Teacher() {
            // TODO Auto-generated constructor stub
        }
        @Override
        public void work() {
            // TODO Auto-generated method stub
            super.work();
            System.out.println("我是教师");
        }
    }
    

    Test类:

    public class Test1 {
    
        public static void main(String[] args) {
            Doctor d = new Doctor();
            Teacher t = new Teacher();
            d.work();
            t.work();
        }
    
    }
    
    点赞 2 评论 复制链接分享
  • wmx499712891 Mengxi丶Wang 2020-04-21 09:37
    public class Test
    {
        public static void main(String[] args)
        {
            Doctor doctor = new Doctor();
            doctor.work();
    
            Teacher teacher = new Teacher();
            teacher.work();
    
        }
    }
    
    
    abstract class Person
    {
        public abstract void work();
    }
    
    class Doctor extends Person
    {
    
        @Override
        public void work()
        {
            System.out.println("我是医生");
        }
    }
    
    class Teacher extends Person
    {
    
        @Override
        public void work()
        {
            System.out.println("我是教师");
        }
    }
    
    
    点赞 1 评论 复制链接分享

相关推荐