接 class Hero extends ActionCharacterJAVAimplementsCanFight,CanSwim,CanFlypublicvoid swimO(public void fly) {}人interfaceCanFight$classActionCharactervoid fight();public void fight()()7}interfaceCanSwimvoid swim();人interfaceCanFlyvoid fly();16
6 接口多重继承 ◼ 利用接口实现多重继承(Multiple inheritance) ◼ class extends 父类 implements接口 interface CanFight { void fight(); } interface CanSwim { void swim(); } interface CanFly { void fly(); } class ActionCharacter { public void fight() { } } class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly { public void swim() { } public void fly() { } }
class C1 implmentsA1,A2接口合并时的命名冲突public void f public int f(int i) return 5; class C //overloadpublic int f0 return 4;1classC2extendsCimplments A2 interfaceA1public int f(int i) ( return 5; )void f();//overload子classC3extendsCinterfaceA2implmentsA3int f(int i);publicintf0return5;1//implementsandoverridinginterfaceA3class C4 extends Cint f();implements A1 人7
7 接口合并时的命名冲突 interface A1 { void f(); } interface A2 { int f(int i); } interface A3 { int f(); } class C { public int f() { return 4; } } class C1 implments A1, A2 { public void f() { } public int f(int i) { return 5; } } class C2 extends C implments A2 { public int f(int i) { return 5; } } class C3 extends C implments A3 { public int f() { return 5; } } class C4 extends C implements A1 { } //overload //overload //implements and overriding
class DD public static void main(String args[)) System.out.println(LotsOfColors.YELLOW);referencetoYELLOWisambiguous7bothvariableYELLOWin RainbowColorsinterface BaseCo and variableYELLOW inPrintColorsmatchintRED=1,GREEN=2,BLUE=4;人interfaceRainbowColorsextendsBaseColorsintYELLOW=3,ORANGE=5,VIOLET=6;人interfacePrintColorsextendsBaseColorsintYELLOW=8,CYAN=16,MAGENTA=32;人interfaceLotsOfColorsextendsRainbowColors,PrintColorsintFUCHSIA=17,CHARTREUSE=RED+90;
8 接口继承中的命名冲突 interface BaseColors { int RED = 1, GREEN = 2, BLUE = 4; } interface RainbowColors extends BaseColors { int YELLOW = 3, ORANGE = 5, VIOLET = 6; } interface PrintColors extends BaseColors { int YELLOW = 8, CYAN = 16, MAGENTA = 32; } interface LotsOfColors extends RainbowColors, PrintColors { int FUCHSIA = 17, CHARTREUSE = RED+90; } class DD implements LotsOfColors { public static void main(String args[]) { System.out.println(YELLOW); } } class DD { public static void main(String args[]) { System.out.println(LotsOfColors.YELLOW); } } reference to YELLOW is ambiguous, both variable YELLOW in RainbowColors and variable YELLOW in PrintColors match
包(package)JAVA为什么需要包?使Java类更容易发现和使用防止命名冲突进行访问控制层次结构,特定的功能A package is a collection of related classesand interfaces providing access protectionand namespace management.import9
9 ◼ 为什么需要包? ◼ 使Java类更容易发现和使用 ◼ 防止命名冲突 ◼ 进行访问控制 ◼ 层次结构,特定的功能 ◼ A package is a collection of related classes and interfaces providing access protection and namespace management. ◼ import 包 (package)
//Graphic.javafilepublicabstractclassGraphic容易地决定那些类和接口是相互关联的7知道从哪里找到提供图形功能的类和接口//Circle-由于包建立了一个新的名字空间,所以你定义的类不会同其他包中的类名有冲突public可以容许在同一个包中无访问限制,同时可对在本包外的访问进行限制人//Rectangle.javafilepublicclass RectangleextendsGraphicimplementsDraggableL大//Draggable.javafilepublicinterfaceDraggable人
10 //Graphic.java file public abstract class Graphic { . . . } //Circle.java file public class Circle extends Graphic implements Draggable { . . . } //Rectangle.java file public class Rectangle extends Graphic implements Draggable { . . . } //Draggable.java file public interface Draggable { . . . } ◼ 容易地决定那些类和接口是相互关联的 ◼ 知道从哪里找到提供图形功能的类和接口 ◼ 由于包建立了一个新的名字空间,所以你定义的类不 会同其他包中的类名有冲突 ◼ 可以容许在同一个包中无访问限制,同时可对在本包 外的访问进行限制