面向对象练习题

创建四个类:Person类、Student类、Teacher类和Test类

Student类、Teacher类继承Person类

public class Person {
String name;
int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public String say(){
        return "名字为:"+name+", 年龄为:"+age;
    }
}

public class Student extends  Person{
  double score;

    public Student(String name, int age, double score) {
        super(name, age);
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
    public String say(){
        return  "学生"+super.say()+", 成绩为"+score;
    }
    //学生私有方法
    public  void study(){
        System.out.println("学生"+getName()+"正在学习Java~");
    }
}
public class Teacher extends Person{
double salary;

    public Teacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String say(){
        return  "老师"+super.say()+", 工资为"+salary;
    }
    public  void teach(){
        System.out.println("老师"+getName()+"正在教Java~");
    }
}
public class Test {
    public static void main(String[] args) {
        Person[] people=new  Person[5];
        people[0]=new Person("Tom",38);
        people[1]=new Student("lxl",18,98);
        people[2]=new Student("lky",21,78);
        people[3]=new Teacher("Tm",32,5000);
        people[4]=new Teacher("lll",48,2000);
        for (int i = 0; i <people.length ; i++) {
            System.out.println(people[i].say());
            if (people[i] instanceof Student) {//判断是否为Student类,否则不能访问study方法
                //如果不是,强制转换,向下转型
                Student student = (Student) people[i];
                student.study();
                //也可以使用一条语句代替
//                ((Student)people[i]).study();
            } else if (people[i] instanceof Teacher) {//判断是否为Student类,否则不能访问study方法
                //如果不是,强制转换,向下转型
                Teacher teacher = (Teacher) people[i];
                teacher.teach();
            } else if (people[i] instanceof Person) {
            }else {
                System.out.println("你输入的类型有错误");
            }
        }
    }
}

相关推荐

  1. 【C++】C++面向对象练习题

    2024-07-20 17:54:02       33 阅读
  2. python面向对象练习

    2024-07-20 17:54:02       37 阅读
  3. python面向对象练习

    2024-07-20 17:54:02       38 阅读
  4. 面向对象进阶基础练习

    2024-07-20 17:54:02       19 阅读
  5. 面向对象——类与对象

    2024-07-20 17:54:02       33 阅读
  6. 面向对象面向过程

    2024-07-20 17:54:02       44 阅读
  7. 面向过程和面向对象

    2024-07-20 17:54:02       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 17:54:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 17:54:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 17:54:02       45 阅读
  4. Python语言-面向对象

    2024-07-20 17:54:02       55 阅读

热门阅读

  1. WPF中UI元素继承关系

    2024-07-20 17:54:02       21 阅读
  2. Linux复习01

    2024-07-20 17:54:02       16 阅读
  3. 算法刷题笔记 八数码(C++实现)

    2024-07-20 17:54:02       20 阅读
  4. Apollo开发指南

    2024-07-20 17:54:02       18 阅读
  5. Day05 Redis 面试题 下

    2024-07-20 17:54:02       18 阅读
  6. 【鸿蒙学习笔记】UI・页面路由 (@ohos.router)

    2024-07-20 17:54:02       19 阅读
  7. 《设计模式之美》学习笔记1

    2024-07-20 17:54:02       17 阅读
  8. WebKit 引擎:Web 组件的崛起与支持

    2024-07-20 17:54:02       20 阅读
  9. Python函数传参

    2024-07-20 17:54:02       17 阅读
  10. 带答案和解题步骤的数独题目分享

    2024-07-20 17:54:02       20 阅读
  11. 关于mysql架构的思考

    2024-07-20 17:54:02       17 阅读