10-1,接口的应用
小例子:电脑USB接口的实现
interface USB { //暴露的规则
public void open();
public void close();
}
class BookPC {
public static void main(String[] args) {
useUSB(new UPan());
useUSB(new UMouse());
}
public static void useUSB(USB u) {
if(u != null) {
u.open();
u.close();
}
}
}
//实现规则
//这些设备和电脑的耦合性降低了。
class UPan implements USB {
public void open() {
System.out.println("UPan open...");
}
public void close() {
System.out.println("UPan close...");
}
}
class UMouse implements USB {
public void open() {
System.out.println("mouse open...");
}
public void close() {
System.out.println("mouse close...");
}
}
10-2,多态-概述
1,对象的多态性(多种形态):
class 动物{...}
class 猫 extends 动物 {...}
class 狗 extends 动物 {...}
之前我们创建 猫 对象的时候,用的方法是:
猫 mao = new 猫();
有了多态之后,创建 猫 对象可以这么创建:
动物 dw = new 猫();
多态的创建方式,可以看出,一个对象有两种形态。即猫这个事物既具备着猫的形态,又具备着动物的形态,这就是对象的多态性。
简单的说,就是一个对象对应着不同的类型。
多态在代码中的体现:父类或者接口的引用指向其子类的对象。
10-3,多态-好处
好处是:提高了代码的扩展性,前期定义的代码可以使用后期的内容。
举例说明:
abstract class Animal {
abstract void eat();
}
class Dog extends Animal {
//实现父类中的抽象方法,实现动物的共性功能:吃饭
void eat() {
System.out.println("啃骨头");
}
//定义自己的方法,只有狗才具备的功能:看家
void lookHome() {
System.out.println("看家");
}
}
class Cat extends Animal {
void eat() {
System.out.println("吃鱼");
}
void catchMouse() {
System.out.println("抓老鼠");
}
}
class Pig extends Animal {
void eat() {
System.out.println("饲料");
}
void gongDi {
System.out.println("拱地");
}
}
class DuoTaiDemo {
public static void main(String[] args) {
Cat c = new Cat();
Dog d = new Dog();
method(new Pig()); //Animal a = new Pig();
method(c);
method(d);
}
public static void method(Animal a) {
a.eat();
}
}
Cat,Dog,Pig都继承了Animal,是Animal的子类,Animal的变量a指向了创建的Pig对象,调用Pig中的eat()方法,实现了多态性。
10-4,多态-弊端&前提
1,多态的弊端:
前期定义的内容不能使用(调用)后期子类的特有内容。
2,多态的前提:
(1)必须有关系,继承或实现,Animal a = new Cat();Cat要继承Animal。
(2)要有覆盖,不然方法不能用。Cat中的方法要重写Animal中的方法。
弊端:参考10-3的代码:
如:
Cat c = new Cat();
method(c);
...
public static void method(Animal a) {
a.eat();
a.catchMouse();
}
创建Cat对象,把c传给method方法,Animal的a指向c,因为在Animal中没有定义catchMouse方法,所以会报错,只能调用Animal中定义过的并且被覆盖实现的方法。也就是不能调用Cat类中的特有内容catchMouse方法。
10-5,多态-转型:
1,Animal a = new Cat();
a.eat();
这是自动类型提升,猫对象被提升为动物类型。但是特点功能无法访问。
其作用就是限制对特有功能的访问。
专业讲:向上转型,向上造型,上溯造型。
2,Cat c = (cat)a;
如果还想用具体动物猫的特有功能,可以将该对象进行向下转型。
向下转型的目的是为了使用子类中的特有方法。
Cat c = (Cat)a;//强制转换为Cat类型
c.eat();
c.catchMouse();
3,注意:对子转型,自始至终都是子类对象在做着类型的变化。
Animal a1 = new Pig();
Cat c1 = (Cat)a1;//ClassCastException
类型转换失败,子类间不能转换。
10-6,多态-类型判断-instanceof
public static void method(Animal a) {
a.eat();
if(a instanceof Cat) { //判断a是不是Cat类型
Cat c = (Cat)a;
c.catchMouse();
} else if (a instanceof Dog) {
Dog d = (Dog)a;
d.lookHome();
}
}
instanceof:用于判断对象的具体类型。只能用于引用数据类型判断。
通常在向下转型前用于健壮性的判断。
向下造型时通常用instanceof判断一下,提高健壮性,若传入的不是相同类型,在转型时会报错。若把Cat改为Animal,则下面些什么都没有用,因为所有的对象都继承于Animal,都属于Animal类型。
10-7,多态-成员变量
1,多态时,成员变量的特点:
编译时,参考引用类型变量所属的类中是否有调用的成员变量,有则编译通过,没有则编译失败。
运行时,参考引用型变量所属的类中是否有调用的成员变量,并运行该所属类中的成员变量。
简单说:编译和运行都参考等号左边。
class Fu {
int num = 3;
}
class Zi extends Fu {
int num = 4;
}
class DuoTaiDemo {
public static void main(String[] args) {
Fu f = new Zi();
System.out.println(f.num);
}
}
结果:3
左边为父类,就看父类中的nu