版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 47217-2026犬猫体况评分技术规范
- GB/T 17980.14-2026农药田间药效试验准则第14部分:杀虫剂防治菜螟
- 浙江省杭州市杭州市萧山区高桥初级中学2025-2026学年普通高中毕业班单科质量检查数学试题含解析
- 浙江省温州市六校2026届初三5月质检英语试题含解析
- 陕西省西安市高新第一中学2026年初三下期入学检测试题数学试题含解析
- 陕西省西安市西安交大附中2025-2026学年初三第二次模拟测试英语试题含解析
- 浙江省宁波市余姚市重点名校2026年初三化学试题质量调研卷(文理合卷)含解析
- 浙江省宁波市镇海区重点达标名校2026年中考二轮物理试题1-4月复习专号数理报含解析
- 武汉市2026届初三下学期期初学情调研考试数学试题试卷含解析
- 2025 高中时评类阅读理解之文化传承创新问题课件
- DB31/T 5000-2012住宅装饰装修服务规范
- 钢结构预拼装方案及标准
- 马工程西方经济学(精要本第三版)教案
- 【初中 语文】第15课《青春之光》课件-2024-2025学年统编版语文七年级下册
- GenAI教育在不同场景下的应用案例分析与演进路径
- GB/T 44815-2024激光器和激光相关设备激光束偏振特性测量方法
- 某爱琴海购物中心开业预热推广方案
- 口腔颌面部肿瘤-血管瘤与脉管畸形的诊疗
- 康复质控中心建设思路和工作计划
- GB/T 44457-2024加氢站用储氢压力容器
- 和父亲断绝联系协议书范本
评论
0/150
提交评论