class ttt {
public static void main(String[] atgs) {
method(new Teacher1());
method(new Teacher2());
method(new Student1());
method(new Stutden2());
}
public static void method(Person p) {
if (p instanceof Teacher1) {
Teacher1 a = (Teacher1)p;
a.setName("张思宁");
a.setAge(33);
System.out.println(a.getName() + "..." + a.getAge());
a.teach();
}else if (p instanceof Teacher2) {
Teacher2 b = (Teacher2)p;
b.setName("刘海可");
b.setAge(34);
System.out.println(b.getName() + "..." + b.getAge());
b.teach();
}else if (p instanceof Student1) {
Student1 c = (Student1)p;
c.setName("冷拉菲");
c.setAge(25);
System.out.println(c.getName() + "..." + c.getAge());
c.study();
}else if (p instanceof Student2) {
Student2 d = (Student2)p;
d.setName("坎蒂丝");
d.setAge(26);
System.out.println(d.getName() + "..." + d.getAge());
d.study();
}else {
return;
}
}
}
class Person {
private String name ;
private int age ;
public Person(){}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
class Teacher1 extends Person {
public Teacher1(){}
public Teacher1(String name,int age) {
super(name,age);
}
public void teach() {
System.out.println("教语文");
}
}
class Teacher2 extends Person {
public Teacher2(){}
public Teacher2(String name,int age) {
super(name,age);
}
public void teach() {
System.out.println("教数学");
}
}
class Student1 extends Person {
public Student1(){}
public Student1(String name,int age) {
super(name,age);
}
public void study() {
System.out.println("学语文");
}
}
class Student2 extends Person {
public Student2(){}
public Student2(String name,int age) {
super(name,age);
}
public void study() {
System.out.println("学数学");
}
} |
|