日期:2014-05-20 浏览次数:20825 次
public class ExtendsDemo {
public static void main(String[] args){
Student s = new Student("张三",23);
Teacher t = new Teacher("李四",35);
Print(s);
Print(t);
}
private static void Print(Person p){
if(p instanceof Student){
System.out.print(p.name+" "+p.age+" "+p.eat()+" ");
p.learn();
}
else if(p instanceof Teacher){
System.out.print(p.name+" "+p.age+" "+p.eat()+" ");
p.teach();
}
else{ System.out.println("error");}
}
}
class Person{
public String name;
public int age;
public String eat(){
return "吃饭";
}
}
class Student extends Person{
Student(String name,int age){
this.name = name;
this.age = age;
}
public void learn(){
System.out.println("学习");
}
}
class Teacher extends Person{
Teacher(String name,int age){
this.name = name;
this.age = age;
}
public void teach(){
System.out.println("教课");
}
}
public class ExtendsDemo {
public static void main(String[] args){
Student s = new Student("张三",23);
Teacher t = new Teacher("李四",35);
Print(s);
Print(t);
}
private static void Print(Person p){
if(p instanceof Student){
System.out.print(p.name+" "+p.age+" "+p.eat()+" ");
Student student =(Student) p;
student.learn();
}
else if(p instanceof Teacher){
System.out.print(p.name+" "+p.age+" "+p.eat()+" ");
Teacher teacher=(Teacher) p;
teacher.teach();
}
else{ System.out.println("error");}
}
}
class Person{
public String name;
public int age;
public String eat(){
return "吃饭";
}
}
class Student extends Person{
Student(String name,int age){
this.name = name;
this.age = age;
}
public void learn(){
System.out.println("学习");