System.out.print(i[x]+"、");
}
}
}
对照范例写出如下题目:
(1).创建GrandFather类,其中包括
a)属性:
姓名(name),年龄(age)
b)方法getGrandFather():
显示爷爷的信息
c)构造方法:
给爷爷的姓名,年龄赋值
(2).创建Father类,继承Grandfather类
a)属性:
除了继承爷爷的属性以外,还要增加自己的属性:
“职业”(occupation)
b)构造方法:
显式调用父类的构造方法,为Father类的姓名和年龄赋初始值。
再为职业输入初始值。
c)方法getFather():
显示父亲的相关信息
(3).创建ClassMain()类,定义main()方法,构造GrandFather类的对象和Father类的对象,并分别显示详细信息。
4、面向对象多态性
范例:
计算柱体的体积。
柱体体积计算公式是:
底部面积乘以高度
柱体底部分为圆形和矩形
要求:
通过抽象类和多态实现
packagecn.jit.demo;
abstractclassBottom{//父类抽象类底部
publicabstractdoublecalculatorArea();
}
classCircleBottomextendsBottom{//圆形底
/**
*半径
*/
privatedoubleradius;
@Override
publicdoublecalculatorArea(){
returnMath.PI*radius*radius;
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
publicCircleBottom(doubleradius){
super();
this.radius=radius;
}
}
classSquareBottomextendsBottom{//矩形底
privatedoublesideA;
privatedoublesideB;
publicdoublegetSideA(){
returnsideA;
}
publicvoidsetSideA(doublesideA){
this.sideA=sideA;
}
publicdoublegetSideB(){
returnsideB;
}
publicvoidsetSideB(doublesideB){
this.sideB=sideB;
}
@Override
publicdoublecalculatorArea(){
returnsideA*sideB;
}
publicSquareBottom(doublesideA,doublesideB){
super();
this.sideA=sideA;
this.sideB=sideB;
}
}
classZhuTi{//柱体类,完成形状的拼装
/**
*底
*/
privateBottombottom;
/**
*高
*/
privatedoubleheight;
/**
*计算体积
*@return
*/
publicdoublecalculatorVolumn(){
returnbottom.calculatorArea()*height;
}
publicZhuTi(Bottombottom,doubleheight){
super();
this.bottom=bottom;
this.height=height;
}
publicBottomgetBottom(){
returnbottom;
}
publicvoidsetBottom(Bottombottom){
this.bottom=bottom;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
publicvoidchangeBottom(Bottombottom){
this.bottom=bottom;
}
}
publicclassVolumnTest{//测试类
publicstaticvoidmain(String[]args){
Bottombottom=newCircleBottom(1.0);
doubleheight=1.0;
ZhuTizhuTi=newZhuTi(bottom,height);
doubleresult=zhuTi.calculatorVolumn();
System.out.println("圆柱体的体积是:
"+result);
bottom=newSquareBottom(1.0,1.0);
zhuTi.changeBottom(bottom);
result=zhuTi.calculatorVolumn();
System.out.println("立方体的体积是:
"+result);
}
}
范例:
接口和多态的应用,例如:
电脑上实现了USB接口,U盘,打印机等等也都实现了此标准。
interfaceUSB{
publicvoidstart();//开始工作
publicvoidstop();//结束工作
}
classComputer{
publicstaticvoidplugin(USBusb){
usb.start();
usb.stop();
}
};
classFlashimplementsUSB{
publicvoidstart(){
System.out.println("U盘开始工作。
");
}
publicvoidstop(){
System.out.println("U盘停止工作。
");
}
};
classPrintimplementsUSB{
publicvoidstart(){
System.out.println("打印机开始工作。
");
}
publicvoidstop(){
System.out.println("打印机停止工作。
");
}
};
publicclassInterPolDemo02{
publicstaticvoidmain(Stringargs[]){
Computer.plugin(newFlash());
Computer.plugin(newPrint());
}
};
对照范例,写出以下程序:
(1)乐器(Instrument)的标准为弹奏(play),而乐器类型分为:
钢琴(Piano)和小提琴(Violin),各种乐器的弹奏方法各不同。
编写代码实现不同乐器的弹奏。
(2)计算机模拟
四、实验结果与分析(程序运行结果及其分析)
2.(3)
classCircle{
privateintradius;
publicCircle(intr){
this.setRadius(r);
}
publicvoidsetRadius(intr){
radius=r;
}
publicintgetRadius(){
returnradius;
}
publicdoubleprintArea(){
return3.14*radius*radius;
}
publicdoubleprintChang(){
return2*3.14*radius;
}
}
publicclassTestCircle{
publicstaticvoidmain(Stringargs[])
{
Circlec1=newCircle(6);
System.out.println("面积为"+c1.printArea());
System.out.println("周长为"+c1.printChang());
}
}
3.
classGrandFather{
privateStringname;
privateintage;
publicGrandFather(Stringname,intage){
this.setName(name);
this.setAge(age);
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(intage){
this.age=age;
}
publicStringgetName(){
returnname;
}
publicintgetAge(){
returnage;
}
publicStringgetGrandFather(){
return"爷爷的姓名:
"+getName()+""+"年龄:
"+getAge();
}
}
classFatherextendsGrandFather{
privateStringoccupation;
publicFather(Stringname,intage,Stringoccupation){
super(name,age);
this.setOccupation(occupation);
}
publicvoidsetOccupation(Stringoccupation){
this.occupation=occupation;
}
publicStringgetOccupation(){
returnoccupation;
}
publicStringgetFather(){
return"爸爸的姓名:
"+getName()+""+"年龄:
"+getAge()+""+"职业:
"+getOccupation();
}
}
publicclassClassMain{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
GrandFathergf=newGrandFather("wuzongyao",78);
System.out.println(gf.getGrandFather());
Fatherf=newFather("wushengguang",48,"工人");
System.out.println(f.getFather());
}
}
4.
(1)
abstractclassInstrument{
abstractvoidplay();