gpt4 book ai didi

java - 将子类中的对象放入父类(super class)类型的数组后如何调用子类方法?

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:44 27 4
gpt4 key购买 nike

我有 2 个类 - “Student”和“Employee”,它们都扩展了 Person 类。所有 3 个类都有内部方法。在我的演示类中,我必须从每个类(学生、员工和人员)创建 2 个对象,并将它们放入 Person 类型的数组中。然后我必须遍历数组,并根据对象是否来自学生、员工或人员,我必须调用此类/子类中的方法。问题是,一旦这些对象进入 Person 数组,只有 Person 类中的 .methods 可见。如果我的 array[i]."method"来自学生或员工(array[i].showStudentInfo() 和 array[i].showEmplyeeInfo()),如何才能找到它预先感谢您!

public class Person {
String name;
int age;
boolean isMan;

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

void showPersonInfo() {
System.out.println("Име: " + this.name + " | " + "години: " + this.age + " | " + "мъж ли е: " + this.isMan);
}
}

public class Student extends Person {
double score;

Student(String name, int age, boolean isMan, double score) {
super(name, age, isMan);
this.score = score;
}
public void showStudentInfo() {
System.out.println("Име: " + super.name + " | " + "години: " + super.age + " | " + "мъж ли е: " + " | "
+ super.isMan + " | " + "Оценка: " + this.score);
}
}

public class Employee extends Person {
double daySallary;
double extraSum;
Employee(String name, int age, boolean isMan, double daySallary){
super(name, age, isMan);
this.daySallary=daySallary;
}
double calculateOvertime(double hours) {
if (this.age< 18)
extraSum = 0;
else
extraSum = (this.daySallary / 8) * hours * 1.5;
return extraSum;
}
public void showEmployeeInfo() {
System.out.println("Име: " + super.name + " | " + "години: " + super.age + " | " + "мъж ли е: " + " | "
+ super.isMan + " | " + "Допълнителна сума от оставане след работно време: " + this.extraSum);
}
}

public class Demo {
public static void main(String[] args) {
Person ivan = new Person("Ivan Georgiev", 27, true);
Person nikola = new Person("Nikola Ivanov", 30, true);
Student iskra = new Student("Iskra Dimitrova", 21, false, 4.5);
Student georgi = new Student("Georgi Kazakov", 19, true, 5.5);
Employee neli = new Employee("Anelia Stoicheva", 35, false, 50);
Employee monika = new Employee("Monika Petrova", 42, false, 80);

Person[] array = new Person[10];
array[0] = ivan;
array[1] = nikola;
array[2] = iskra;
array[3] = georgi;
array[4] = neli;
array[5] = monika;


for (int i = 0; i < 6; i++) {
if (array[i].getClass().equals(ivan.getClass())) {
array[i].showPersonInfo();
}
if (array[i].getClass().equals(iskra.getClass())) {

array[i].showStudentInfo();
}
if (array[i].getClass().equals(neli.getClass())) {

array[i].showEmployeeInfo();
}
}

最佳答案

就您而言,由于您已经确认在循环的每次迭代期间正在处理的 Person 类型,因此您可以根据需要将 Person 简单地转换为 EmployeeStudent:

if (array[i].getClass().equals(iskra.getClass())) {
((Student)array[i]).showStudentInfo();
}
<小时/>

但是,更好的想法是遵循更标准的面向对象编程模型。由于从 Person 扩展的所有类都有一个显示信息的方法,因此您应该在 Person 类中声明该方法并让子级重写它。

通过在 Person 类中声明 showInfo() 方法,您可以确保您的 for 循环可以访问该方法,无论您使用的是哪种类型的 Person

<小时/>

人员类别:

public class Person {

String name;
int age;
boolean isMan;

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

public void showInfo() {
System.out.println("Име: " + this.name + " | " + "години: " + this.age + " | " + "мъж ли е: " + this.isMan);
}

// SETTERS and GETTERS
}

学生类(class):

public class Student extends Person {

double score;

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

@Override
public void showInfo() {
System.out.println("Име: " + super.name + " | " + "години: " + super.age + " | " + "мъж ли е: " + " | "
+ super.isMan + " | " + "Оценка: " + this.score);
}

// SETTERS and GETTERS
}

员工类别:

public class Employee extends Person {

double daySallary;
double extraSum;

Employee(String name, int age, boolean isMan, double daySallary) {
super(name, age, isMan);
this.daySallary = daySallary;
}

double calculateOvertime(double hours) {
if (this.age < 18)
extraSum = 0;
else
extraSum = (this.daySallary / 8) * hours * 1.5;
return extraSum;
}

@Override
public void showInfo() {
System.out.println("Име: " + super.name + " | " + "години: " + super.age + " | " + "мъж ли е: " + " | "
+ super.isMan + " | " + "Допълнителна сума от оставане след работно време: " + this.extraSum);
}
}
<小时/>

从那里,您可以更新您的 Demo 类,以简单地调用每个 Person 上的 showInfo() 方法,而不必首先专门检查 Person 的类型:

for (i = 0; i < array.length; i++) {
array[i].showInfo();
}

关于java - 将子类中的对象放入父类(super class)类型的数组后如何调用子类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55079150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com