public class PersonTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person[] p = new Person[2];
p[0] = new Employer("Aric", 85000.88);
p[1] = new Students("Elise", "Computer");
for (Person e : p) {
System.out.println(e.getName() + e.getDescription());
}
}
}
abstract class Person {
public Person(String name) {
this.name = name;
}
public abstract String getDescription();
public String getName() {
return name;
}
private String name;
}
class Employer extends Person {
public Employer(String name, double salary) {
super(name);
this.salary = salary;
}
public String getDescription() {
return String.format("an employee with a salary of $%.2f", salary);
}
private double salary;
}
class Students extends Person {
public Students(String name, String major) {
super(name);
this.major = major;
}
public String getDescription() {
return "a student major in " + this.major;
}
private String major;
}
Java抽象类与集成实例
最新推荐文章于 2021-10-26 15:06:59 发布