抽象方法、抽象类与接口课件

上传人:石磨 文档编号:178416957 上传时间:2021-04-02 格式:PPT 页数:13 大小:49.50KB
返回 下载 相关 举报
抽象方法、抽象类与接口课件_第1页
第1页 / 共13页
抽象方法、抽象类与接口课件_第2页
第2页 / 共13页
抽象方法、抽象类与接口课件_第3页
第3页 / 共13页
抽象方法、抽象类与接口课件_第4页
第4页 / 共13页
抽象方法、抽象类与接口课件_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《抽象方法、抽象类与接口课件》由会员分享,可在线阅读,更多相关《抽象方法、抽象类与接口课件(13页珍藏版)》请在金锄头文库上搜索。

1、抽象方法、抽象类与接口,1,抽象方法、抽象类与接口,抽象方法 抽象类 接口,抽象方法、抽象类与接口,2,抽象方法 只有声明,没有实现的方法,父类声明方法签名(方法名、参数、返回类型、修饰符) 子类重写次方法,并提供方法体实现 包含抽象方法的类叫做“抽象类” 抽象类不能实例化(不能 new Shape() ) 抽象类也可以包含非抽象方法,Shape,abstract draw(,Circle,draw( ),Rectangle,draw( ),抽象方法、抽象类与接口,3,抽象方法的语法示例,没有花括号方法体 加 “abstract” 修饰符,abstract class Shape abstra

2、ct public void draw( ) ;,抽象方法、抽象类与接口,4,抽象方法与多态,Shape s;s = new Shape() /错误! s = new Circle();s.draw(); /画圆 s = new Rectrangle();s.draw(); /画矩形,抽象方法、抽象类与接口,5,接口(Interface) 即纯抽象类,只有抽象方法,无其它实体方法,接口与抽象类的区别: 接口里面的方法必须全都是抽象的,而抽象类则不然。 一个子类可实现(继承)多个接口,但最多只能继承一个类,Person,void speak( ),妖,abstrract void 妖法 (,cl

3、ass,interface,人妖,void 妖法( ) ./void 仙法( ),间接实现多继承,抽象方法、抽象类与接口,6,接口的实现(继承,一个子类可以实现多个接口 子类必须实现接口中所有的abstract方法,抽象方法、抽象类与接口,7,实例 Animal 抽象类,abstract class Animal /定义抽象类 String str; Animal(String s) /定义抽象类的一般方法 str=s; abstract void eat(); /定义抽象方法,抽象方法、抽象类与接口,8,实例 - Horse 子类,class Horse extends Animal /定义

4、继承Animal的子类 String str; Horse(String s) super(s); /调用父类的构造方法 void eat() /重写父类的抽象方法 System.out.println(马吃草料!);,抽象方法、抽象类与接口,9,实例 - Dog 子类,class Dog extends Animal String str; Dog(String s) super(s); void eat() System.out.println(狗吃骨头!);,抽象方法、抽象类与接口,10,实例 - Test 测试,class test public static void main(St

5、ring args) Horse Horse1 = new Horse(马); Dog Dog1 = new Dog(狗); Horse1.eat(); Dog1.eat();,运行结果: 马吃草料! 狗吃骨头,抽象方法、抽象类与接口,11,示例 驯化 接口(Interface,public interface Domestic abstract void 口令(); abstract void 打招呼();,抽象方法、抽象类与接口,12,实例 - Dog 子类,class Dog extends Animal implement Domestic String str; Dog(String s) super(s); void eat() System.out.println(狗吃骨头!); public void 口令( ) ; public void 打招呼( ) ;,抽象方法、抽象类与接口,13,实例 - Test 测试,class test public static void main(String args) Domestic d; d = new Dog(狗); d.eat(); d.eat(); d.打招呼();,运行结果

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 医学试题/课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号