用网站做平台,电商网站构建预算方案,门户网站的营销特点,医院网站建设 费用面向对象 多态的概述及其代码的体现
A#xff1a;多态(polymorphic)概述 事物存在的多种形态B#xff1a;多态前提 要有继承关系要有方法重写要有弗雷引用指向子类对象。C#xff1a;案例演示 代码体现多态 public class Dome1 {
public static void main(String[] args…面向对象 多态的概述及其代码的体现
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(看家);}}