JAVA Interface_第1页
JAVA Interface_第2页
JAVA Interface_第3页
JAVA Interface_第4页
JAVA Interface_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、西安电子科技大学软件学院 刘惠,Interface,西安电子科技大学软件学院 刘惠,Agender,What is Interface? How to Create Interface? Interface Vs. Class Interfaces Modifier How to Use Interfer? “Multiple inheritance” in Java Extending an interface with inheritance Grouping constants,西安电子科技大学软件学院 刘惠,What is Interface?,An interface says: “

2、This is what all classes that implement this particular interface will look like.” Thus, any code that uses a particular interface knows what methods might be called for that interface. The interface is used to establish a “protocol” between classes.,西安电子科技大学软件学院 刘惠,How to Create Interface?,To creat

3、e an interface, use the interface keyword instead of the class keyword.,modifier interface interface_name variableType variableName1; variableType variableName2; returnType method1( paras); returnType method2( paras); ,西安电子科技大学软件学院 刘惠,Interface Vs. Class,An interface provides only a form (method nam

4、es, argument lists, and return types), but no method bodies (no implementation). An interface can also contain fields, but these are implicitly static and final.,西安电子科技大学软件学院 刘惠,Interfaces Modifier,Modifier before interface: Like a class, you can add the public keyword before the interface keyword (

5、but only if that interface is defined in a file of the same name) or leave it off to give “friendly” status so that it is only usable within the same package.,西安电子科技大学软件学院 刘惠,Interfaces Modifier,Modifier before method: You can choose to explicitly declare the method declarations in an interface as p

6、ublic. But they are public even if you dont say it.,西安电子科技大学软件学院 刘惠,Interfaces Modifier,Modifier before attribute: An interface can also contain fields, but these are implicitly static and final. The fields in an interface are automatically public .,西安电子科技大学软件学院 刘惠,How to Use Interfer,To make a clas

7、s that conforms to a particular interface (or group of interfaces) use the implements keyword.,西安电子科技大学软件学院 刘惠,First Exaple,Music5.java,西安电子科技大学软件学院 刘惠,“Multiple inheritance” in Java,there is no storage associated with an interfacetheres nothing to prevent many interfaces from being combined. This i

8、s valuable because there are times when you need to say “An x is an a and a b and a c.”,西安电子科技大学软件学院 刘惠,“Multiple inheritance” in Java,西安电子科技大学软件学院 刘惠,Example,Adventure.java,西安电子科技大学软件学院 刘惠,Question1,In above example, if I update code like this , then what will we get?,class Hero implements CanFight

9、, CanSwim, CanFly extends ActionCharacter public void swim() System.out.println(Swin); public void fly() System.out.println(fly); ,西安电子科技大学软件学院 刘惠,Question2,In above example, if I update code like this , then what will we get?,class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly p

10、ublic void swim() System.out.println(Swin); / public void fly() System.out.println(fly); ,西安电子科技大学软件学院 刘惠,Question3,In above example, if I update code like this , then what will we get?,class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly public void swim() System.out.println(Swin

11、); public void fly() System.out.println(fly); public void fight() System.out.println(fight); /add a line ,西安电子科技大学软件学院 刘惠,Extending an interface with inheritance,You can easily add new method declarations to an interface using inheritance ,you can combine several interfaces into a new interface with inheritance . HorrorShow.java,西安电子科技大学软件学院 刘惠,Grouping constants,Because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, much as you would with an enum in C or C+. See an example:,public interf

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论