java接口难点讲解.docx

上传人:b****4 文档编号:4387281 上传时间:2023-05-07 格式:DOCX 页数:18 大小:17.25KB
下载 相关 举报
java接口难点讲解.docx_第1页
第1页 / 共18页
java接口难点讲解.docx_第2页
第2页 / 共18页
java接口难点讲解.docx_第3页
第3页 / 共18页
java接口难点讲解.docx_第4页
第4页 / 共18页
java接口难点讲解.docx_第5页
第5页 / 共18页
java接口难点讲解.docx_第6页
第6页 / 共18页
java接口难点讲解.docx_第7页
第7页 / 共18页
java接口难点讲解.docx_第8页
第8页 / 共18页
java接口难点讲解.docx_第9页
第9页 / 共18页
java接口难点讲解.docx_第10页
第10页 / 共18页
java接口难点讲解.docx_第11页
第11页 / 共18页
java接口难点讲解.docx_第12页
第12页 / 共18页
java接口难点讲解.docx_第13页
第13页 / 共18页
java接口难点讲解.docx_第14页
第14页 / 共18页
java接口难点讲解.docx_第15页
第15页 / 共18页
java接口难点讲解.docx_第16页
第16页 / 共18页
java接口难点讲解.docx_第17页
第17页 / 共18页
java接口难点讲解.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java接口难点讲解.docx

《java接口难点讲解.docx》由会员分享,可在线阅读,更多相关《java接口难点讲解.docx(18页珍藏版)》请在冰点文库上搜索。

java接口难点讲解.docx

java接口难点讲解

1接口特点

/*

接口的特点:

A:

接口用关键字interface表示

interface接口名{}

B:

类实现接口用implements表示

class类名implements接口名{}

C:

接口不能实例化

那么,接口如何实例化呢?

按照多态的方式来实例化。

D:

接口的子类

a:

可以是抽象类。

但是意义不大。

b:

可以是具体类。

要重写接口中的所有抽象方法。

(推荐方案)

由此可见:

A:

具体类多态(几乎没有)

B:

抽象类多态(常用)

C:

接口多态(最常用)

*/

//定义动物培训接口

interfaceAnimalTrain{

publicabstractvoidjump();

}

//抽象类实现接口

abstractclassDogimplementsAnimalTrain{

}

//具体类实现接口

classCatimplementsAnimalTrain{

publicvoidjump(){

System.out.println("猫可以跳高了");

}

}

classInterfaceDemo{

publicstaticvoidmain(String[]args){

//AnimalTrain是抽象的;无法实例化

//AnimalTrainat=newAnimalTrain();

//at.jump();

AnimalTrainat=newCat();

at.jump();

}

}

2接口成员特点

/*

接口成员特点

成员变量;只能是常量,并且是静态的。

默认修饰符:

publicstaticfinal

建议:

自己手动给出。

构造方法:

接口没有构造方法。

成员方法:

只能是抽象方法。

默认修饰符:

publicabstract

建议:

自己手动给出。

所有的类都默认继承自一个类:

Object。

类Object是类层次结构的根类。

每个类都使用Object作为超类。

*/

interfaceInter{

publicintnum=10;

publicfinalintnum2=20;

publicstaticfinalintnum3=30;

//错误:

需要<标识符>

//publicInter(){}

//接口方法不能带有主体

//publicvoidshow(){}

//abstractvoidshow();//默认public

publicvoidshow();//默认abstract

}

//接口名+Impl这种格式是接口的实现类格式

/*

classInterImplimplementsInter{

publicInterImpl(){

super();

}

}

*/

classInterImplextendsObjectimplementsInter{

publicInterImpl(){

super();

}

publicvoidshow(){}

}

//测试类

classInterfaceDemo2{

publicstaticvoidmain(String[]args){

//创建对象

Interi=newInterImpl();

System.out.println(i.num);

System.out.println(i.num2);

//i.num=100;

//i.num2=200;

//System.out.println(i.num);//无法为最终变量num分配值

//System.out.println(i.num2);//无法为最终变量num2分配值

System.out.println(Inter.num);

System.out.println(Inter.num2);

System.out.println("--------------");

}

}

3接口类与类的关系

/*

类与类:

继承关系,只能单继承,可以多层继承。

类与接口:

实现关系,可以单实现,也可以多实现。

并且还可以在继承一个类的同时实现多个接口。

接口与接口:

继承关系,可以单继承,也可以多继承。

*/

interfaceFather{

publicabstractvoidshow();

}

interfaceMother{

publicabstractvoidshow2();

}

interfaceSisterextendsFather,Mother{

}

//classSonimplementsFather,Mother//多实现

classSonextendsObjectimplementsFather,Mother{

publicvoidshow(){

System.out.println("showson");

}

publicvoidshow2(){

System.out.println("show2son");

}

}

classInterfaceDemo3{

publicstaticvoidmain(String[]args){

//创建对象

Fatherf=newSon();

f.show();

//f.show2();//报错

Motherm=newSon();

//m.show();//报错

m.show2();

}

}

4接口猫狗案例

/*

猫狗案例,加入跳高的额外功能

分析:

从具体到抽象

猫:

姓名,年龄

吃饭,睡觉

狗:

姓名,年龄

吃饭,睡觉

由于有共性功能,所以,我们抽取出一个父类:

动物:

姓名,年龄

吃饭();

睡觉(){}

猫:

继承自动物

狗:

继承自动物

跳高的额外功能是一个新的扩展功能,所以我们要定义一个接口

接口:

跳高

部分猫:

实现跳高

部分狗:

实现跳高

实现;

从抽象到具体

使用:

使用具体类

*/

//定义跳高接口

interfaceJumpping{

//跳高功能

publicabstractvoidjump();

}

//定义抽象类

abstractclassAnimal{

//姓名

privateStringname;

//年龄

privateintage;

publicAnimal(){}

publicAnimal(Stringname,intage){

this.name=name;

this.age=age;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

//吃饭();

publicabstractvoideat();

//睡觉(){}

publicvoidsleep(){

System.out.println("睡觉觉了");

}

}

//具体猫类

classCatextendsAnimal{

publicCat(){}

publicCat(Stringname,intage){

super(name,age);

}

publicvoideat(){

System.out.println("猫吃鱼");

}

}

//具体狗类

classDogextendsAnimal{

publicDog(){}

publicDog(Stringname,intage){

super(name,age);

}

publicvoideat(){

System.out.println("狗吃肉");

}

}

//有跳高功能的猫

classJumpCatextendsCatimplementsJumpping{

publicJumpCat(){}

publicJumpCat(Stringname,intage){

super(name,age);

}

publicvoidjump(){

System.out.println("跳高猫");

}

}

//有跳高功能的狗

classJumpDogextendsDogimplementsJumpping{

publicJumpDog(){}

publicJumpDog(Stringname,intage){

super(name,age);

}

publicvoidjump(){

System.out.println("跳高狗");

}

}

classInterfaceTest{

publicstaticvoidmain(String[]args){

//定义跳高猫并测试

JumpCatjc=newJumpCat();

jc.setName("哆啦A梦");

jc.setAge(3);

System.out.println(jc.getName()+"---"+jc.getAge());

jc.eat();

jc.sleep();

jc.jump();

System.out.println("-----------------");

JumpCatjc2=newJumpCat("加菲猫",2);

System.out.println(jc2.getName()+"---"+jc2.getAge());

jc2.eat();

jc2.sleep();

jc2.jump();

//定义跳高狗并进行测试的事情自己完成。

}

}

5接口类老师学生案例

/*

老师和学生案例,加入抽烟的额外功能

分析:

从具体到抽象

老师:

姓名,年龄,吃饭,睡觉

学生:

姓名,年龄,吃饭,睡觉

由于有共性功能,我们提取出一个父类,人类。

人类:

姓名,年龄

吃饭();

睡觉(){}

抽烟的额外功能不是人或者老师,或者学生一开始就应该具备的,所以,我们把它定义为接口

抽烟接口。

部分老师抽烟:

实现抽烟接口

部分学生抽烟:

实现抽烟接口

实现:

从抽象到具体

使用:

具体

*/

//定义抽烟接口

interfaceSmoking{

//抽烟的抽象方法

publicabstractvoidsmoke();

}

//定义抽象人类

abstractclassPerson{

//姓名

privateStringname;

//年龄

privateintage;

publicPerson(){}

publicPerson(Stringname,intage){

this.name=name;

this.age=age;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicintgetAge(){

returnage;

}

publicvoidsetAge(intage){

this.age=age;

}

//吃饭();

publicabstractvoideat();

//睡觉(){}

publicvoidsleep(){

System.out.println("睡觉觉了");

}

}

//具体老师类

classTeacherextendsPerson{

publicTeacher(){}

publicTeacher(Stringname,intage){

super(name,age);

}

publicvoideat(){

System.out.println("吃大白菜");

}

}

//具体学生类

classStudentextendsPerson{

publicStudent(){}

publicStudent(Stringname,intage){

super(name,age);

}

publicvoideat(){

System.out.println("吃红烧肉");

}

}

//抽烟的老师

classSmokingTeacherextendsTeacherimplementsSmoking{

publicSmokingTeacher(){}

publicSmokingTeacher(Stringname,intage){

super(name,age);

}

publicvoidsmoke(){

System.out.println("抽烟的老师");

}

}

//抽烟的学生

classSmokingStudentextendsStudentimplementsSmoking{

publicSmokingStudent(){}

publicSmokingStudent(Stringname,intage){

super(name,age);

}

publicvoidsmoke(){

System.out.println("抽烟的学生");

}

}

classInterfaceTest2{

publicstaticvoidmain(String[]args){

//测试学生

SmokingStudentss=newSmokingStudent();

ss.setName("林青霞");

ss.setAge(27);

System.out.println(ss.getName()+"---"+ss.getAge());

ss.eat();

ss.sleep();

ss.smoke();

System.out.println("-------------------");

SmokingStudentss2=newSmokingStudent("刘意",30);

System.out.println(ss2.getName()+"---"+ss2.getAge());

ss2.eat();

ss2.sleep();

ss2.smoke();

//测试老师留给自己练习

}

}

6抽象类和接口的区别

抽象类和接口的区别:

A:

成员区别

抽象类:

成员变量:

可以变量,也可以常量

构造方法:

成员方法:

可以抽象,也可以非抽象

接口:

成员变量:

只可以常量

成员方法:

只可以抽象

B:

关系区别

类与类

继承,单继承

类与接口

实现,单实现,多实现

接口与接口

继承,单继承,多继承

C:

设计理念区别

抽象类被继承体现的是:

”isa”的关系。

抽象类中定义的是该继承体系的共性功能。

接口被实现体现的是:

”likea”的关系。

接口中定义的是该继承体系的扩展功能。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2