一、接口
1.接口中定义的所有成员方法都是抽象的,没有方法体的,使用的修饰符默认为public abstract
2.接口中定义的所有成员变量都是常量,初始化之后不能修改,使用的默认修饰符为public final abstract
3.一个类可以实现很多个接口,但是只能继承一个父类.通过接口间接实现了多重继承
4.降低了耦合性
5.接口可以继承接口,而且可以多重继承
二、多态
1.因为一个类可以继承父类,可以实现多个接口,所以便具有了多种状态,多种身份。在多种身份中灵活的转换,就是多态的主要作用,即:使用父类(接口)的变量来引用子类(实现类)的对象。等号左侧为父类或者接口,小范围。等号右侧为子类或者实现类,大范围
2.小范围 = 大范围,属于隐式转换。但是 大范围 = 小范围,必须强制转换,容易出现转换类型异常。
3.在继承或者接口实现中,父类中的同名方法是会被子类覆盖掉的,但是父类中的同名字段属性则不会被覆盖掉,二者是共存的,并且通过super 和 this进行区分。
4.使用多态的时候,注意一点:方法如果被覆盖,那么就看子类,等号右侧。字段则是看等号左侧,变量类型是谁就是谁。
三、使用MotherBoard,PCI,VideoCard,SoundCard,NetCard实现接口和类的关系
PCI是接口
VideoCard,SoundCard,NetCard是实现类,实现PCI接口。
MotherBoard是类,使用PCI作为方法参数。
代码展示
public class Ts01 { public static void main(String[] args) { MotherBoard mo = new MotherBoard(); VideoCard vPci = new VideoCard(); SoundCard sPci = new SoundCard(); NetCard nPci = new NetCard(); mo.play(vPci); mo.play(sPci); mo.play(nPci); } } class MotherBoard { public void play(PCI pci) { pci.usb(); } } class VideoCard implements PCI { public void usb() { System.out.println("我是显卡USB"); } } class SoundCard implements PCI { public void usb() { System.out.println("我是声卡USB"); } } class NetCard implements PCI { public void usb() { System.out.println("我是网卡USB"); } } interface PCI { public void usb(); }
四、Girl.boyFriend(TRH)
Girl:女孩(class)
Tall:高(interface)
Rich:富(interface)
Handsome:帅(interface)
TRH:高富帅(interface)
代码展示:
public class Ts02 { public static void main(String[] args) { Girl g = new Girl(); NMX l = new NMX(); g.boyFriend(l); System.out.println("------------------"); Tall t = l; Rich r = l; t.tall(); r.rich(); System.out.println("------------------"); Handsome h = (Handsome) t; h.handsome(); } } class Girl { public void boyFriend(TRH trh) { trh.tall(); trh.rich(); trh.handsome(); } } class NMX implements TRH { @Override public void tall() { System.out.println("I am tall"); } @Override public void rich() { System.out.println("I am rich"); } @Override public void handsome() { System.out.println("I am handsome"); } } interface Tall { public void tall(); } interface Rich { public void rich(); } interface Handsome { public void handsome(); } interface TRH extends Tall, Rich, Handsome { }
五、多态展示
public class Ts01 { public static void main(String[] args) { Jing8 jing8 = new Jing8(); jing8.cry(); Dog dog = jing8; dog.cry(); System.out.println("========="); System.out.println("jing8.name--" + jing8.name); System.out.println("dog.name--" + dog.name); System.out.println("========="); System.out.println("jing8.name--" + jing8.getName()); System.out.println("dog.name--" + dog.getName()); } } abstract class Animal { abstract public void cry(); } class Dog extends Animal { public String name = "dahuang"; public String getName() { System.out.println("Dog.getName()"); return this.name; } public void cry() { System.out.println("wangwang~~"); } } class Jing8 extends Dog { public String name = "xiaohuang"; public String getName() { System.out.println("Jing8.getName()"); return this.name; } public void cry() { System.out.println("heihei~~~"); } }
全文完!