类的高级特性抽象类接口等精.docx
- 文档编号:25261524
- 上传时间:2023-06-06
- 格式:DOCX
- 页数:14
- 大小:16.32KB
类的高级特性抽象类接口等精.docx
《类的高级特性抽象类接口等精.docx》由会员分享,可在线阅读,更多相关《类的高级特性抽象类接口等精.docx(14页珍藏版)》请在冰豆网上搜索。
类的高级特性抽象类接口等精
3、类的高级特性(抽象类、接口等)
引入:
本章继续深入学习面向对象思想。
正课:
1、抽象类
并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
用abstract修饰。
●定义抽象类:
abstractclass类名称
{
成员变量;
方法();//定义一般方法
abstract方法();//定义抽象方法
}
范例testabstract:
packagetestabstract;
publicabstractclassAnimal
{
publicAnimal(StringaType)
{
type=newString(aType);
}
publicStringtoString()
{
return"Thisisa"+type;
}
//Dummymethodtobeimplementedinthederivedclasses
publicabstractvoidsound();
privateStringtype;
}
packagetestabstract;
publicclassCatextendsAnimal
{
publicCat(StringaName)
{
super("Cat");//Callthebaseconstructor
name=aName;//Suppliedname
breed="Unknown";//Defaultbreedvalue
}
publicCat(StringaName,StringaBreed)
{
super("Cat");//Callthebaseconstructor
name=aName;//Suppliedname
breed=aBreed;//Suppliedbreed
}
//ReturnaStringfullofacat'sdetails
publicStringtoString()
{
returnsuper.toString()+"\nIt's"+name+"the"+breed;
}
//Amiaowingmethod
publicvoidsound()
{
System.out.println("Miiaooww");
}
privateStringname;//Nameofacat
privateStringbreed;//Catbreed
}
packagetestabstract;
publicclassDogextendsAnimal
{
publicDog(StringaName)
{
super("Dog");//Callthebaseconstructor
name=aName;//Suppliedname
breed="Unknown";//Defaultbreedvalue
}
publicDog(StringaName,StringaBreed)
{
super("Dog");//Callthebaseconstructor
name=aName;//Suppliedname
breed=aBreed;//Suppliedbreed
}
//Presentadog'sdetailsasastring
publicStringtoString()
{
returnsuper.toString()+"\nIt's"+name+"the"+breed;
}
//Abarkingmethod
publicvoidsound()
{
System.out.println("WoofWoof");
}
privateStringname;//NameofaDog
privateStringbreed;//Dogbreed
}
packagetestabstract;
publicclassDuckextendsAnimal
{
publicDuck(StringaName)
{
super("Duck");//Callthebaseconstructor
name=aName;//Suppliedname
breed="Unknown";//Defaultbreedvalue
}
publicDuck(StringaName,StringaBreed)
{
super("Duck");//Callthebaseconstructor
name=aName;//Suppliedname
breed=aBreed;//Suppliedbreed
}
//ReturnaStringfullofaduck'sdetails
publicStringtoString()
{
returnsuper.toString()+"\nIt's"+name+"the"+breed;
}
//Aquackingmethod
publicvoidsound()
{
System.out.println("Quackquackquack");
}
privateStringname;//Duckname
privateStringbreed;//Duckbreed
}
packagetestabstract;
classSpanielextendsDog
{
publicSpaniel(StringaName)
{
super(aName,"Spaniel");
}
}
packagetestabstract;
importjava.util.Random;
publicclassTestAbstract
{
publicstaticvoidmain(String[]args)
{
//Createanarrayofthreedifferentanimals
Animal[]theAnimals={
newDog("Rover","Poodle"),
newCat("Max","Abyssinian"),
newDuck("Daffy","Aylesbury"),
newSpaniel("Fido")
};
AnimalpetChoice;//Choiceofpet
Randomselect=newRandom();//Randomnumbergenerator
//Makefiverandomchoicesofpet
for(inti=0;i<5;i++)
{//Choosearandomanimalasapet
petChoice=theAnimals[select.nextInt(theAnimals.length)];
System.out.println("\nYourchoice:
\n"+petChoice);
petChoice.sound();//Getthepet'sreaction
}
}
}
2、接口
接口可以被用来实现类间多继承结构。
接口内部只能定义public的抽象方法和静态的、公有常量,因此所有的方法需要在子类中实现。
接口定义方法:
●[访问权限]interface接口名称[extends父接口名1,父接口名2,….]
{
//接口体程序
}
范例testinterface:
packagetestinterface;
publicclassCatimplementsPetOutput
{
publicCat(StringaName)
{
//super("Cat");//Callthebaseconstructor
name=aName;//Suppliedname
breed="Unknown";//Defaultbreedvalue
}
publicCat(StringaName,StringaBreed)
{
//super("Cat");//Callthebaseconstructor
name=aName;//Suppliedname
breed=aBreed;//Suppliedbreed
}
//ReturnaStringfullofacat'sdetails
publicStringtoString()
{
return"\nIt's"+name+"the"+breed;
}
//Amiaowingmethod
publicvoidsound()
{
System.out.println("Miiaooww");
}
privateStringname;//Nameofacat
privateStringbreed;//Catbreed
}
packagetestinterface;
publicclassDogimplementsPetOutput
{
publicDog(StringaName)
{
name=aName;//Suppliedname
breed="Unknown";//Defaultbreedvalue
}
publicDog(StringaName,StringaBreed)
{
name=aName;//Suppliedname
breed=aBreed;//Suppliedbreed
}
//Presentadog'sdetailsasastring
publicStringtoString()
{
return"It's"+name+"the"+breed;
}
//Abarkingmethod
publicvoidsound()
{
System.out.println("WoofWoof");
}
publicStringgetName()
{
returnname;
}
privateStringname;//Nameofadog
privateStringbreed;//Dogbreed
}
packagetestinterface;
publicclassDuckimplementsPetOutput
{
publicDuck(StringaName)
{
//super("Duck");//Callthebaseconstructor
name=aName;//Suppliedname
breed="Unknown";//Defaultbreedvalue
}
publicvoidlayEgg()
{
System.out.println("Egglaid");
}
publicDuck(StringaName,StringaBreed)
{
//super("Duck");//Callthebaseconstructor
name=aName;//Suppliedname
breed=aBreed;//Suppliedbreed
}
//ReturnaStringfullofaduck'sdetails
publicStringtoString()
{
return"\nIt's"+name+"the"+breed;
}
//Aquackingmethod
publicvoidsound()
{
System.out.println("Quackquackquack");
}
privateStringname;//Duckname
privateStringbreed;//Duckbreed
}
packagetestinterface;
publicinterfacePetOutput
{
voidsound();
}
packagetestinterface;
publicinterfaceRead1
{
voidreadNewspaper();
}
packagetestinterface;
//importtestabstract.Read;
classSpanielextendsDogimplementsRead1
{
publicSpaniel(StringaName)
{
super(aName,"Spaniel");
}
publicvoidreadNewspaper(){
System.out.println("IcanreadNewspaper");
}
}
packagetestinterface;
importjava.util.Random;
publicclassTestInterface
{
publicstaticvoidmain(String[]args)
{
//PetOutput[]thePets={
//Dog("Rover","Poodle"),
//newCat("Max","Abyssinian"),
//newDuck("Daffy","Aylesbury"),
//newSpaniel("Fido")
//};
//PetOutputpetChoice;
//Randomselect=newRandom();//Randomnumbergenerator
//for(inti=0;i<5;i++)
//{
//petChoice=thePets[select.nextInt(thePets.length)];
//System.out.println("\nYourchoice:
\n"+petChoice);
//petChoice.sound();
//}
Spaniels=newSpaniel("Fido");
s.sound();
s.readNewspaper();
}
}
3、特殊类
1.内部类
在一个类的内部还可以定义类,这就是内部类,也称为嵌套类。
2.关键字static
static关键字来声明类变量和类方法,格式如下:
类变量:
staticvariableTypevariableName;
类方法:
staticreturnTypeclassMethod([paramlist]){…}
3.关键字final
final类与普通类的使用几乎没有差别,只是它失去了被继承的特性。
小结:
请同学们课后结合笔记查找相关资料进行深入学习。
作业:
请同学们完成印发的练习。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 高级 特性 抽象 接口