m0_59605943 2022-01-21 17:51 采纳率: 83.3%
浏览 16

我使用强制转换数据类型,要调用Person,Student,Teacher各自的特有方法(putong(),study(),teach()),为什么结果都是调用putong()方法,怎么解决啊

public class Person {
private String name;
private int age;


```java


    public Person(String name, int age) {
        this.setName(name);
        this.setAge(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 "我叫"+getName()+",今年"+getAge()+"岁了";
    }
    public void putong(){
        System.out.println("我是一个普通人");
    }
}
public class Student extends Person{
    private double grade;

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

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }
    public String say(){
        return "我叫"+getName()+",今年"+getAge()+"岁了,"+"分数"+getGrade();
    }
    public void study(){
        System.out.println("我正在学习");

    }
}
public class Teacher extends Person{
    private double salary;

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String say(){
        return "我叫"+getName()+",今年"+getAge()+"岁了,"+"薪水"+getSalary();
    }
    public void teach(){
        System.out.println("我正在教学");
    }
}
import java.util.Arrays;

public class master {
    public static void main(String[]args){
        Person[] persons=new Person[3];
        persons[0]=new Person("小明",16);
        persons[1]=new Student("小米",18,100);
        persons[2]=new Teacher("史塔克",50,12000);
//        System.out.println(Arrays.toString(persons));
        for (int i=0;i< persons.length;i++){
           System.out.println(persons[i].say());
           if (persons[i] instanceof Person){
               Person person=(Person) persons[i];
               person.putong();
           }else if (persons[i] instanceof Student){
               Student student=(Student) persons[i];
               student.study();
           }else if (persons[i] instanceof Teacher){
               Teacher teacher=(Teacher) persons[i];
               teacher.teach();
           }
           else {
               System.out.println("对象不存在");
           }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • [PE]经典八炮 2022-01-21 20:10
    关注

    img

    先判断persons[i] instanceof Person,因为Student Teacher都是它的子类,所以都是true,一直是putong()

    评论

报告相同问题?

问题事件

  • 创建了问题 1月21日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀