java面向对象(接口举例体现)
时间:2014-07-22 22:25 来源: 我爱IT技术网 作者:山风
面向对象(接口举例体现)
1)接口的作用:功能扩展,由子类去实现。
2)继承和接口的区别
继承:你是我中一种,is a
接口:你像我中一种,like a
3)共有功能定义在类中;扩展功能定义在接口中
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- //学员类,是父类,也是抽象类。
- abstract class Student
- {
- //学习方式不同
- abstract void study();
- //都休息
- void sleep(){}
- }
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- //吸烟接口。吸烟不是学生类的基本功能,从学生体系中分离出来,作为扩展功能,需要才用。
- interface Smoking
- {
- void smoke();
- }
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- //有的学员类,是子类。继承Student类,并实现Smoking接口
- class SomeStudents extends Student implements Smoking
- {
- void study(){}
- public void smoke(){}
- }
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- //其它学员类,是子类。继承Student类。
- class OtherStudents extends Student
- {
- void study(){}
- }
- //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- class Test
- {
- public static void main(String[] args)
- {
- System.out.println(1);
- }
- }
本文来源 我爱IT技术网 http://www.52ij.com/jishu/5626.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
