实验7面向对象编程_第1页
实验7面向对象编程_第2页
实验7面向对象编程_第3页
实验7面向对象编程_第4页
实验7面向对象编程_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

利用面向抽象的编程思想,计算各种柱体的体积。右图为该程序的 UML 类图。编写测试类 PillarTest,输出底为圆形、长方形、三角形的柱体体积。(在实验 3 的基础上修改)public abstract class Geometry private double r; Geometry(double r) this.r=r; /计算圆的面积 public double getArea(); /* *创建一个面向抽象类的的Pillar类 * */ public class Pillar private Geometry geometry; private double height; Pillar(Geometry geometry,double height) this.geometry=geometry; this.height=height; public double getVolumn() return geometry.getArea()*height; /* *创建一个Geometry的子类Rectangle * */ public class Rectangle extends Geometry double width; double height; Rectangle(double width,double height) this.width=width; this.height=height; Override public double getArea() return width*height; /* *创建一个Application计算柱体的体积 * */ public class Application public static void main(String args) Geometry geometry=new Rectangle(10.0,12.0); Pillar pillar=new Pillar(geometry,12.0); double volumn=pillar.getVolumn(); System.out.println(矩形底的柱体的体积:+volumn); 要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年薪领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:Public abstract earnings();子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。有一个Company类,该类用Employee数组作为成员,Employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。 package homework; class Company Employee em; /Employee作为成员 Company(Employee em) /构造函数 this.em=em; public static void main(String args) / TODO Auto-generated method stub Employee em=new Employee3; em0=new YearWorker(1,12000.0); /上转型对象 em1=new MonthWorker(11,8000.0); em2=new WeekWorker(40,1200.0); Company com=new Company(em); double sum; sum=em0.earnings()+em1.earnings()+em2.earnings(); System.out.println(总工资为:+sum); abstract class Employee abstract double earnings(); /抽象方法 class MonthWorker extends Employee int month; double monthSalary; MonthWorker(int m,double s) month=m; monthSalary=s; double earnings() return month*monthSalary; class WeekWorker extends Employee int week; double weekSalary; WeekWorker(int w,double s) week=w; weekSalary=s; double earnings() return week*weekSalary; class YearWorker extends Employee int year; double yearSalary; YearWorker(int y,double s) /工作年数,年工资 year=y; yearSalary=s; double earnings() /重写抽象方法 return year*yearSalary; 多态性是指统一的接口,不同的表现形式。在我们下面的例子中,有5个类。Game类是Football、Basketball、Popolong的父类,Games类使用前面4个类。Java根据动态绑定决定执行“更具体”的方法,即子类方法。1./Game.java2.3.package .uibe.oop;4.5.public class Game 6. protected void play()7. System.out.println(play game);8. 2.13./Football.java14.package .uibe.oop;15.16.public class Football extends Game 17.18. protected void play() 19. System.out.println(play football);20. super.play();21. 22. void f()23. play();24. 8.29./Basketball.java30.31.package .uibe.oop;32.33.public class Basketball extends Game34.35. protected void play() 36. System.out.println(play basketball);37. 1.42./Popolong.java43.44.package .uibe.oop;45.46.public class Popolong extends Game 47.48. protected void play() 49. System.out.println(play popolong);50. 4.55./Games.java56.57.package .uibe.oop;58.59.public class Games 60.61. public static void main(String args) 62. Game games = new Game10;63. games0 = new Basketball();64. games1 = new Football();65. games2 = new Popolong();66. 67. for(int i=0;igames.length;i+)68. if(gamesi!=null)69. gamesi.pl多态性是指统一的接口,不同的表现形式。在我们下面的例子中,有5个类。70.Game类是Football、Basketball、Popolong的父类,Games类使用前面4个类。71.Java根据动态绑定决定执行“更具体”的方法,即子类方法。72./Game.java73.74.package .uibe.oop;75.76.public class Game 77. protected void play()78. System.out.println(play game);79. 3.84./Football.java85.package .uibe.oop;86.87.public class Football extends Game 88.89. protected void play() 90. System.out.println(play football);91. super.play();92. 93. void f()94. play();95. 9.100./Basketball.java101.102.package .uibe.oop;103.104.public class Basketball extends Game105.106. protected void play() 107. System.out.println(play basketball);108. 12.113./Popolong.java114.115.package .uibe.oop;116.117.public class Popolong extends Game 118.119. protected void play() 120. System.out.println(play popolong);121. 25.126./Games.java127.128.package .uibe.oop;129.130.public class Games 131.132. public static void main(String args) 133. Game games = new Game10;134. ga

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论