Presentation is loading. Please wait.

Presentation is loading. Please wait.

辅导课程十六.

Similar presentations


Presentation on theme: "辅导课程十六."— Presentation transcript:

1 辅导课程十六

2 final关键字 TestFinal/TestFinal.java final可以修饰变量(成员变量、局部变量)、方法和类
final修饰的局部变量(形参)。相当于C++中的const final修饰的方法不能够被重写 final修饰的类不能够被继承 JDK中用final修饰的类 类java.lang.System 类java.lang.String 类java.lang.Math 类java.lang.Boolean/Character/Interger/Double… JDK中用final修饰的成员变量 类java.lang.Math中的public static final double PI 类java.lang.System中的public static final PrintStream out TestFinal/TestFinal.java

3 接 口 接口(interface)是抽象方法和常量值的定义的集合。
从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现。 接口定义举例: public interface Runner { public static final int id = 1; public void start(); public void run(); public void stop(); }

4 接口特性 接口中所有成员变量的属性默认为 public static final 的、也只能是public static final的;
接口中只能定义抽象成员方法,而且这些方法默认为public的、也只能是public的; 接口可以继承(extends)其它的接口,并添加新的属性和抽象方法。 public interface Runner { public static final int id = 1; public void start(); public void run(); public void stop(); }

5 接 口 多个无关的类可以实现同一个接口。 一个类可以实现多个无关的接口。 与继承关系类似,接口与实现类之间存在多态性。
定义Java类的语法格式: < modifier> class < name> [extends < superclass>] [implements < interface> [,< interface>]* ] { < declarations>* }

6 接口举例一 TestInterface/TestInterface00.java interface Singer {
public void sing(); public void sleep(); } class Student implements Singer { private String name; Student(String name) {this.name = name;} public void study() {System.out.println("studying");} public String getName() {return name;} public void sing() {System.out.println("student is singing");} public void sleep() {System.out.println("student is sleeping");} TestInterface/TestInterface00.java

7 heap stack sing() sleep() s:xx code seg

8 heap stack sing() sleep() study() getName() name:”wangke” s:xx
Student:study() Student:sleep() s:xx Student:sing() Student:getName() code seg code seg

9 接口举例二 interface Singer { public void sing(); public void sleep(); }
interface Painter { public void paint(); public void eat(); class Student implements Singer { private String name; Student(String name) {this.name = name;} public void study() {System.out.println("studying");} public String getName() {return name;} public void sing() {System.out.println("student is singing");} public void sleep() {System.out.println("student is sleeping");}

10 接口举例二 TestInterface/TestInterface05.java
class Teacher implements Singer,Painter { private String name; public String getString() { return name; } Teacher(String name) {this.name = name;} public void teach() {System.out.println("teaching");} public void sing() {System.out.println("teacher is singing");} public void sleep() {System.out.println("teacher is sleeping");} public void paint() {System.out.println("teacher is painting");} public void eat() {System.out.println("teacher is eating");} TestInterface/TestInterface05.java

11 heap stack sing() sleep() t:xx code seg code seg

12 heap stack sing() sleep() paint() eat() t:xx code seg code seg

13 heap stack sing() sleep() paint() eat() teach() getName() name:”wake”
Teacher:sleep() Teacher:teach() Teacher:eat() t:xx Teacher:sing() Teacher:paint() Teacher:getName() code seg code seg

14 接口举例三 public class TestInterface {
public static void main(String args[]){ Singer s = new Student(“linda"); s.sing(); s.sleep(); Singer t = new Teacher("steven"); // 将Teacher对象当作Singer来看 t.sing(); t.sleep(); Painter p = (Painter)t; //也可以将Teacher对象当作当作Painter来看 p.paint(); p.eat(); } TestInterface/TestInterface.java 输出结果: student is singing student is sleeping teacher is singing teacher is sleeping teacher is painting teacher is eating

15 heap stack sing() sleep() study() getName() name Singer:s1 code seg
站到s1的角度上只能看到sing、sleep方法 Student:study() Student:sleep() Singer:s1 Student:sing() Student:getName() code seg code seg

16 heap stack sing() sleep() paint() eat() teach() getName() name
站到s2的角度上只能看到sing、sleep方法 teach() getName() name Singer:s2 Teacher:sleep() Teacher:teach() Teacher:eat() Teacher:sing() Teacher:paint() Teacher:getName() code seg code seg

17 heap stack sing() sleep() paint() eat() teach() getName() name
Painter:p1 Singer:s2 Teacher:sleep() Teacher:teach() Teacher:eat() Teacher:sing() Teacher:paint() Teacher:getName() code seg code seg

18 接 口 包java.lang中定义的一些接口 public interface Appendable
public interface Cloneable public interface Comparable<T>

19 产生常量群 采用如下方法取用其值 Months.JANUARY 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; } 采用如下方法取用其值 Months.JANUARY

20 数据成员初始化 TestInterface/RandVals/Test.java import java.util.*;
Random rand = new Random(); int randomInt = rand.nextInt(10); long randomLong = rand.nextLong() * 10; float randomFloat = rand.nextLong() * 10; double randomDouble = rand.nextDouble() * 10; } public class Test { public static void main(String[] args) { System.out.println(RandVals.randomInt); System.out.println(RandVals.randomLong); System.out.println(RandVals.randomFloat); System.out.println(RandVals.randomDouble); TestInterface/RandVals/Test.java

21 名称冲突 interface CanFight{ void fight(); } class ActionCharacter{
public void fight() {}; class Hero extends ActionCharacter implements CanFight{ 编译通过

22 名称冲突 interface I { void f(); } class C { public int f() { return 1; }
class C1 extends C implements I { public void f() { } 编译报错:C1 中的 f() 无法覆盖 C 中的 f();正在尝试使用不兼容的返回类型。

23 名称冲突 interface I1 { void f(); } interface I2 { int f();
interface I3 extends I1,I2 { 编译报错:I2 和 I1 不兼容;两者都定义了 f(),但却带有不相关的返回类型。

24 接口用法总结 通过接口可以实现不相关类的相同行为,而不需要考虑这些类之间的层次关系。(就像人拥有一项本领)
通过接口可以指明多个类需要实现的方法。(描述这项本领的共同接口) 通过接口可以了解对象的交互界面,而不需了解对象所对应的类。 使用接口?还是抽象类? interface同时赋予了接口和抽象类的好处 如果父类可以不带任何函数定义,或任何成员变量,那么优先使用接口。

25 回顾 编程语言的发展 面向过程的设计思想 面向对象的设计思想 对象和类的概念 类(对象)之间的关系 Java 类的定义(class)
引用的概念 对象的创建和使用 new 构造方法的概念 方法重载(overload) 构造方法重载 this 关键字 static 关键字 package 和 import 语句 类的继承(extends) 访问控制(private/default/proteced/public) 方法的重写(overwrite,override) super关键字 final关键字 Object类 toString equals 对象转型(upcasting/downcasting) 多态(polymorphism) 抽象类(abstract class) 接口(interface) implements


Download ppt "辅导课程十六."

Similar presentations


Ads by Google