实验三 继承和多态(精选)Word格式文档下载.docx

上传人:wj 文档编号:13050333 上传时间:2022-10-03 格式:DOCX 页数:7 大小:14.12KB
下载 相关 举报
实验三 继承和多态(精选)Word格式文档下载.docx_第1页
第1页 / 共7页
实验三 继承和多态(精选)Word格式文档下载.docx_第2页
第2页 / 共7页
实验三 继承和多态(精选)Word格式文档下载.docx_第3页
第3页 / 共7页
实验三 继承和多态(精选)Word格式文档下载.docx_第4页
第4页 / 共7页
实验三 继承和多态(精选)Word格式文档下载.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

实验三 继承和多态(精选)Word格式文档下载.docx

《实验三 继承和多态(精选)Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验三 继承和多态(精选)Word格式文档下载.docx(7页珍藏版)》请在冰豆网上搜索。

实验三 继承和多态(精选)Word格式文档下载.docx

publicStringShow(){

return"

横坐标为:

"

+this.x+"

,纵坐标为:

+this.y;

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstubPointa=newPoint(3.0,4.0);

System.out.println(a.Show());

publicclassCircleextendsPoint{

doubleRadius;

publicCircle(){super(0,0);

Radius=0;

publicCircle(doublex,doubley,doublez){

super(x,y);

Radius=z;

System.out.println("

圆心的"

+super.Show()+"

半径为:

+Radius);

+Radius;

publicStringPrintArea(){

圆的面积为:

+Math.PI*Radius*Radius);

+Math.PI*Radius*Radius;

//TODOAuto-generatedmethodstubCirclea=newCircle(3,4,5);

a.Show();

a.PrintArea();

2.编写一测试类,对其进行编译、运行。

结果如何?

如去掉语句“super.Show();

”,再看看运行结果。

理解程序中重载和多态性的运用。

完成以下步骤要求:

(1)设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。

(2)设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r,获取和设置r值的public方法、计算圆面积的public方法。

(3)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包含有表示圆柱体

高的protected类型的成员变量h、获取和设置h值的public方法、计算圆柱体体积的

public方法。

package实验三2;

publicclassPoint{protecteddoublex,y;

publicPoint(){

x=0;

y=0;

publicvoidsetx(doublea){

this.x=a;

publicvoidsety(doubleb){

this.y=b;

publicdoublegetx(){

returnthis.x;

publicdoublegety(){

returnthis.y;

//TODOAuto-generatedmethodstubPointa=newPoint();

a.setx(3.0);

a.sety(4.0);

+a.x+"

纵坐标为:

+a.y);

protecteddoubler;

r=0;

publicvoidsetr(doublez){

this.r=z;

publicdoublegetr(){

returnthis.r;

+Math.PI*r*r);

+Math.PI*r*r;

//TODO自动生成的方法存根Circlea=newCircle();

a.setx(3.0);

a.setr(5.0);

圆心坐标为:

+"

("

+a.getx()+"

"

+a.gety()+"

)"

+

+a.getr());

a.PrintArea();

publicclassCylinderextendsCircle{

protecteddoubleh;

publicvoidseth(doublez){

this.h=z;

publicdoublegeth(){

returnthis.h;

publicStringVolume(){

圆柱体的体积为:

+Math.PI*r*r*h);

+Math.PI*r*r*h;

//TODO自动生成的方法存根Cylindera=newCylinder();

a.seth(6.0);

圆柱的轴心坐标为:

+a.getx()

,半径为:

+a.getr()+"

,高为:

+a.geth());

a.Volume();

(4)建立若干个Cylinder对象,输出其轴心位置坐标、半径、高及其体积的值。

4.学校中有老师和学生两类人,而在职研究生既是老师又是学生,对学生的管理和对教师的管理在他们身上都有体现。

(1)设计两个信息管理接口StudentInterface和TeacherInterfaceo其中,

StudentInterface接口包括setFee方法和getFee方法,分别用于设置和获取学生的学费;

TeacherInterface接口包括setPay方法和getPay方法,分别用于设置和获取教师的工资。

(2)定义一个研究生类Graduate,实现StudentInterface接口和TeacherInterface

接口,它定义的成员变量有name(姓名)、sex(性别)、age(年龄)、fee(每学期学费)、

pay(月工资)。

(3)创建一个姓名为"

zhangsan"

的研究生,统计他的年收入和学费,如果收入减去学费不足2000元,则输出“providealoan”(需要贷款)信息。

package实验三4;

publicinterfaceStudentInterface{ //创建接口publicvoidsetFee(inttuition);

//设置学生的学费publicintgetFee();

//获取学生的学费

publicinterfaceTeacherInterface{

publicvoidsetPay(intsalary);

//设置教师的工资

publicintgetPay();

//获取教师的工资

publicclassGraduateimplementsStudentInterface,TeacherInterface{

privateStringname;

//研究生的姓名privateStringsex;

//研究生的性别privateintage;

//研究生的年龄

privateintfee;

//研究生每学期的学费

privateintpay;

//研究生的月工资

publicvoidsetName(Stringname){ //设置姓名

this.name=name;

publicStringgetName(){ //获取姓名

returnname;

publicvoidsetSex(Stringsex){ //设置性别

this.sex=sex;

publicStringgetSex(){ //获取性别

returnsex;

publicvoidsetAge(intage){ //设置年龄

this.age=age;

publicintgetAge(){ //获取年龄

returnage;

@Override

publicvoidsetPay(intsalary){

//实现TeacherInterface接口的方法设置研究生的月工资

//TODO自动生成的方法存根

this.pay=salary;

publicintgetPay(){

//实现TeacherInterface接口的方法获取研究生的月工资

returnthis.pay;

publicvoidsetFee(inttuition){

//实现StudentInte

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 金融投资

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1