java上机实验-类

上传人:第*** 文档编号:34244768 上传时间:2018-02-22 格式:DOC 页数:8 大小:155.50KB
返回 下载 相关 举报
java上机实验-类_第1页
第1页 / 共8页
java上机实验-类_第2页
第2页 / 共8页
java上机实验-类_第3页
第3页 / 共8页
java上机实验-类_第4页
第4页 / 共8页
java上机实验-类_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《java上机实验-类》由会员分享,可在线阅读,更多相关《java上机实验-类(8页珍藏版)》请在金锄头文库上搜索。

1、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(), publ

2、ic void averageHeight()和 public void averageWeight()方法。(3)定义一个 BeijingPeople类, ,它是 ChinaPeople的子类,新增了 public void beijingOpera()方法,并要求 BeijingPeople类重写父类的 public void speakHello(), public void averageHeight()和 public void averageWeight()方法。(4)定义一个主类,在主类中用上述类生成对象以及使用对象。class Peopleprotected double he

3、ight;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 Peoplepublic void china

4、Gongfu()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 Beijing

5、People extends ChinaPeoplepublic 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(Beijing

6、People平均体重为+weight+斤);public class E31public 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.speak

7、Hello();chinapeople.averageHeight();chinapeople.averageWeight();beijingpeople.chinaGongfu();beijingpeople.beijingOpera();beijingpeople.speakHello();beijingpeople.averageHeight();beijingpeople.averageWeight(); 2 掌握抽象类的应用,要求:定义抽象类 Shape,有 2个抽象方法为 showArea()、showzc(),分别求出面积、周长并显示,定义其子类正方形类 Square,圆类 Ci

8、rcle,定义各自类的属性,用 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()r

9、eturn(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+”的正方形的面积为:

10、”+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)编

11、写一个正方形类 Square,该类实现 Shape2D接口; (4)定义一个 Scaleable接口,该接口中定义一个 scale(double proportion)方法;(5) 编写一个可缩放的正方形 SquareScaleable类,该类继承于 Square类同时实现 Scaleable接口;(6)定义主类,在主类中生成 SquareScaleable类的若干对象,并计算其缩放前后的面积和周长。interface Shapedouble showArea();interface Shape2D extends Shapedouble showzc();class Square imple

12、ments 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=

13、a*proportion;public class t3 public static void main(String args)SquareScaleable square = new SquareScaleable(3);System.out.println(square边长+square.a+面积为:+square.showArea();System.out.println(square的周长为:+square.showzc();square.scale(2);System.out.println(square放大后面积为:+square.showArea();System.out.println(square放大后周长为:+square.showzc();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号