- 1、本文档共27页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 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.包含抽象方法的类叫做抽象类。注意定义里面包含这样的意思,只要类中包含一个抽象方法,该类就是抽象类。抽象类在派生中就是作为基类的角色,为不同的子类提供通用的
您可能关注的文档
- 2013年老河口市中考地理适应题【和答案.doc
- 2013年老河口市中考适应性考试数学试卷和答案(解析版).doc
- 2013年四川高考数学(理)试题(真题和答案)word版.doc
- 2013年数学高考总复习重点精品课件: 空间点、直线、平面之间位置关系 83张.ppt
- 2013年四川高考数学(理)试题(真题和答案)word版 2.doc
- 2013年苏州市中考数学模拟试卷和答案(word解析版).doc
- 2013广西公务员面试考前题型和解析9.doc
- 2013广东省湛江市中考数学试题和答案(Word解析版).doc
- 2013年温州市高三第一次适应性测试数学(理科)试卷和答案.doc
- 2013河南教师资格中学教育学模拟试卷和答案(10套).doc
- 第十一章 电流和电路专题特训二 实物图与电路图的互画 教学设计 2024-2025学年鲁科版物理九年级上册.docx
- 人教版七年级上册信息技术6.3加工音频素材 教学设计.docx
- 5.1自然地理环境的整体性 说课教案 (1).docx
- 4.1 夯实法治基础 教学设计-2023-2024学年统编版九年级道德与法治上册.docx
- 3.1 光的色彩 颜色 电子教案 2023-2024学年苏科版为了八年级上学期.docx
- 小学体育与健康 四年级下册健康教育 教案.docx
- 2024-2025学年初中数学九年级下册北京课改版(2024)教学设计合集.docx
- 2024-2025学年初中科学七年级下册浙教版(2024)教学设计合集.docx
- 2024-2025学年小学信息技术(信息科技)六年级下册浙摄影版(2013)教学设计合集.docx
- 2024-2025学年小学美术二年级下册人美版(常锐伦、欧京海)教学设计合集.docx
文档评论(0)