已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java上机31 掌握类的继承并应用,要求:(1)定义一个People类, 该类具有类型为double,访问权限是protected的成员变量:height和weight;以及方法:public void speakHello(), public void averageHeight()和public void averageWeight()。(2)定义一个ChinaPeople类, ,它是People的子类,新增了public void chinaGongfu()方法,并要求ChinaPeople类重写父类的public void speakHello(), public void averageHeight()和public void averageWeight()方法。(3)定义一个BeijingPeople类, ,它是ChinaPeople的子类,新增了public void beijingOpera()方法,并要求BeijingPeople类重写父类的public void speakHello(), public void averageHeight()和public void averageWeight()方法。(4)定义一个主类,在主类中用上述类生成对象以及使用对象。class People protected double height; protected double weight; public void speakHello() System.out.println(Hello,我是people类!); public void averageHeight() height=165; System.out.println(people平均身高为+height+厘米); public void averageWeight() weight=150; System.out.println(people平均体重为+weight+斤); class ChinaPeople extends People public void chinaGongfu() System.out.println(I like chinese gongfu!); public void speakHello() System.out.println(Hello,我是ChinaPeople类!); public void averageHeight() height=175; System.out.println(ChinaPeople平均身高为+height+厘米); public void averageWeight() weight=145; System.out.println(ChinaPeople平均体重为+weight+斤); class BeijingPeople extends ChinaPeople public void beijingOpera() System.out.println(我会唱京剧!); public void speakHello() System.out.println(Hello,我是BeijingPeople类!); public void averageHeight() height=168; System.out.println(BeijingPeople平均身高为+height+厘米); public void averageWeight() weight=148; System.out.println(BeijingPeople平均体重为+weight+斤); public class E31 public static void main(String args) People people=new People(); ChinaPeople chinapeople=new ChinaPeople(); BeijingPeople beijingpeople=new BeijingPeople(); people.speakHello(); people.averageHeight(); people.averageWeight(); chinapeople.chinaGongfu(); chinapeople.speakHello(); chinapeople.averageHeight(); chinapeople.averageWeight(); beijingpeople.chinaGongfu(); beijingpeople.beijingOpera(); beijingpeople.speakHello(); beijingpeople.averageHeight(); beijingpeople.averageWeight(); 2 掌握抽象类的应用,要求:定义抽象类Shape,有2个抽象方法为showArea()、showzc(),分别求出面积、周长并显示,定义其子类正方形类Square,圆类 Circle,定义各自类的属性,用showArea方法、showzc()方法求出各自的面积、周长。定义主类,并在main方法中构造2个对象,调用showArea方法、showzc()方法。abstract class Shapeabstract double showArea(); abstract double showzc();class Square extends Shapedouble a,b;Square(double a,double b) /构造方法this.a=a;this.b=b; double showArea()return (a*b); double showzc()return(a*b)*2;class Circle extends Shapedouble r;Circle(double r) /构造方法this.r=r; double showArea()return(3.14*r*r); double showzc() return 2*3.14*r;public class t2public static void main(String args) Square square=new Square(10,10); /有参构造Circle circle = new Circle(2);System.out.println(边长为”+a+”的正方形的面积为:”+square.showArea();System.out.println(正方形的周长为:+square.showzc();System.out.println(半径为”+r+”的圆的面积为:+circle.showArea();System.out.println(圆的周长为:+circle.showzc();3掌握接口的应用,要求:设计一个程序可以计算平面图形的面积。(1)使用interface关键字定义Shape接口,接口中包含一个求解面积的方法;(2)使用extends从接口Shape派生出接口Shape2D,并为接口Shape2D添加一个求解周长的方法定义; (3)编写一个正方形类Square,该类实现Shape2D接口; (4)定义一个Scaleable接口,该接口中定义一个scale(double proportion)方法;(5) 编写一个可缩放的正方形SquareScaleable类,该类继承于Square类同时实现Scaleable接口;(6)定义主类,在主类中生成SquareScaleable类的若干对象,并计算其缩放前后的面积和周长。interface Shapedouble showArea();interface Shape2D extends Shapedouble showzc();class Square implements Shape2Ddouble a;Square (double a)this.a = a;public double showArea() return a*a;public double showzc() return 4*a;interface Scaleablevoid scale(double proportion);class SquareScaleable extends Square implements ScaleableSquareScaleable(double a) super(a);public void scale(double proportion) a=a*proportion;public class t3 public static void main(String args)SquareScaleable square = new SquareScaleable(3);System.out.println(square边
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 南京采购计算机管理制度
- 沈阳医学院《国际经济法》2025-2026学年期末试卷
- 上海中华职业技术学院《跨境电商理论与务实》2025-2026学年期末试卷
- 泰州学院《金融理论与实务》2025-2026学年期末试卷
- 徐州医科大学《供应链管理》2025-2026学年期末试卷
- 上海对外经贸大学《经济学专业导论》2025-2026学年期末试卷
- 上海公安学院《内科护理》2025-2026学年期末试卷
- 朔州陶瓷职业技术学院《中药炮制学》2025-2026学年期末试卷
- 上海海关学院《工程数学》2025-2026学年期末试卷
- 沈阳体育学院《旅游资源开发与管理》2025-2026学年期末试卷
- 五类人员笔试真题及答案
- 产品设计文档撰写规范案例示范版
- 数字孪生应用技术员国家职业标准(2024版)
- 中老年模特学习课件
- 2025年设备监理师职业资格考试(设备工程项目管理)历年参考题库含答案详解(5套)
- 2025年四川大学教育培训部业务岗工作人员招聘考前自测高频考点模拟试题附答案详解
- 2025年西安科技大学专职辅导员招聘笔试备考试题(含答案详解)
- 2026届湖南省岳阳市岳阳县达标名校中考物理押题试卷含解析
- 2025年4月自考《思想道德修养与法律基础03706》真题试题和答案
- 表皮样囊肿与皮脂腺囊肿超声鉴别诊断
- 私企请假管理办法细则
评论
0/150
提交评论