面向对象 多态的概述及其代码的体现
- A:多态(polymorphic)概述
- 事物存在的多种形态
- B:多态前提
- 要有继承关系
- 要有方法重写
- 要有弗雷引用指向子类对象。
C:案例演示
- 代码体现多态
public class Dome1 { public static void main(String[] args) {Cat c = new Cat();c.eat();Animal a = new Cat(); //父类引用指向子类对象a.eat();最后的输出结果为 Cat 这个类中的eat方法语句;这里的的Cat是Animal的一种,所以可以理解为a.eat();为调用Cat方法。} } class Animal { public void eat() {System.out.println("动物吃饭");} } class Cat extends Animal { public void eat() {System.out.println("猫吃鱼"); }}
- 代码体现多态
面向对象 多态中的成员访问特点之成员变量
- 成员变量
- 编译看左边(父类),运行看左边(父类);
public class Demo2 {public static void main(String[] args) {Father f = new Son();System.out.println(f.num);//结果为 10; Son s = new Son();System.out.println(s.num);//结果为 20;}}/*成员变量编译看左边(父类),运行也看左边(父类); */class Father{int num = 10;}class Son extends Father{int num = 20;}
面向对象 多态中的成员访问特点之成员方法
- 成员方法
- 编译看左边(父类),运行看右边(子类);
public class Demo2 {public static void main(String[] args) {Father f = new Son();f.print();//结果为 son;}}/*成员方法编译看左边(父类),运行看右边(子类);*/class Father{int num = 10;public void print() {System.out.println("father"); }}class Son extends Father{int num = 20;public void print() {System.out.println("son"); }
}
package beysdasxw;public class Test1_Demo {public static void main(String[] args) {Fu f = new Zi();// f.method();//成员方法,编译看左边(父类)没有method方法,所以会出现编译错误;f.show();}}
class Fu{public void show(){System.out.println("fu show");}
}class Zi extends Fu{public void show(){System.out.println("zi show");}public void method(){System.out.println("zi method");}
}
面向对象 多态中的成员访问特点之成员方法
- 静态方法
- 编译看左边(父类),运行看左边(父类)。
- (静态和类相关,算不上重写,所以,访问还是左边的)
- 只有非静态的成员方法,编译看左边,运行看右边。
public class jingtai {public static void main(String[] args) { Father f = new Son();f.method();//相当与Father。net后的();}} /*静态方法编译看左边(父类),运行看左边(父类)。(静态和类相关,算不上重写,所以,访问还是左边的)只有非静态的成员方法,编译看左边,运行看右边。 */class Father {int num = 10;public void print(){System.out.println("Father");} public static void method(){System.out.println("Father static method");} }class Son extends Father{int num = 20; public void print(){System.out.println("son");} public static void method(){System.out.println("Son static method");}}
面向对象(多态中的向上转型和向下转型)
- A:案例分析
- Person p = new SuperMan();//向上转型
- SuperMan sm = (SuperMan)p;//向下转型
只有向上转型之后才可以向下转型;
public class Dome_SuperMan {public static void main(String[] args) {Person p = new SuperMan(); //父类引用指向子类对象,超人提升为了人System.out.println(p.name);//父类引用指向子类对象就是向上转型p.谈生意();SuperMan sm = (SuperMan)p;//向下转型sm.fly();/*基本数据类型自动类型提升和强制类型转换*/int i =10;byte b= 20;//i = b; //自动类型提升//b= (byte) i; //强制类型转换;}}class Person{String name = "John";public void 谈生意(){System.out.println("谈生意");}}class SuperMan extends Person{String name = "SuperMan";public void 谈生意(){System.out.println("谈几个亿的大单子");}public void fly(){System.out.println("飞出去救人");}}
public static void main(String[] args) {//Cat c = new Cat ();//c1.eat();method (new Cat());method (new Dog()); //Animal a = new Cat(); 开发的是很少在创建对象的时候用父类引用指向子类对象,直接创建子类对象更方便,可以使用子类中特有的属性和行为} // Cat c = new Dog(); 狗是一只猫,这是错误的 /* public static void method(Cat c){ c.eat();}public static void method(Dog d){d.eat(); }*///如果吧狗强制转成猫就会出现类型转换异常,ClassCastException public static void method(Animal a){//当作参数的时候用多态最好,因为拓展性强 /*Cat c = (Cat)a;c.eat();c.catchMouse();*/ //关键字 instanseof 判断前边的引用是否是后面的数据类型 if(a instanceof Cat){Cat c = (Cat)a;c.eat();c.catchMouse();}else if(a instanceof Dog){Dog d = (Dog)a;d.eat();d.lookHome();}else {a.eat();} }
}
/** A:多态的好处* a;提高了代码的维护性(继承保证)* b;提高了代码的拓展性(由多态保证) * B;案例演示* 多态的好处* 可以当作形式参数,可以接收任意子类对象* C;多态的弊端* 不能使用子类的特有属性和行为 */class Animal{public void eat(){System.out.println("动物吃饭");} }class Cat extends Animal{public void eat(){System.out.println("猫吃鱼");} public void catchMouse(){System.out.println("抓老鼠");}}class Dog extends Animal{public void eat(){System.out.println("狗吃肉");} public void lookHome(){System.out.println("看家");}}