我有2个类-“学生”和“雇员”,这两个类都扩展了Person类。 这3个类中都有方法。 在我的演示类中,我必须为每个类(学生,雇员和人)创建2个对象,并将它们放入类型为Person的数组中。然后,我必须遍历数组,并根据对象是来自Student,Employee还是Person的方式,必须在此类/子类中调用一个方法。问题在于,一旦这些对象进入Person数组中,则仅可见Person类中的.method。如果我的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();
}
}
答案 0 :(得分:0)
在您的情况下,由于您已经在循环的每次迭代期间确认正在使用哪种类型的Person
,因此可以将Person
强制转换为Employee
或Student
根据需要:
if (array[i].getClass().equals(iskra.getClass())) {
((Student)array[i]).showStudentInfo();
}
但是,更好的主意是遵循更标准的面向对象编程模型。由于从Person
扩展的所有类都将具有一种显示信息的方法,因此您应该在Person
类中声明该方法,并让其子级覆盖它。
通过在showInfo()
类中声明一个Person
方法,可以确保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
}
学生班:
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
类以仅在每个showInfo()
上调用Person
方法,而不必先专门检查Person
的类型:
for (i = 0; i < array.length; i++) {
array[i].showInfo();
}