Java学习笔记--多态:多态的介绍,多态的基本使用,多态的条件下成员的访问特点,多态的好处
目录
1.多态的介绍
2.多态的基本使用
编辑
3.多态的条件下成员的访问特点
3.1成员变量
3.2成员方法
4.多态的好处(为什么学多态)
1.问题描述:
2.多态方式和原始方式new对象的优缺点:
一.多态的介绍
1.前提:a.必须有子父类继承或者接口实现关系b.必须有方法的重写(没有重写,多态没有意义),多态主要玩儿的是重写方法c.new对象:父类引用指向子类对象Fu fu = new Zi() -> 理解为大类型接收了一个小类型的数据 ->比如 double b = 10
2.注意:多态下不能直接调用子类特有功能
二.多态的基本使用
用动物类举例
public abstract class Animal {public abstract void eat();
}
public class Dog extends Animal{@Overridepublic void eat() {System.out.println("狗啃骨头");}//特有方法public void lookDoor(){System.out.println("狗会看门");}
}
public class Cat extends Animal{@Overridepublic void eat() {System.out.println("猫吃鱼");}//特有方法public void catchMouse(){System.out.println("猫会捉老鼠");}
}
public class Test01 {public static void main(String[] args) {//原始方式Dog dog = new Dog();dog.eat();//重写的dog.lookDoor();//特有的Cat cat = new Cat();cat.eat();//重写的cat.catchMouse();//特有的System.out.println("==================");//多态形式new对象Animal animal = new Dog();//相当于double b = 10animal.eat();//重写的 animal接收的是dog对象,所以调用的是dog中的eat
// animal.lookDoor(); 多态前提下,不能直接调用子类特有成员Animal animal1 = new Cat();animal1.eat();//cat重写的}
}
三.多态的条件下成员的访问特点
3.1成员变量
public class Fu {int num = 1000;
}
public class Zi extends Fu{int num = 100;
}
public class Test01 {public static void main(String[] args) {Fu fu = new Zi();System.out.println(fu.num);}
}
看等号左边是谁,先调用谁中的成员变量
3.2成员方法
public class Fu {int num = 1000;public void method(){System.out.println("我是父类中的method方法");}
}
public class Zi extends Fu{int num = 100;public void method(){System.out.println("我是子类中的method方法");}
}
public class Test01 {public static void main(String[] args) {Fu fu = new Zi();System.out.println(fu.num);//父类中的numfu.method();//子类中重写的method方法}
}
看new的是谁,先调用谁中的成员方法,子类没有,找父类
四.多态的好处(为什么学多态)
1.问题描述:
如果使用原始方式new对象(等号左右两边一样),既能调用重写的,还能调用继承的,还能调用自己特有的成员
但是多态方式new对象,只能调用重写的,不能直接调用子类特有的成员,那为啥还要用多态呢?
2.多态方式和原始方式new对象的优缺点:
原始方式:
a.优点:既能调用重写的,还能调用父类非私有的,还能调用自己特有的
b.缺点:扩展性差,不能同时接受多个子类类型,需要重写,这就意味着需要不断的创建对应子类方法
多态方式:
a.优点:扩展性强,比如,接受不同子类类型
Fu fu = new Zi()
double b = 10;
b = 100L;
b.缺点:不能直接调用子类特有功能
public abstract class Animal {public abstract void eat();
}
public class Dog extends Animal {@Overridepublic void eat() {System.out.println("狗啃骨头");}//特有方法public void lookDoor(){System.out.println("狗会看门");}
}
public class Cat extends Animal {@Overridepublic void eat() {System.out.println("猫吃鱼");}//特有方法public void catchMouse(){System.out.println("猫会捉老鼠");}
}
public class Test01 {public static void main(String[] args) {Dog dog = new Dog();dog.eat();//重写的dog.lookDoor();//特有的//dog = new Cat();System.out.println("=============");method(dog);Cat cat = new Cat();method(cat);/* houzi houzi = new houzi();method(houzi);bird bird = new bird();method(bird);*/}public static void method(Dog dog){dog.eat();dog.lookDoor();}public static void method(Cat cat){cat.eat();cat.catchMouse();}/* public static void method(houzi houzi){cat.eat();cat.catchMouse();}*/
}
public class Test02 {public static void main(String[] args) {/*double b = 10;b = 100L;*/Animal animal = new Dog();animal.eat();animal = new Cat();animal.eat();System.out.println("=================");Dog dog = new Dog();method(dog);Cat cat = new Cat();method(cat);}/*形参传递父类类型,调用此方法父类类型可以接收任意它的子类对象传递哪个子类对象,就指向哪个子类对象,就调用哪个子类对象重写的方法*/public static void method(Animal animal){//Animal animal = dog Animal animal = catanimal.eat();}
}
形参传递父类类型,调用此方法父类类型可以接收任意它的子类对象 传递哪个子类对象,就指向哪个子类对象,就调用哪个子类对象重写的方法