
JavaSunMicrotyterJava的面向对象特征(3)ZUST Software Institute
1 Java的面向对象特征(3) ZUST Software Institute

接口(interface)JAVA接口是对ahstract类论进一步扩层An interfaceisa named collection of method接口方冲definitions(withoutimplementations)法),An interfacecanalsodeclareconstants协议class类名 implements 接口1,接口2... 接口定义[public]interface接口名成员变量:方法声明:人一个类符合某个或一组接口,利 用implements2
2 ◼ 接口是对abstract类的进一步扩展 ◼ 接口中的方法都是未实现的(类似于抽象方 法),目的是在实现接口的类之间建立一种 协议 ◼ 接口中的变量都是常量 ◼ 定义 ◼ 一个类符合某个或一组接口,利用implements 接口 (interface) An interface is a named collection of method definitions (without implementations). An interface can also declare constants. [public] interface 接口名 { 成员变量; 方法声明; } class 类名 implements 接口1, 接口2 . { . . . }

接口名修饰JAVA接口名修饰public:无任何访问限制无修饰:仅限于本包中接口变量默认都是“public static final'public interface Months ^int JANUARY=1, FEBRUARY=2, MARCH=3APRIL=4, MAY=5, JUNE=6, JULY=7,AUGUST=8,SEPTEMBER=9,OCTOBER=10NOVEMBER=11,DECEMBER=12;13
3 ◼ 接口名修饰 ◼ public: 无任何访问限制 ◼ 无修饰: 仅限于本包中 ◼ 接口变量默认都是“public static final” public interface Months { int JANUARY=1, FEBRUARY=2, MARCH=3, APRIL=4, MAY=5, JUNE=6, JULY=7, AUGUST=8, SEPTEMBER=9,OCTOBER=10, NOVEMBER=11,DECEMBER=12; } 接口名修饰

接口方法quizJAVAinterfaceFigureclass Circle implements Figuredouble x, y, r;doublehalf=0.5,pi=3.14159;Circle(double u, double , double m)voidparameterO;(x=u;y=v;r=m;}void area();publicvoidparameter(){ System.out.println(x +"" +y+" "+r); }class Triangle implements Figurepublic void area)double b, h; System.out.println(pi*r*r);)Triangle (double u, double v) }b = u; h = v,Triangle t = new Triangle(2, 3)7Circlec = new Circle(4, 5, 6)public voidparameterOFigure[] f = {t, c}];System.out.println(b + "" + h);7for (int i =0; i < f.length; i++) (public void area()f[i].parameter(); System.out.println(half*h*b);)f[i].area();人
4 接口方法 quiz ◼ 接口方法 ◼ 无修饰 ◼ 但在实现接口方法的类中,修饰符为public interface Figure { double half=0.5,pi=3.14159; void parameter(); void area(); } class Triangle implements Figure { double b, h; Triangle (double u, double v) { b = u; h = v; } public void parameter() { System.out.println(b + ""+ h); } public void area() { System.out.println(half*h*b); } } class Circle implements Figure { double x, y, r; Circle(double u, double v, double m) { x=u; y=v; r=m; } public void parameter() { System.out.println(x +“ " +y+“ " +r); } public void area() { System.out.println(pi*r*r); } } Triangle t = new Triangle(2, 3); Circle c = new Circle(4, 5, 6); Figure[] f = {t, c}; for (int i =0; i < f.length; i++) { f[i].parameter(); f[i].area(); }

classDDimplementsDA,DX接口的public int add(int x, int y) return x+y; )JAVApublic int sub(int x, int y) ( return x-y; 接public int mul(int x, int y) return x*y;public int div(int x, int y) (return x/y;)public int mod(int x, int y) return x%y; )人interfaceDCinterfaceDYintadd(intx,inty))int div(int x, int y))1interfaceDBextendsDCinterfaceDXextendsDYint sub(int x, int y);int mod(int x, int y)interfaceDA extendsDBint mul(int x, int y)}5
5 接口的继承 extends ◼ 接口的继承 extends ◼ 将多个接口合并为一个新的接口 interface DC { int add(int x, int y); } interface DB extends DC { int sub(int x, int y); } interface DA extends DB { int mul(int x, int y); } interface DY { int div(int x, int y); } interface DX extends DY { int mod(int x, int y); } class DD implements DA, DX { public int add(int x, int y) { return x+y; } public int sub(int x, int y) { return x-y; } public int mul(int x, int y) { return x*y; } public int div(int x, int y) { return x/y; } public int mod(int x, int y){ return x%y; } }

接 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 { . . . } ◼ 容易地决定那些类和接口是相互关联的 ◼ 知道从哪里找到提供图形功能的类和接口 ◼ 由于包建立了一个新的名字空间,所以你定义的类不 会同其他包中的类名有冲突 ◼ 可以容许在同一个包中无访问限制,同时可对在本包 外的访问进行限制