目录
多态:
直接赋值:
2. 向下转型
---
前言
这些内容是对javase基础部分的复习
---
继承和组合:
1.关系上说:
继承 is a
组合 has a
2.用法上说:
组合和继承都可以实现代码复用,应该使用继承还是组合,需要根据应用场景来选择,一般建议:能用组合尽量用组合。
>类似于造车,一个车可以由轮子,灯.....构成用组合写代码如下
组合的代码:
```java
//演示组合
class Tire{
private int size;
public void run(){
System.out.println("轮胎转动");
}
}
class Light{
public void bright(){
System.out.println("灯亮");
}
}
public class Vehicle {
private Tire tire;
private Light light;
public Vehicle(Tire tire,Light light){
this.tire=tire;
this.light=light;
}
public void operation(){
tire.run();
light.bright();
}
public static void main(String[] args) {
Tire tire=new Tire();
Light light=new Light();
Vehicle vehicle=new Vehicle(tire,light);
vehicle.operation();
}
}
区别和联系
1.安全性角度(封装性)
组合是拼装在一起内部实现细节是不可见的,具有更好的封装性(黑盒)
在继承结构中,父类的内部细节对于子类是可见的。所以我们通常也可以说通过继承的代码复用是一种白盒式代码复用。(如果基类的实现发生改变,那么派生类的实现也将随之改变。这样就导致了子类行为的不可预知性;)
比如:类的方法被其他类用到,万一其他类重写了该方法,这个方法对后续会有影响(所有用到)
2.灵活性角度(牵一发动全身)
继承,在写代码的时候就要指名具体继承哪个类,所以,在编译期就确定了关系。(从基类继承来的实现是无法在运行期动态改变的,因此降低了应用的灵活性。)
比如:回调(每次继承一次,回调一遍其中的方法和属性)
组合,在写代码的时候可以采用面向接口编程。所以,类的组合关系一般在运行期确定
多态
一.怎样实现多态
1.完成向上转型
注意三点
直接赋值:
直接把子类对象赋给父类
Animal animal=new Bird("鹦鹉",2);
animal.eat()
此时父类引用只能访问自己的方法,要想访问子类特有方法的需要向下转型
方法传参
方法的返回值
2.完成方法重写:
注意6点
animal.eat();
调用的是子类的eat()
上述过程其实就是一次方法重写
满足方法重写的条件:
1.方法名相同,
2.参数列表相同【个数,类型,顺序】
3.返回值相同(或者子类的返回值和父类构成父子类关系(协变类型))
这是子类
这是父类:
public Animal eat() {
System.out.println(name + "会干饭");
return new Animal("动物",18);
}
由于子类的返回值和父类构成父子类关系,运行结果还是子类的eat
4.static修饰方法不能被重写
5.private修饰方法不能被重写(private只能在当前类使用)
6.子类的访问修饰符,需要大于等于父类的访问修饰符
对父类方法重写
3.通过父类引用指向子类对象(重写的方法)
发生了动态的绑定
动态绑定
又叫运行时绑定,子类和父类都有这个方法
编译的时候,还是调用父类自己的,但是在运行的时候,程序发生了动态绑定---子类自己的
这是编译时候的样子:
运行时候,编译器悄悄将animal.eat方法地址改成0x56
父类引用指向子类对象:
// Bird bird=new Bird("鹦鹉",2); // Animal animal=bird;
Animal animal=new Bird("鹦鹉",2);
animal.eat()
此时父类引用只能访问自己的方法,要想访问子类特有方法的需要向下转型
二.什么是多态:
public static void function(Animal animal) {
animal.eat();//调用者不关心谁吃
}
public static void main(String[] args) {
function(new Bird("鹦鹉",2));
function(new Dog("狗子",1));
}
父类引用引用了子类对象,通过父类引用,调用这个重写的方法,此时发生了动态绑定,此时父类引用就这一个,但是我引用的对象不一样,通过调用同一个方法,我们发现,此时所表现的形式是不一样的,把这样的现象叫做多态
三.多态思想的运用(画图形)
package 多态;
class Shape{
public void draw(){
System.out.println("画图形");
}
}
class Rect extends Shape{
@Override
public void draw() {
System.out.println("长方形");
}
}
class Cycle extends Shape{
@Override
public void draw() {
System.out.println("圆形");
}
}
class Triangle extends Shape{
@Override
public void draw() {
System.out.println("△");
}
}
public class Test2 {
public static void drawMAP(Shape shape){
shape.draw();
}
public static void main(String[] args) {
Rect rect=new Rect();
Cycle cycle=new Cycle();
Triangle triangle=new Triangle();
drawMAP(rect);
drawMAP(cycle);
drawMAP(triangle);
}
}
假设现在要画的图形在shape数组中
public static void main(String[] args) {
Rect rect=new Rect();
Cycle cycle=new Cycle();
Triangle triangle=new Triangle();
Shape[]shapes={rect,cycle,rect,cycle,rect,triangle};
for (Shape shape:shapes) {
//当前shape引用对象重写draw方法就调用,没有就调用自己的(已经向上转型和动态绑定了)
shape.draw();
}
}
如果不使用多态
四.多态的好处:
1.降低了圈复杂度(减少条件循环语句出现的个数)
2.扩展能力强
补充内容:
总结重写和重载区别:
方法重载是静态绑定,在编译的时候,在编译时根据实参类型就知道具体的方法
方法重写是动态绑定,在运行的时候,确定调用哪个方法,
重写就是重写再搭一个弓箭,重载是在原来基础上加功能,
即:方法重载是一个类的多态性表现,而方法重写是子类与父类的一种多态性表现
向下转型
不安全的:
编译不报错:
//说明向下转型不安全
public static void main(String[] args) {
Animal animal = new Dog("狗子", 16);
Bird bird = (Bird) animal;
bird.fly();
}
public static void main1(String[] args) {
Animal animal=new Bird("小鸟",2);
Bird bird=(Bird)animal;//向下转型,父类类型给子类类型
bird.fly();
}
运行时,main显示:(狗子不是鸟)
main1因为子类具有这个属性没事(鸟是动物)
修改代码:
引入instance of
public static void main(String[] args) {
Animal animal = new Dog("狗子", 16);
if (animal instanceof Bird) {
//不是所有动物都是鸟
Bird bird = (Bird) animal;
bird.fly();
}
}
狗子不是鸟,显然,没有输出,无法向下转型