当前位置: 首页 > 其他范文 > 其他范文

实验五-继承与接口

作者:yangyi | 发布时间:2021-01-29 01:00:38 收藏本文 下载本文

实验五 继承与接口 1.实验目的 1、掌握类的继承细节内容,包括子类的继承、子类对象创建、成员变量的继承与隐藏、方法的继承与重写 2、掌握重写的目的以及怎样使用 super 关键字 3、掌握上转型对象与接口回调技术 4、掌握类怎样实现接口,及面向接口的编程思想 2.实验内容 1、根据附录里的源代码,按照注释要求,完成代码填空,使程序能够运行得出结果。

1)实验 1 中国人与美国人 package boy;public class People { protected double weight ,height;public void speakHello(){ System.out.println("yayayaya");} public void averageHeight(){ height=173;System.out.println("average height:"+height);} public void averageWeight(){ weight=70;System.out.println("average weight:"+weight);} } package boy;public class ChinaPeople extends People{ public void speakHello(){ System.out.println("你好");} public void averageHeight(){ height=168.78;System.out.println("中国人的平均身高:"+height+"厘米");} public void averageWeight(){ weight=65;System.out.println("中国人的平均体重:"+weight+"公斤");}

//【代码 1】//重写 public void averageWeight()方法,输出:"中国人的平均体重:65公斤" public void chinaGongfu(){ System.out.println("坐如钟,站如松,睡如弓");} } package boy;public class AmericanPeople extends People{ public void speakHello(){ System.out.println("How do you do");} public void averageHeight(){ System.out.println("American"s average height:176cm");} //【代码 2】//重写 public void speakHello()方法,输出:"How do you do" //【代码 3】//重写 public void averageHeight()方法,输出:"American"s average height:176cm" public void averageWeight(){ weight=75;System.out.println("American"s average weight:"+weight+"kg");} public void americanBoxing(){ System.out.println("直拳、钩拳、组合拳");} } package boy;public class BeijingPeople extends ChinaPeople{ public void averageHeight(){ System.out.println("北京人的平均身高:172.5 厘米");} public void averageWeight(){ System.out.println("北京人的平均体重:70 公斤");} // 【代码 4】//重写 public void averageHeight()方法,输出:"北京人的平均身高:172.5厘米" //【代码 5】//重写 public void averageWeight()方法,输出:"北京人的平均体重:70公斤" public void beijingOpera(){ System.out.println("花脸、青衣、花旦和老生");} }

package boy;public class Example { public static void main(String[] args){ ChinaPeople chinaPeople = new ChinaPeople();AmericanPeople americanPeople = new AmericanPeople();BeijingPeople beijingPeople = new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();} } 你好 How do you do 你好 中国人的平均身高:168.78 厘米 American"s average height:176cm 北京人的平均身高:172.5 厘米 中国人的平均体重:65.0 公斤 American"s average weight:75.0kg 北京人的平均体重:70 公斤 坐如钟,站如松,睡如弓 直拳、钩拳、组合拳 花脸、青衣、花旦和老生 坐如钟,站如松,睡如弓 2)实验 2 银行与利息 package boy;public class Bank { int savedMoney;int year;double interest;double interestRate=0.29;

public double computerInterest(){ interest=year*interestRate*savedMoney;return interest;} public void setInterestRate(double rate){ interestRate = rate;} } package boy;public class ConstructionBank extends Bank{ double year;public double computerInterest(){ super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=super.computerInterest();//【代码 1】//super 调用隐藏的 computerInterest()方法 double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d 元存在建设银行%d 年零%d 天的利息:%f 元n",savedMoney,super.year,day,interest);return interest;} } package boy;public class BankOfDalian extends Bank{ double year;public double computerInterest(){ super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=super.computerInterest();//【代码 2】//super 调用隐藏的 computerInterest()方法 double dayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d 元存在大连银行%d 年零%d 天的利息:%f 元n",savedMoney,super.year,day,interest);return interest;} } package boy;

public class SaveMoney { public static void main(String[] args){ int amount=8000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=bank1.computerInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);double interest2=bank2.computerInterest();System.out.printf("两个银行利息相差%f 元n",interest2-interest1);} } 8000 元存在建设银行 8 年零 236 天的利息:2428.800000 元 8000 元存在大连银行 8 年零 236 天的利息:2466.560000 元 两个银行利息相差 37.760000 元 3)实验 3 面积之和 Geometry.java public abstract class Geometry { public abstract double getArea();} package boy;public class TotalArea { Geometry[] tuxing;double totalArea=0;public void setTuxing(Geometry[] t){ tuxing=t;} public double computerTatalArea(){ for(int i=0;i

} } package boy;public class Rect extends Geometry{ double a,b;Rect(double a,double b){ this.a=a;this.b=b;} public double getArea(){ return a*b;} }//【代码 1】//重写 getArea()方法 package boy;public class Circle extends Geometry{ double r;Circle(double r){ this.r=r;} public double getArea(){ return 3.14*r*r;}//【代码 2】//重写 getArea()方法 } package boy;public class MainClass { public static void main(String[] args){ Geometry[] tuxing=new Geometry[29];//有 29 个 Geometry 对象 for(int i=0;i

各种图形的面积之和:

58778.360000 4)实验 4 歌手大赛 package boy;public interface ComputerAverage { public double average(double x[]);} package boy;public class SongGame implements ComputerAverage{ public double average(double x[]){ int count=x.length;double aver=0,temp=0;for(int i=0;i2){ aver=aver/(count-2);}else{ aver=0;} return aver;} } package boy;public class School implements ComputerAverage{ public double average(double x[]){//【代码 1】//重写 public double

average(double x[])方法,返回数组 x[]的元素的算术平方 double sum=0;for(int i=0;i

} } package boy;public class CloudyLittleState implements WeatherState{ public void showState(){ System.out.print("少云,有时晴。");} } package boy;public class HeavyRainState implements WeatherState{ public void showState(){ //【代码 2】//重写 public void showState()方法 System.out.print("暴雨,有闪电。");} } package boy;public class LightRainState implements WeatherState{ public void showState(){ //【代码 2】//重写 public void showState()方法 System.out.print("小雨,转多云。");} } package boy;public interface WeatherState { public void showState();//重写 public void showState()方法 } package boy;public class WeatherForecast { public static void main(String[] args){ Weather weatherBeijing=new Weather();System.out.print("n 今天白天:");weatherBeijing.setState(new CloudyDayState());weatherBeijing.show();System.out.print("n 今天夜间:");weatherBeijing.setState(new LightRainState());weatherBeijing.show();System.out.print("n 转:");weatherBeijing.setState(new HeavyRainState());

weatherBeijing.show();System.out.print("n 明天白天:");weatherBeijing.setState(new LightRainState());weatherBeijing.show();System.out.print("n 明天夜间:");weatherBeijing.setState(new CloudyLittleState());weatherBeijing.show();} } 今天白天:少云,有时晴。

今天夜间:小雨,转多云。

转:暴雨,有闪电。

明天白天:小雨,转多云。

明天夜间:少云,有时晴。

2、设计编写程序完成以下任务。

1)根据要求修改实验 2,参照建设银行再编写一个商业银行(广发行),让程序输出 8000 元在广发行 8 年零 212 天的利息。

package boy;public class Bank { int savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest(){ interest=year*interestRate*savedMoney;return interest;} public void setInterestRate(double rate){ interestRate = rate;} } package boy;public class ConstructionBank extends Bank{ double year;public double computerInterest(){ super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=super.computerInterest();//【代码 1】//super 调用隐

藏的 computerInterest()方法 double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d 元存在建设银行%d 年零%d 天的利息:%f 元n",savedMoney,super.year,day,interest);return interest;} } package boy;public class SaveMoney { public static void main(String[] args){ int amount=8000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.212;bank1.setInterestRate(0.035);double interest1=bank1.computerInterest();} } 2)根据要求修改实验3,再增加一种几何图形(梯形),并让主类中的tuxing的某些元素是梯形的上转型对象。

package four1;public abstract class Geometry { public abstract double getArea();} package four1;public class TotalArea { Geometry[] tuxing;double totalArea=0;public void setTuxing(Geometry[] t){ tuxing=t;} public double computerTatalArea(){ for(int i=0;i

} //【代码 3】//用循环语句让 tuxing 的元素调用 getArea 方法,并将返回的值累加到 totalArea return totalArea;} } package four1;public class Trapezoid extends Geometry{ double x,y,z;Trapezoid(double x,double y,double z){ this.x=x;this.y=y;this.z=z;} public double getArea(){ return z*(x+y)/2;} } package four1;public class MainClass { public static void main(String[] args){ Trapezoid tuxing=new Trapezoid(4.6,4.6,7.9);//有 29 个 Geometry 对象 double s=tuxing.getArea();System.out.printf("梯形的面积为:n%f",s);} } 3)仿照实验5编写一个程序实现模拟水杯中的水在不同温度下可能出现的状态。

package four1;public class BoiledState implements WaterState{ public void showState(){ System.out.println("开水。");} } package four1;

public class IceState implements WaterState{ public void showState(){ System.out.print("结冰。");} } package four1;public class LittleWarmState implements WaterState{ public void showState(){ System.out.println("微凉。");} } package four1;public class WarmState implements WaterState{ public void showState(){ System.out.println("温水。");} //【代码 2】//重写 public void showState()方法 } package four1;public class Water { WaterState state;public void show(){ state.showState();} public void setState(WaterState s){ state=s;} } package four1;public class WaterForecast { public static void main(String[] args){ Water waterState=new Water();System.out.print("n 温度在 90-100 时水杯水的状态:");waterState.setState(new BoiledState());waterState.show();System.out.print("n 温度在 50--80 时水杯水的状态:");waterState.setState(new WarmState());waterState.show();

System.out.print("n 温度在 30-50 时水杯水的状态:");waterState.setState(new LittleWarmState());waterState.show();System.out.print("n 温度在 0 度下时水杯水的状态:");waterState.setState(new IceState());waterState.show();} } package four1;public interface WaterState { public void showState();} 实验结果 4)编写一个接口并创建两个实现该接口的类 A、B。A、B 类实现了接口的 f方法,A 类的 f 方法内容为计算 1!+3!+5!……+9!并返回结果,B 类的 f 方法内容为计算 2!+4!+6!……+10!也返回结果;再编一个执行类,执行类运行时要求通过接口回调方式用同一个对象实例分别调用 A 类的 f 方法和 B 类的 f方法。

package four1;public class A implements object { public int f(int a, int b){ int sum=0,temp=1;for(int i=a;i<=b;i=i+2){ for(int j=1;j<=i;j++){ temp*=j;} sum+=temp;temp=1;}

return sum;} } package four1;public class B implements object { public int f(int a, int b){ int sum=0,temp=1;for(int i=a;i<=b;i=i+2){ for(int j=1;j<=i;j++){ temp*=j;} sum+=temp;temp=1;} return sum;} } package four1;public interface object { int f(int a,int b);} package four1;public class Test { public static void main(String[] args){ A a=new A();B b=new B();int sum1=a.f(1, 9);int sum2=b.f(2, 10);System.out.println("1!+3!+5!……+9!的结果为:"+sum1);System.out.println("2!+4!+6!……+10!的结果为:"+sum2);} }

实验结果 5)(1)定义一个汽车类 Vehicle,要求如下:(知识点:类的继承 方法的覆盖)(a)属性包括:汽车品牌 brand(String 类型)、颜色 color(String 类型)和速度 speed(double 类型)。

(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为 0)。

(c)为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。

(d)定义一个一般方法 run(),用打印语句描述汽车奔跑的功能。

定义测试类 VehicleTest,在其 main 方法中创建一个品牌为“benz”、颜色为“black”的汽车。

(2)定义一个 Vehicle 类的子类轿车类 Car,要求如下:

(a)轿车有自己的属性载人数 loader(int 类型)。

(b)提供该类初始化属性的构造方法。

(c)重新定义 run(),用打印语句描述轿车奔跑的功能。

(d)定义测试类 Test,在其 main 方法中创建一个品牌为“Honda”、颜色为“red”,载人数为 2 人的轿车。

面向对象基础出过类似的,这个把继承引入了 package four1;public class Car extends Vehicle { int loader;public Car(String brand, String color, double speed,int l){ super(brand, color, speed);this.loader=l;} public void run(){ System.out.println("颜色为"+super.getColor()+"的"+super.getBrand()+"汽车的时速为:"+super.getSpeed());} } package four1;

public class Test { public static void main(String[] args){ Car c=new Car("Honda","red",300.0,20);c.run();} } package four1;public class Vehicle { private String brand="BMW";//汽车品牌 private String color="red";//汽车颜色 private double speed=0;//汽车时速 public String getColor(){ return color;} public void setColor(String color){ this.color = color;} public double getSpeed(){ return speed;} public void setSpeed(double speed){ this.speed = speed;} public String getBrand(){ return brand;} public Vehicle(String brand, String color, double speed){ this.brand=brand;this.color=color;this.speed=speed;} public void run(){ System.out.println(brand+"汽车的时速为:"+speed);} } package four1;

public class VehicleTest { public static void main(String[] args){ Vehicle v=new Vehicle("benz","black",200.0);v.run();} } 实验结果 6)Cola 公司的雇员分为以下若干类:(知识点:多态)(1)ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

方法:getSalary(int month)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励 100 元。

(2)SalariedEmployee :

ColaEmployee 的子类,拿固定工资的员工。

属性:月薪(3)HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出 160 小时的部分按照 1.5 倍工资发放。

属性:每小时的工资、每月工作的小时数(4)SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

属性:月销售额、提成率(5)定义一个类 Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类 TestCompany,在 main 方法,把若干各种类型的员工放在一个 ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

知识点:继承与多态 package four1;public class ColaEmployee { String name;//员工姓名 int month;//员工生日月份 public String getName(){

return name;} public void setName(String name){ this.name = name;} public int getMonth(){ return month;} public void setMonth(int month){ this.month = month;} int getSalary(int month){ if(month == this.month){ return 100;} else { return 0;} } } package four1;public class Company extends ColaEmployee{ void print(String name, double salary,int month){ String Name=name;double Salary=salary;int Month=month;System.out.println("员工"+Name+"在"+Month+"月"+"的薪资为:"+Salary);} } package four1;public class HourlyEmployee extends ColaEmployee { double hours;//每月工作的时间 double hsalary;//每小时的工资

Company c=new Company();public HourlyEmployee(String name,int month, double hours, double hsalary){ this.hours=hours;this.hsalary=hsalary;this.month=month;this.name=name;} public double getHours(){ return hours;} public void setHours(double hours){ this.hours = hours;} public double getHsalary(){ return hsalary;} public void setHsalary(double hsalary){ this.hsalary = hsalary;} public int getSalary(int month){ if(this.hours<=160){ this.hsalary=this.hsalary*this.hours+super.getSalary(month);c.print(name,hsalary,month);return 0;} else { this.hsalary=(this.hours-160)*this.hsalary*1.5+160*this.hsalary+super.getSalary(month);c.print(name,hsalary,month);return 0;} } } package four1;public class SalariedEmployee extends ColaEmployee { double salary;//员工固定薪资(按月)Company c=new Company();public SalariedEmployee(String name, int month, double salary){

this.name=name;this.month=month;this.salary=salary;} public int getSalary(int month){ this.salary=salary + super.getSalary(month);c.print(name,salary,month);return 0;} } package four1;public class SalesEmployee extends ColaEmployee{ double money;//月销售额 double ticheng;//提成率 Company c=new Company();public SalesEmployee(String name, int month, double money, double ticheng){ this.money=money;this.month=month;this.name=name;this.ticheng=ticheng;} public double getMoney(){ return money;} public void setMoney(double money){ this.money = money;} public double getTicheng(){ return ticheng;} public void setTicheng(double ticheng){ this.ticheng = ticheng;} public int getSalary(int month){ this.money= money + super.getSalary(month)+(money*ticheng);c.print(name,money,month);return 0;}

} package four1;public class SalesEmployee extends ColaEmployee{ double money;//月销售额 double ticheng;//提成率 Company c=new Company();public SalesEmployee(String name, int month, double money, double ticheng){ this.money=money;this.month=month;this.name=name;this.ticheng=ticheng;} public double getMoney(){ return money;} public void setMoney(double money){ this.money = money;} public double getTicheng(){ return ticheng;} public void setTicheng(double ticheng){ this.ticheng = ticheng;} public int getSalary(int month){ this.money= money + super.getSalary(month)+(money*ticheng);c.print(name,money,month);return 0;} } package four1;public class TestCompany { public static void main(String[] args){ ColaEmployee[] ce =new ColaEmployee[3];ce[0]=new SalariedEmployee("aa",9,3000.0);ce[1]=new HourlyEmployee("bb",3,2000.0,180);ce[2]=new SalesEmployee("cc",6,6000,0.3);ce[0].getSalary(2);ce[1].getSalary(2);

ce[2].getSalary(2);} } 实验结果 7)利用接口实现动态的创建对象:(知识点:接口)(1)创建 4 个类 1 苹果 2 香蕉 3 葡萄 4 园丁(2)在三种水果的构造方法中打印一句话.以苹果类为例 class apple { public apple(){ System.out.println(“创建了一个苹果类的对象”);} }(3)类图如下:

ApplePearOranges+create(): FruitGardener<<接口>>Fruit(4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果

中哪个类的对象。

运行结果如图:

package four1;public class apple implements Fruit { public void apple(){ System.out.println("创建了一个苹果类的对象");} public void banana(){ } public void pear(){ } } package four1;public class banana implements Fruit { public void banana(){ System.out.println("创建了一个香蕉类的对象");} public void apple(){ } public void pear(){ } }

package four1;public interface Fruit { void apple();void banana();void pear();} package four1;public class pear implements Fruit { public void pear(){ System.out.println("创建了一个梨子类的对象");} public void apple(){ } public void banana(){ } } package four1;import java.util.Scanner;public class Test { public static void main(String[] args){ Scanner input=new Scanner(System.in);System.out.println("请输入您要创建的类:");String attr=input.nextLine();if(attr.equals("banana")){ Fruit fruit=new banana();fruit.banana();} else if(attr.equals("apple")){ Fruit fruit = new apple();fruit.apple();} else if(attr.equals("pear")){ Fruit fruit =new pear();

fruit.pear();} else System.out.println("输入有误!");} } 实验结果 3.实验步骤 略 4.评分标准 1.A——内容功能完善,编程风格好,人机接口界面好; 2.B——内容功能完善,编程风格良好,人机接口界面良好; 3.C——完成必做内容; 4.D——能完成必做内容; 5.E——未按时完成必做内容,或者抄袭(雷同者全部为 E).参照书上实验按模版要求,将【代码】替换为 Java 程序代码,编写好完整的程序文档,最后运行得到的相关文件,把实验所得文件一起打包上交。(压缩包的文件名为:学号后三位和名字开头字母,如 109zhh.RAR|ZIP)

附录:

验 实验 1 中国人与美国人 模板代码 People.java public class People { protected double weight ,height;public void speakHello(){ System.out.println("yayayaya");} public void averageHeight(){ height=173;System.out.println("average height:"+height);} public void averageWeight(){ weight=70;System.out.println("average weight:"+weight);} } ChinaPeople.java public class ChinaPeople extends People{ public void speakHello(){ System.out.println("你好");} public void averageHeight(){ height=168.78;System.out.println("中国人的平均身高:"+height+"厘米");} //【代码 1】//重写 public void averageWeight()方法,输出:"中国人的平均体重:65 公斤" public void chinaGongfu(){ System.out.println("坐如钟,站如松,睡如弓");} } AmericanPeople.java public class AmericanPeople extends People{ //【代码 2】//重写 public void speakHello()方法,输出:"How do you do" // 【代码 3】//重写 public void averageHeight()方法,输出:"American"s average height:176cm"

public void averageWeight(){ weight=75;System.out.println("American"s average weight:"+weight+"kg");} public void americanBoxing(){ System.out.println("直拳、钩拳、组合拳");} } BeijingPeople.java public class BeijingPeople extends ChinaPeople{ //【代码 4】//重写 public void averageHeight()方法,输出:"北京人的平均身高:172.5 厘米" //【代码 5】//重写 public void averageWeight()方法,输出:"北京人的平均体重:70 公斤" public void beijingOpera(){ System.out.println("花脸、青衣、花旦和老生");} } Example.java public class Example { public static void main(String[] args){ ChinaPeople chinaPeople = new ChinaPeople();AmericanPeople americanPeople = new AmericanPeople();BeijingPeople beijingPeople = new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera();beijingPeople.chinaGongfu();} } 验 实验 2 银行与利息 模板代码

Bank.java public class Bank { int savedMoney;int year;double interest;double interestRate=0.29;public double computerInterest(){ interest=year*interestRate*savedMoney;return interest;} public void setInterestRate(double rate){ interestRate = rate;} } ConstructionBank.java public class ConstructionBank extends Bank{ double year;public double computerInterest(){ super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=//【代码 1】//super 调用隐藏的 computerInterest()方法 double dayInterest=day*0.0001*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d 元 存 在 建 设 银 行 %d 年 零 %d 天 的 利 息 :

%f 元n",savedMoney,super.year,day,interest);return interest;} } BankOfDalian.java public class BankOfDalian extends Bank{ double year;public double computerInterest(){ super.year=(int)year;double r=year-(int)year;int day=(int)(r*1000);double yearInterest=//【代码 2】//super 调用隐藏的 computerInterest()方法 double dayInterest=day*0.00012*savedMoney;interest=yearInterest+dayInterest;System.out.printf("%d 元 存 在 大 连 银 行 %d 年 零 %d 天 的 利 息 :

%f 元n",savedMoney,super.year,day,interest);return interest;

} } SaveMoney.java public class SaveMoney { public static void main(String[] args){ int amount=8000;ConstructionBank bank1=new ConstructionBank();bank1.savedMoney=amount;bank1.year=8.236;bank1.setInterestRate(0.035);double interest1=bank1.computerInterest();BankOfDalian bank2=new BankOfDalian();bank2.savedMoney=amount;bank2.year=8.236;bank2.setInterestRate(0.035);double interest2=bank2.computerInterest();System.out.printf("两个银行利息相差%f 元n",interest2-interest1);} } 验 实验 3 面积之和 模板代码 Geometry.java public abstract class Geometry { public abstract double getArea();} TotalArea.java public class TotalArea { Geometry[] tuxing;double totalArea=0;public void setTuxing(Geometry[] t){ tuxing=t;} public double computerTatalArea(){ for(int i=0;i

到 totalArea return totalArea;} } Rect.java public class Rect extends Geometry{ double a,b;Rect(double a,double b){ this.a=a;this.b=b;} public double getArea(){ return a*b;} }//【代码 1】//重写 getArea()方法 Trapezoid.java public class Trapezoid extends Geometry{ double x,y,z;Rect(double x,double y,double z){ this.x=x;this.y=y;This.z=z;} public double getArea(){ return z*(x+y)/2;} } Circle.java public class Circle extends Geometry{ double r;Circle(double r){ this.r=r;} public double getArea(){ return 3.14*r*r;}//【代码 2】//重写 getArea()方法 } MainClass.java public class MainClass { public static void main(String[] args){

Trapezoid tuxing=new Trapezoid(4.6,4.6,7.9);//有 29 个 Geometry 对象 double s=tuxing.getArea();System.out.print(“梯形的面积为:n%f”,s);Geometry[] tuxing=new Geometry[29];//有 29 个 Geometry 对象 for(int i=0;i

} if(count>2){ aver=aver/(count-2);}else{ aver=0;} return aver;} } School.java public class School implements ComputerAverage{ //【代码 1】//重写 public double average(double x[])方法,返回数组 x[]的元素的算术平方 } Estimator.java public class Estimator { public static void main(String[] args){ double a[]={};double b[]={};ComputerAverage computer;computer=new SongGame();double result=//【代码 2】//computer 调用 average(x[])方法,将数组 a 传递给参数 x System.out.printf("%n");System.out.printf("歌手最后得分:%5.3fn",result);computer=new School();result=//【代码 3】//computer 调用 average(x[])方法,将数组 b 传递给参数 x System.out.printf("学生平均体重:%-5.2f kg",result);} } 验 实验 5 天气预报 模板代码 WeatherState.java public interface WeatherState { public void showState();} Weather.java

public class Weather { WeatherState state;public void show(){ state.showState();} public void setState(WeatherState s){ state=s;} } CloudyLittleState.java public class CloudyLittleState implements WeatherState{ public void showState(){ System.out.print("少云,有时晴。");} } CloudyDayState.java public class CloudyDayState implements WeatherState{ //【代码 1】//重写 public void showState()方法 } HeavyRainState.java public class HeavyRainState implements WeatherState{ //【代码 2】//重写 public void showState()方法 } LightRainState.java public class LightRainState implements WeatherState{ //【代码 3】//重写 public void showState()方法 } WeatherForecast.java public class WeatherForecast { public static void main(String[] args){ Weather weatherBeijing=new Weather();System.out.print("n 今天白天:");weatherBeijing.setState(new CloudyDayState());weatherBeijing.show();

System.out.print("n 今天夜间:");weatherBeijing.setState(new LightRainState());weatherBeijing.show();System.out.print("n 转:");weatherBeijing.setState(new HeavyRainState());weatherBeijing.show();System.out.print("n 明天白天:");weatherBeijing.setState(new LightRainState());weatherBeijing.show();System.out.print("n 明天夜间:");weatherBeijing.setState(new CloudyLittleState());weatherBeijing.show();} }

实验08,接口

实验课,RJ45接口连线实验

实验五

实验二,继承机制实验报告

C,,实验五

本文标题: 实验五-继承与接口
链接地址:https://www.dawendou.com/fanwen/qitafanwen/427060.html

版权声明:
1.大文斗范文网的资料来自互联网以及用户的投稿,用于非商业性学习目的免费阅览。
2.《实验五-继承与接口》一文的著作权归原作者所有,仅供学习参考,转载或引用时请保留版权信息。
3.如果本网所转载内容不慎侵犯了您的权益,请联系我们,我们将会及时删除。

重点推荐栏目

关于大文斗范文网 | 在线投稿 | 网站声明 | 联系我们 | 网站帮助 | 投诉与建议 | 人才招聘 | 网站大事记
Copyright © 2004-2025 dawendou.com Inc. All Rights Reserved.大文斗范文网 版权所有