本帖最后由 王琪 于 2013-12-22 02:04 编辑
<12>这个是我自己想的,想了很多样子,最后把自己弄混了! 谢谢
class person{ public String name="爸爸"; public int age=40; public String talk(){ return "我是爸爸"; } }
class student extends person{ public String name="孩子"; public int age=21; public String school ="辽宁工大"; public String talk(){ return "我是儿子"; } } public class java { public static void main(String[]args){ person p1_2; student p2_2=new student(); p1_2=p2_2; System.out.println(p1_2.name+p1_2.age+p1_2.talk()); System.out.println(p2_2.name+p2_2.age+p2_2.school+p2_2.talk()); /* person p1_3; student p2_3=new student(); p2_3=(student)p1_3;
person p1; student p2=new student(); p1=p2; p2=(student)p1; System.out.println(p1_2.name+p1_2.age+p1_2.talk()); System.out.println(p2_2.name+p2_2.age+p2_2.school+p2_2.talk());
/* //********************************************************** person p3_2=new person(); student p4_2; p4_2=(student)p3_2;
person p3_3=new person(); student p4_3; p3_3=p4_3;
person p3=new person(); student p4; p4=(student)p3; p3=p4; //**************************************************************
person p5_2=new student(); student p6_2=(student)p5_2; p5_2=p6_2;
person p5=new student(); student p6=(student)p5; p5=p6; //******************************************* //错student p7=new person(); person pp1=new person(); student pp2=new student(); pp1=pp2; pp2=(student)pp1;
person pp1_2=new person(); student pp2_2=new student(); pp1_2=pp2_2;
person pp1_3=new person(); student pp2_3=new student(); pp2_3=(student)pp1_3; */ }
}
|