《Java程序设计》课程教学课件(PPT讲稿)04 Java面向对象5-面向对象特征(3/3)

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

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 . { . . . }

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; } 接口名修饰

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(); }

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; } }

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() { } }

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

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

9 ◼ 为什么需要包? ◼ 使Java类更容易发现和使用 ◼ 防止命名冲突 ◼ 进行访问控制 ◼ 层次结构,特定的功能 ◼ A package is a collection of related classes and interfaces providing access protection and namespace management. ◼ import 包 (package)

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 { . . . } ◼ 容易地决定那些类和接口是相互关联的 ◼ 知道从哪里找到提供图形功能的类和接口 ◼ 由于包建立了一个新的名字空间,所以你定义的类不 会同其他包中的类名有冲突 ◼ 可以容许在同一个包中无访问限制,同时可对在本包 外的访问进行限制
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《Java程序设计》课程教学课件(PPT讲稿)04 Java面向对象4-面向对象特征(2/3).pptx
- 《Java程序设计》课程教学课件(PPT讲稿)04 Java面向对象3-面向对象特征(1/3).pptx
- 清华大学出版社:《计算机操作系统教程》习题解答与实验指导(教材PDF电子版,第2版,编著:张尧学).pdf
- 《汇编语言与接口技术》课程教学资源(作业习题)汇编语言与接口技术练习题(答案).doc
- 《汇编语言与接口技术》课程教学资源(作业习题)汇编语言与接口技术练习题(题目).doc
- 《汇编语言与接口技术》课程教学资源(PPT课件)第7章 串并行接口技术.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第8章 中断和DMA技术.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第5章 软件接口技术.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第6章 存储器技术.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第2章 80x86微处理器.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第4章 80x86汇编语言程序设计.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第1章 微型计算机概述.ppt
- 《汇编语言与接口技术》课程教学资源(PPT课件)第3章 80x86指令系统和寻址方式.ppt
- 《汇编语言与接口技术》课程教学大纲 Assembly Language and Interface Technology.doc
- 浙江科技大学:《计算机网络》课程教学资源(PPT课件)第6章 应用层.ppt
- 浙江科技大学:《计算机网络》课程教学资源(PPT课件)第5章 运输层.ppt
- 浙江科技大学:《计算机网络》课程教学资源(PPT课件)第7章 网络安全.ppt
- 浙江科技大学:《计算机网络》课程教学资源(PPT课件)第4章 网络层.ppt
- 浙江科技大学:《计算机网络》课程教学资源(PPT课件)第3章 数据链路层.ppt
- 浙江科技大学:《计算机网络》课程教学资源(PPT课件)第1章 概述(主讲:马伟锋).ppt
- 《Java程序设计》课程教学课件(PPT讲稿)05 Java异常处理.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)06 Java文件输入输出.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)07 Java线程.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)08 Java网络编程.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)09 Java数据库编程(1/2).pptx
- 《Java程序设计》课程教学课件(PPT讲稿)09 Java数据库编程(2/2).pptx
- 《Java程序设计》课程教学课件(PPT讲稿)0 1Java概述.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)02 Java程序设计基础1—运算符和表达式.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)03 Java程序设计基础2—数组.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)03 Java程序设计基础3—程序流程控制.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)04 Java面向对象1-软件开发周期简介.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)04 Java面向对象2-面向对象程序设计基础.pptx
- 《Java程序设计》课程教学课件(PPT讲稿)Coding_Standard_Java.pptx
- 《数据结构》课程教学大纲 Data Structure.doc
- 《数据结构》课程教学课件(PPT讲稿)第一章 绪论.ppt
- 《数据结构》课程教学课件(PPT讲稿)第三章 栈和队列.ppt
- 《Java基础入门》课程电子教案(PPT教学课件)第1章 Java开发入门.pptx
- 《Java基础入门》课程电子教案(PPT教学课件)第2章 Java编程基础.pptx
- 《Java基础入门》课程电子教案(PPT教学课件)第3章 面向对象(上).pptx
- 《Java基础入门》课程电子教案(PPT教学课件)第4章 面向对象(下).pptx
