/*
重载,重写,多态
*/publicclassExam1{publicstaticvoidmain(String[] args){
A a1 =newA();//本态引用,只看A类
A a2 =newB();//多态,看运行时类也就是B类
B b =newB();//本态引用看B类,所以也是4个方法,A类和B类中的方法,A类中的方法有被重写的
C c =newC();
D d =newD();
System.out.println("(1)"+ a1.show(b));//多态参数,哪个能接受B类型的对象/*
* public String show(D obj){//D不行D是B的子类
return ("A and D");
}
public String show(A obj){//A可以A是B的父类,所以结果是 A and A
return "A and A";
}
*/
System.out.println("(2)"+ a2.show(d));/*
//这是父类的两个方法
public String show(D obj){ //这个直接符合D类所以结果就是 A and D
return ("A and D");
}
//这个方法被子类重写了,也就是覆盖了,所以不运行了,直接看子类中的方法
// public String show(A obj){
// return "A and A";
// }
//这是子类的两个方法
public String show(B obj){
return "B and B";
}
public String show(A obj){
return "B and A";
}
*/
System.out.println("(3)"+ b.show(c));/*
//这是父类的两个方法
public String show(D obj){
return ("A and D");
}
//这个方法被子类重写了,也就是覆盖了,所以不运行了,直接看子类中的方法
// public String show(A obj){
// return "A and A";
// }
//这是子类的两个方法
public String show(B obj){ //没有直接的C类,按照就近原则,选择C的父类也就是B类 所以结果是 B and B
return "B and B";
}
public String show(A obj){
return "B and A";
}
*/
System.out.println("(4)"+ b.show(d));/*
//这是父类的两个方法
public String show(D obj){ //直接就有D类了,是所以结果就是 A and D
return ("A and D");
}
//这个方法被子类重写了,也就是覆盖了,所以不运行了,直接看子类中的方法
// public String show(A obj){
// return "A and A";
// }
//这是子类的两个方法
public String show(B obj){
return "B and B";
}
public String show(A obj){
return "B and A";
}
*/}}classA{public String show(D obj){return("A and D");}public String show(A obj){return"A and A";}}classBextendsA{public String show(B obj){return"B and B";}public String show(A obj){return"B and A";}}classCextendsB{}classDextendsB{}/*
(1)A and A
(2)A and D
(3)B and B
(4)A and D
*/
练习2
/*
* 多态只针对方法
* 属性没有多态,属性只看编译时类型
*/publicclassExam2{publicstaticvoidmain(String[] args){
Base b =newSub();//多态引用
System.out.println(b.x);//父类无法直接调用子类重写的属性和方法}}classBase{int x =1;}classSubextendsBase{int x =2;}
练习3
publicclassFather{
String name ="atguigu";int age =0;}/*
考权限修饰符
private在别的类中不能直接使用
*/publicclassChildextendsFather{public String grade;publicstaticvoidmain(String[] args){
Father f =newChild();//System.out.println(f.name);//报错}}
练习4
publicclassPerson{publicPerson(){//构造器
System.out.println("this is a Person.");//不继承}}/*
* 考点:
* (1)super()必须在构造器的首行
* (2)静态方法中不能使用this
*/publicclassTeacherextendsPerson{private String name ="tom";publicTeacher(){//子类无参构造器
System.out.println("this is a teacher.");//super();//空参 报错}publicstaticvoidmain(String[] args){
Teacher tea =newTeacher();//本态//System.out.println(this.name);this is a teacher.Tom 报错}}
classHelloA{publicHelloA(){
System.out.println("HelloA");}{
System.out.println("I'm A Class");}static{
System.out.println("static A");//静态}}publicclassHelloBextendsHelloA{publicHelloB(){
System.out.println("HelloB");}{
System.out.println("I'm B Class");}static{
System.out.println("static B");//静态}publicstaticvoidmain(String[] args){newHelloB();}}
练习7
/*
* (1)父类的实例化
* 默认调用父类的无参构造 System.out.print("1"); 1
* (1)子类的实例化
* System.out.print("3"); 3
father = new People(name +" F"); 显示创建父类的对象,显式调用父类有参构造 System.out.print("2");
*
*/publicclassTestChild{publicstaticvoidmain(String[] args){newChild("mike");}}classPeople{private String name;publicPeople(){
System.out.print("1");}publicPeople(String name){
System.out.print("2");this.name = name;}}classChildextendsPeople{
People father;publicChild(String name){
System.out.print("3");
father =newPeople(name +" F");}publicChild(){
System.out.print("4");}}
练习8
/*
* 多态创建子类对象的过程
* (1)导致父类的实例化初始化方法执行
* 父类 x = 10;
*
* this.print(); 此时this代表子类的对象 执行 System.out.println("Son.x = " + x); 这个x是子类的x,还没有赋值,所以默认值0
父类 x = 20;
(2)子类的实例化初始化方法的执行
子类 x = 30
this.print(); 此时的this也是子类 执行System.out.println("Son.x = " + x); 30
x = 40;
(3)System.out.println(f.x);
这个是父类的X,为什呢,应为属性没有多态
*/publicclassExam3{publicstaticvoidmain(String[] args){
Father f =newSon();
System.out.println(f.x);}}classFather{int x =10;publicFather(){this.print();
x =20;}publicvoidprint(){
System.out.println("Father.x = "+ x);}}classSonextendsFather{int x =30;publicSon(){this.print();
x =40;}publicvoidprint(){
System.out.println("Son.x = "+ x);}}
练习9
publicclassFather{publicFather(){
System.out.println("father create");}}publicclassChildextendsFather{publicChild(){
System.out.println("child create");}publicstaticvoidmain(String[] args){
Father f =newFather();
Child c =newChild();}}
练习10
publicclassFather{private String name ="father";public String getName(){return name;}}/*
* 因为子类没有重写getName(),离getName()声明最近,就近找到父类的this
*/publicclassTestextendsFather{private String name ="test";publicstaticvoidmain(String[] args){
Test test =newTest();
System.out.println(test.getName());}}