ImageVerifierCode 换一换
格式:DOCX , 页数:40 ,大小:1.09MB ,
资源ID:16475560      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/16475560.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验6继承与接口文档格式.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验6继承与接口文档格式.docx

1、 void setdata(String xm,int xh) this.xm=xm; this.xh=xh; public void print() System.out.println(xm+,+xh); (2) 编译源程序。(二)创建将被继承的类(1) 程序功能:通过EXP3_7类产生子类EXP3_8,其不仅具有父类的成员变量xm(姓名)、xh(学号),还定义了新成员变量xy(学院)、bj(bj)。在程序中调用了父类的print 方法,同时可以看出子类也具有该方法。(2) 程序源代码如下。class EXP3_8 extends EXP3_7 protected String xy; p

2、rotected String bj; public static void main(String args) EXP3_7 p1=new EXP3_7(); p1.setdata(李四,12321); p1.print(); EXP3_8 s1=new EXP3_8(); s1.setdata(张三,12345); s1.xy=山西大学计算机学院; s1.bj=2008级计算机科学与技术 s1.print(); System.out.println(s1.xm+s1.xy+s1.bj);(1) 编译并运行,结果如图3.7所示。图3.7(三)了解成员方法的覆盖方式(1) 编写覆盖Object

3、 类toString方法的程序文件EXP3_9.java,源代码如下。class Circle private int radius; Circle (int radius) this.radius=radius; /定义一个参数的构造方法 public int getRadius() return radius; public void setRadius(int radius) this.radius = radius; public double area() return Math.PI*radius*radius; public String toString() return Ra

4、dius:+getRadius()+ area:+area(); public class EXP3_9 public static void main(String args) Circle c=new Circle(10); System.out.println(n+c.toString();(2)编译并运行,结果如图3.8所示。图3.8(3)试着以Point类为例,尝试为Object类的clone()和equals()方法进行覆盖,Point类包含私有成员x,y,构造方法1(包含两个参数a,b),构造方法2(参数为Point p),clone方法,equals方法,toString方法。

5、用TestPoint类进行测试。class Point extends Object private int x; private int y; Point(int a,int b) this.x=a; this.y=b; Point(Point p) this.x=p.x; this.y=p.y; public int getX() return x; public void setX(int x) this.x = x; public int getY() return y; public void setY(int y) this.y = y; protected String clon

6、e() return this.x+ +this.y; public boolean equals (Point p) if(this.x=p.x&this.y=p.y) return true; else return false; public String toSting()x是:+this.x+ y是:public class TestPoint /* * param args */ public static void main(String args) / TODO Auto-generated method stub Point p1=new Point(1,2); Point

7、p2=new Point(1,2); Point p3=new Point(3,4);P1:+p1.getX()+p1.getY(); System.out.println( p1.equals(p2); System.out.println( p1.equals(p3); System.out.println(p3.clone(); System.out.println(p2.toSting(); System.out.println(p3.toSting();(4)编译并运行结果如图3.9图3.9(四)this、super和super()的使用程序功能:说明this、super 和supe

8、r()的用法。程序首先定义Point(点)类,然后创建点的子类Line(线)。最后通过LX3_10 类输出线段的长度。程序中通过super(a,b)调用父类Point 的构造方法为父类的x 和y 赋值。在子类Line 的setLine方法中,因为参数名和成员变量名相同,为给成员变量赋值,使用this 引用,告诉编译器是为当前类的成员变量赋值。在length 和toString 方法中使用父类成员变量时,使用super 引用,告诉编译器使用的是父类的成员变量。class Point1 protected int x, y; Point1(int a, int b) setPoint(a, b);

9、 public void setPoint(int a, int b) x=a; y=b;class Line extends Point1 Line(int a, int b) super(a, b); setLine(a, b); public void setLine(int x, int y) this.x=x+x; this.y=y+y; public double length() int x1=super.x, y1=super.y, x2=this.x, y2=this.y; return Math.sqrt(x2-x1) * (x2-x1) + (y2-y1) * (y2-y

10、1);直线端点: + super.x + + super.y + + x + + y + 直线长度: + this.length();public class EXP3_10 Line line=new Line(50, 50);+line.toString();(3)编译并运行,结果如图3.10。图3.10五)1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。该程序的模板代码如下:请将其补充完整并调试运行。class

11、People protected double weight,height; public void speakHello() System.out.println(yayawawa); public void averageHeight() height=173;average height:+height); public void averageWeight() weight=70;average weight:+weight);class ChinaPeople extends People 你好,吃了吗? height=165;“中国人的平均身高:+height+厘米 weight=

12、65;中国人的平均体重:+weight+公斤 public void chinaGongfu()坐如钟,站如松,睡如弓 class AmericanPeople extends PeopleHow do you do height=175; weight=67; public void americanBoxing()直拳+、钩拳 class BeijingPeople extends ChinaPeople 您好 height=168; weight=60; public void beijingOpera() System.out.println(Beijing Operapublic c

13、lass 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.averageHeig

14、ht(); americanPeople.averageHeight(); beijingPeople.averageHeight(); chinaPeople.averageWeight(); americanPeople.averageWeight(); beijingPeople.averageWeight(); chinaPeople.chinaGongfu(); americanPeople.americanBoxing(); beijingPeople.beijingOpera() ; beijingPeople.chinaGongfu();运行结果如图3.11所示:图3.112、

15、读懂下面模板代码,按要求补充程序并调试运行。掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。abstract class Employee public abstract double earnings();class YearWorker extends Employee public double earnings() return 60000;class MonthWorker extends Employee return 5000;class WeekWorker extends Employee return 1200;class Company Employee empl

16、oyee; double salaries=0; Company(Employee employee) this.employee=employee; public double salariesPay() salaries=0; for(int i=0;iemployee.length;i+) salaries+=employeei.earnings(); return salaries;public class Hardwork Employee employee=new Employee20;i+) if(i%3=0) employeei=new WeekWorker(); else i

17、f(i%3=1) employeei=new MonthWorker(); else if(i%3=2) employeei=new YearWorker(); Company company=new Company(employee);公司年工资总额:+company.salariesPay();编译并运行程序结果如图3.12图3.123、读懂下面模板代码,按要求补充程序并调试运行。掌握接口的定义及其实现方法,学习接口回调的运用方法。interface ComputerWeight public double computeWeight();class Television implemen

18、ts ComputerWeight public double computeWeight() return 30.5;class Computer implements ComputerWeight return 25; class WashMachine implements ComputerWeight return 45;class Car ComputerWeight goods; double totalWeights=0; Car(ComputerWeight goods) this.goods=goods; public double getTotalWeights() tot

19、alWeights=0;goods.length; totalWeights+=puteWeight(); return totalWeights;public class Road ComputerWeight goodsOne=new ComputerWeight50, goodsTwo=new ComputerWeight22 ;goodsOne.length; goodsOnei=new Television(); goodsOnei=new Computer(); goodsOnei=new WashMachine();goodsTwo.length; goodsTwoi=new T

20、elevision(); goodsTwoi=new Computer(); goodsTwoi=new WashMachine(); Car 大货车=new Car(goodsOne);大货车装载的货物重量:+大货车.getTotalWeights(); Car 小货车=new Car(goodsTwo);小货车装载的货物重量:+小货车.getTotalWeights();编译并运行结果如图3.13所示图3.13(六) 接口的实现与运用实验任务 :本实验的任务是设计和实现一个 Soundable 接口 , 该接口具有发声功能 , 同时还能够调节声音大小。 Soundable 接口的这些功能将

21、会由 3 种声音设备来具体实现 , 它们分别是收音机 Radio 、随身昕 Walkman 和手机 Mobilephone 。最后还要设计一个应用程序类来使用这些实现了 Soundable 接口的声音设备类。程序运行时 , 先询问用户想听哪种设备 , 然后程序就会按照该设备的工作方式来发出声音。实验步骤 :(1) 仔细阅读程序清单1-9, 并完成其中的代码1代码3。程序清单1-9 InterfaceTest.javaimport java.util.Scanner; interface Soundable public void increaseVolume( ); public void d

22、ecreaseVolume( ); public void stopSound( ); public void playSound( ); class Radio implements Soundable public void increaseVolume( ) 增大收音机音量 public void decreaseVolume( ) 减小收音机音量 public void stopSound( ) 关闭收音机 public void playSound( ) 收音机播放广播 class Walkman implements Soundable 增大随声听音量 System.out.pri

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

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