1 / 22

继承: extends 变量覆盖、方法重写 super、final 上转型对象

回顾. 继承: extends 变量覆盖、方法重写 super、final 上转型对象. 本节学习目标. 掌握抽象类的创建和使用 掌握接口的创建和使用 了解内部类、匿名类. 5.8 继承与多态. 父类的多个子类都 重写 了父类中的某个方法。 这些子类的 上转型对象 调用重写的方法。. 龙有九子, 子子不同!. 5.9 abstract 类. 画饼充饥!. 使用 abstract 修饰的类,叫 抽象类 。. abstract class A{ …… }. 使用 abstract 修饰的方法,叫 抽象方法 。抽象方法 没有方法体 。.

Download Presentation

继承: extends 变量覆盖、方法重写 super、final 上转型对象

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 回顾 • 继承:extends • 变量覆盖、方法重写 • super、final • 上转型对象

  2. 本节学习目标 • 掌握抽象类的创建和使用 • 掌握接口的创建和使用 • 了解内部类、匿名类

  3. 5.8 继承与多态 • 父类的多个子类都重写了父类中的某个方法。 • 这些子类的上转型对象调用重写的方法。 龙有九子, 子子不同!

  4. 5.9 abstract类 画饼充饥! • 使用abstract修饰的类,叫抽象类。 abstract class A{ …… } • 使用abstract修饰的方法,叫抽象方法。抽象方法没有方法体。 abstract数据类型 方法名(参数);

  5. 5.9 abstract类 • 注意: • 抽象类中可能没有抽象方法,有抽象方法的类一定是抽象类。 • 抽象类不能实例化。 • 抽象类必须继承,抽象方法必须在子类中给出具体实现。 • 子类根据自身需要扩展抽象类。

  6. abstract class Employee {//抽象类 int basic = 2000; //抽象方法 abstract void salary(); } class Manager extends Employee { void salary() {//在子类中重写salary方法 System.out.println("工资等于 "+basic*5+8672); } }

  7. abstract和final能叠用吗? 答:不能,矛盾。

  8. (1)声明 [public]interface接口名 {//接口体} (2)接口体:包括常量和抽象方法 [public][static][final]变量名=初值; [public][abstract]方法类型 方法名([参数表]) 5.10 接口 • 接口:弥补单继承的缺点,即一个类可以实现多个接口。 1. 接口的声明与使用 接口中定义的都是共有常量,可以省略 接口中定义的都是共有抽象方法,可以省略

  9. 5.10 接口 (3)接口的使用 • 接口不能实例化,必须被实现。 • 类必须实现接口的所有方法,否则是抽象类。 • 父类实现了接口,子类自然也就实现了。 • 实现接口的方法时要有修饰符public。 class A implements接口1,接口2, ……

  10. <<interface>> Flyable takeOff() Land() Fly() <<class>> Bird takeOff() Land() Fly() <<class>> Airplane takeOff() Land() Fly() <<class>> Superman takeOff() Land() Fly() 考虑一组可以飞翔的对象,具有相同的本领:飞翔。可以定义下面的公共接口Flyable,支持三个方法:takeOff、land、fly。 public interface Flyable{ public void takeOff(); public void land(); public void fly(); } class Airplane implements Flyable { public void takeOff(){//加速起飞} public void land(){//减速着陆} public void fly(){//保持飞} }

  11. 5.10 接口 2. 接口与多态 (1)接口体只关心功能。 (2)不同的类实现接口,体现多态性。

  12. 5.10 接口 3. 接口的继承与组合 interface A { …… } interface B { …… } interface C { …… } interface D extends A,B,C { …… } interface A { …… } interface B extends A { …… } interface C extends B { …… } interface D extends C { …… }

  13. 什么时候继承? 什么时候组合?

  14. 5.11 接口回调 1. 接口回调:接口声明的变量引用实现它的类的实例。 interface A{ …… } class B implements A{ …… } A a=new B();

  15. interface ShowMessage { void show (); } class TV implements ShowMessage { public void show() { System.out.println(“我是电视机”); } } class PC implements ShowMessage { public void show() { System.out.println(“我是电脑”); } } public class Example { … main… { ShowMessage sm; sm=new TV(); sm.show(); sm=new PC(); sm.show(); } } 声明接口变量 引用类的实例 引用类的实例

  16. 5.11 接口回调 2. 接口作为参数:接口类型的参数可以赋值为实现它的类的实例。 interface A{ …… } class B implements A{ …… } void method(A a){ …… } method(new B());

  17. interface ShowMessage { void show(); } class A implements ShowMessage { public void show() { System.out.println("I love This Game"); } } class C { public void f(ShowMessage s) { s.show(); } } public class Example { public static void main(String args[]) { C c=new C(); c.f(new A()); } } 接口作参数 传递实现接口的类的对象

  18. 5.12 内部类 • 类中类 • 外嵌类 • 内部类 • 内部类是类的一个成员

  19. 5.13 匿名类 • 类体与对象的创建组合在一起 button1.addActionListener(new ActionListener(…){ … }) • 多用于事件处理

  20. 总结: • 上转型对象 • 抽象类的创建和使用 • 接口的创建和使用 • 内部类、匿名类

  21. 实验3:继承 • 实验时间: 待定 • 实验地点:教8五楼 • 实验内容: • 创建“表”类,要求: • 成员变量:时、分、秒 • 成员方法:构造方法、获取时间、 设置时间(系统的时间) • 创建“闹钟”类,要求: • 继承“表”类 • 成员变量:整点报时

  22. 实验3:继承 • 实验要求: • 提前准备实验内容 • 遵守实验室规定 • 遵守编程规范

More Related