JAVA开发与应用 抽象类与接口和多态.ppt

  1. 1、本文档共27页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
JAVA开发与应用 抽象类与接口和多态

抽象类与接口;抽象方法和抽象类;接口 ;接口的概念 ;Java接口 ;接口应用实例 ;接口;接口;Math.java interface Math { double PI=3.1415926; } class Arithmetic implements Math { double roundArea(double radius) { return PI*radius*radius; } } class Student { public static void main(String[] args) { Arithmetic a=new Arithmetic(); System.out.println(a.roundArea(3)); System.out.println(Math.PI); //ok System.out.println(Arithmetic.PI); //ok System.out.println(a.PI); //ok } };接口;Sofa.java interface Sittable { void sit(); } interface Lie { void sleep(); } interface Chair extends Sittable { } interface Sofa extends Sittable, Lie { };接口;Sofa.java //实现多个接口 interface Sittable { void sit(); } interface Lie { void sleep(); } class Sofa implements Sittable, Lie { public void sit() { } public void sleep() { } };Sofa.java //在继承类的同时,实现多个接口 interface Sittable { void sit(); } interface Lie { void sleep(); } interface HealthCare { void massage(); } class Chair implements Sittable { public void sit(){}; } class Sofa extends Chair implements Lie, HealthCare { public void sleep(){}; public void massage(){}; };多态的概念;方法的覆盖与重载的区别;public?Class?Parents?{ ??public?void?print()?{ ????System.out.println(“parents”); } } public?Class?Father?extends?Parents?{ ??public?void?print()?{ System.out.println(“father”); } } public?Class?Mother?extends?Parents?{ ??public?void?print()?{ ????System.out.println(“mother”); } } ;最后的输出结果分别是father和mother, 将派生类的引用传给基类的引用,然后调用重写方法,基类的引用之所以能够找到应该调用那个派生类的方法,就是因为程序在运行时进行了绑定。 在Java中,谈论多态就是在讨论方法调用的绑定,绑定就是将一个方法调用同一个方法主体关联起来。在C语言中,方法(在C中称为函数)的绑定是由编译器来实现的,在英文中称为early?binding(前期绑定),因此,大家自然就会想到相对应的late?binding(后期绑定),这在Java中通常叫做run-time?binding(运行时绑定),运行时绑定的目的就是在代码运行的时候能够判断对象的类型。 多态,的作用就是用来将接口和实现分离开,改善代码的组织结构,增强代码的可读性。 ;多态的一个小结: 1.??Java中除了static和final方法外,其他所有的方法都是运行时绑定的。private方法都被隐式指定为final的,因此final的方法不会在运行时绑定。当在派生类中重写基类中static、final、或private方法时,实质上是创建了一个新的方法。 2.在派生类中,对于基类中的private方法,最好采用不同的名字。 3.包含抽象方法的类叫做抽象类。注意定义里面包含这样的意思,只要类中包含一个抽象方法,该类就是抽象类。抽象类在派生中就是作为基类的角色,为不同的子类提供通用的

您可能关注的文档

文档评论(0)

sy78219 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档