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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

实验5内部类.docx

1、实验5内部类第一题.目的:验证内部类对象总与创建它的外部类对象关联 1第二题.定义一个Father类 1第三题.修改BankAccount 2第四题.拷贝自身重要数据 3第五题.连锁店问题 4第六题.修改外围类对象数据,影响内部类行为 4第七题.迭代器的局部类实现 6第一题参考答案 7第二题参考答案 7第三题参考答案 8第四题参考答案 10第五题参考答案 10第六题参考答案 11第七题参考答案 11实验五. 内部类,局部类,匿名类背景知识 内部类 内部类对象的创建 局部类 匿名类实验目的1. 掌握内部类,局部类,匿名类概念2. 学习定义内部类,局部类,匿名类。3. 学习创建内部类,局部类,匿名

2、类对象。实验内容和步骤第一题.目的:验证内部类对象总与创建它的外部类对象关联1. 定义一个类A,它有一个内部类B.2. 定义B的方法g,它访问A的数据成员。3. 定义A的方法f,它访问B的数据成员,注意必须指明那个内部类对象的数据成员。4. 确保类A有方法修改A的域。5. 确保类B有方法print,输出A的域。6. 定义类C,它只有一个main方法。在main方法体内创建A类对象a。a作为外围对象创建B类对象b; 7. 验证:每个内部类对象,都有一个相关联的外部类对象,就是创建它的外部类对象。方法:首先对象b调用print,打印a的域,然后修改a的域,最后b再次调用print,打印a的域,打印

3、结果应该与对a的修改一致,从而说明b与a相关联。第二题.定义一个Father类它包括:1. 内部类Son,表示儿子的类,包括a) 数据域phone,表示儿子电话号。b) 内部类构造方法c) CallFather方法,给父亲打电话,打印“xxx给yyy打电话”即可,xxx是儿子电话号,yyy是父亲电话号。2. 数据域phone,表示父亲电话号。3. Son数组,保存儿子。4. count,保存儿子个数5. 构造函数。6. 给儿子打电话的方法CallSon,打印“给xxx打电话”即可,xxx是儿子电话号。第三题.修改BankAccount给下面的BankAccount类添加一个方法,显示numbe

4、r,balance,lastaction。 然后写main方法,首先创建两个账户,然后执行存款,取款,转账动作,每个动作之后都显示账户信息。体会内部类的作用。public class BankAccount /银行帐号类定义 private long number;/帐号 private long balance;/余额/内部类对象.最后一次去银行的动作:存款,取款,转账 private Action lastaction; public class Action/内部类. private String act;/描述动作的串 private long amount;/存取的金额 Action

5、(String a,long amo)/构造函数,指出动作和金额 act=a; amount=amo; public void deposit(long amount)/存款 balance+=amount; /动作:deposit和金额 lastaction=new Action(deposit,amount); public void withdraw(long amount)/取款 balance-=amount;/动作:wuthdraw和金额 lastaction=new Action(wuthdraw,amount); public void transfer(BankAccount

6、 other,long amount)/转账 other.withdraw(amount); deposit(amount); lastaction=new Action(transfer,amount);/转入other.lastaction=other.new Action(transfer,amount); /转出 第四题.拷贝自身重要数据读如下java程序,回答问题。一个类希望能够拷贝对象自身的重要数据,以便在必要的时候恢复这个对象。为此该类有一个datacpy bak域,它保存重要数据的拷贝,其中datacpy是一个内部类。class data int i; String s; da

7、tacpy bak; public data(int j,String t)i=j;s=t; /内部类class datacpy int b; public datacpy() b=i; public void copy() bak=new datacpy(); public void resume() i=bak.b; public void change()i+; public void print()System.out.println(i+ +s);public class test_main public static void main(String args) data x=ne

8、w data(1,zzz); x.copy(); x.print(); x.change(); x.print(); x.resume(); x.print(); 1data类如何备份重要数据?2给出main方法每一语句的注释。3修改程序,使得也备份域s。4. 修改main方法,测试你的修改。5. 体会内部类的作用。第五题.连锁店问题尝试用内部类解决下面问题。 一个连锁店有许多分店,这些分店对外公布的商品价格是price*ratio,其中price是总店制定的统一价格,ratio是总店下发给各个分店的系数,各个分店的ratio值可能不同。为了简单起见,假设该连锁店只销售一种商品,从而只有一个价

9、格price。现在要求设计一个表示总店的类,它有一个内部类,表示分店。实现如下目标:1. 总店能够修改price,能够修改各个分店的ratio。2. 每创建内部类的一个对象就相当于开了一家分店,构造函数需要初始化该分店的ratio。3. 总店能够记住它的所有分店。4. 各个分店能够显示商品价格,即price*ratio。5. 写main方法,测试你的实现。第六题.修改外围类对象数据,影响内部类行为读懂如下代码。回答问题:interface addition int add(int x,int y);interface multiplication int multiply(int x,int

10、y);class outer int part=5; public multiplication f1() /局部类.它有一个方法,计算两个数的积+part。class local implements multiplication public int multiply(int x,int y)return x*y+part; return new local();/创建局部类对象,并返回 public addition f2() /匿名类,继承自Object,实现接口addition。addition sum=new addition() /实现接口addition。public int

11、add(int x,int y)return x+y+part; ; return sum; public class main_class public static void main(String args) outer out=new outer(); multiplication mul=out.f1(); outer out1=new outer(); multiplication mul1=out1.f1(); out.part=8; out1.part=10000; System.out.println(mul.multiply(10, 12); System.out.prin

12、tln(mul1.multiply(10, 12); addition add=out.f2(); System.out.println(add.add(10, 20); out.part=5; System.out.println(add.add(10, 20); 1. 在add和multiply的实现中,都使用了outer类的属性part,合法性是什么?2. 修改part之后,add和multiply的功能是否也改变?3. 程序输出是什么?与你的预想是否一致?第七题.迭代器的局部类实现以下是ArrayList的iterator方法的实现。它是使用局部类实现的。读懂这个方法的实现,然后尝试使

13、用匿名类重新实现它。/返回一个此List所有元素上的一个Iterator迭代器。public MyListIterator iterator() /局部类定义开始,它实现了Iterator接口,因此它的对象也是一个Iterator对象。 class Iter implements MyListIterator int pos;/数组list的下标。 Iter()pos=0;/局部类构造函数 /返回true如果还有下一个元素。 public boolean hasNext() return poscount; /返回迭代过程中的下一个元素 public Object next() return

14、listpos+; /从当前集群中删除最后一次调用next方法时返回的元素, /每调用一次next方法,至多调用一次remove方法。 public void remove() for(int j=pos-1;jcount-1;j+)listj=listj+1; count-; /局部类定义结束 Iter iter=new Iter(); return iter;第一题参考答案第二题参考答案class Father class Son String sonphone; Son(String p) sonphone=p; count+; void CallFather() System.out.

15、println(sonphone+给+phone+打电话号); String phone; Son sons=new Son10; int count=0; Father(String p)/外围类Father构造函数 phone=p; void giveBirth(String p)/生儿子的方法 sonscount=new Son(p); void CallSon(int i)/给儿子打电话的方法CallSon,打印“给xxx打电话”即可,xxx是儿子电话号。 System.out.println(给+sonsi.sonphone+打电话号); public class Test publ

16、ic static void main(Stringargs) Father f=new Father(father); f.giveBirth(son1); f.giveBirth(son2); f.giveBirth(son3); int c=f.count; System.out.println(c); for(int i=0;ic;i+)f.CallSon(i); for(int i=0;ic;i+)f.sonsi.CallFather(); 第三题参考答案下面是整个程序,增加部分用红字标出,红字粗体是重点public class Test public static void mai

17、n(Stringargs) BankAccount b1=new BankAccount(1,0); BankAccount b2=new BankAccount(2,10000); b1.deposit(1000); b1.print(); b2.withdraw(2000); b2.print(); b1.transfer(b2, 200); b1.print(); b2.print(); class BankAccount /银行帐号类定义 private long number;/帐号 private long balance;/余额 /内部类对象.最后一次去银行的动作:存款,取款,转

18、账 private Action lastaction; /构造函数 public BankAccount(long n,long a)number=n;balance=a; /内部类定义开始 private class Action private String act;/描述动作的串 private long amount;/存取的金额 Action(String a,long amo)/内部类构造函数,指出动作和金额 act=a; amount=amo; public String toString() return action:+act+ amount:+amount; /内部类定义

19、结束. public void deposit(long amount)/存款 balance+=amount; /动作:deposit和金额 lastaction=new Action(deposit,amount); public void withdraw(long amount)/取款 balance-=amount; /动作:wuthdraw和金额 lastaction=new Action(wuthdraw,amount); public void transfer(BankAccount other,long amount)/转账 other.withdraw(amount);

20、deposit(amount); lastaction=new Action(transfer_in,amount);/转入 other.lastaction=other.new Action(transfer_out,amount); /转出 public void print() System.out.println(number:+number); System.out.println(balance:+balance); System.out.println(action:+lastaction); System.out.println(); 第四题参考答案第五题参考答案class H

21、eadOffice /内部类定义开始 private class Branch private double ratio;/分店的折扣率 private Branch(double r)ratio=r;/分店构造函数 /显示分店售价,注意它可以访问外围类的price。 public void showPrice()System.out.println(price*ratio); /内部类定义结束 private double price;/总店价格 private Branch b=new Branch100;/保存已开设的分店 private int count=0;/已开设分店数目 pub

22、lic HeadOffice() public void setup(double r)/开设新分店 bcount+=new Branch(r); /设置第i个分店的ratio, /注意,外围类访问内部类的数据域,必须指明访问哪个内部类对象的数据域 public void setRatio(int i,double r)bi.ratio=r; public void setPrice(double r)price=r;/设置总店price public int getcount()return count;/返回分店个数 public void showPrice(int i)bi.showP

23、rice();/显示第i个分店的价格public class Test public static void main(Stringargs) HeadOffice hd=new HeadOffice(); hd.setPrice(200);/设置总店价格为200 hd.setup(0.9);/开三个分店,折扣分别为0.9,1,1.1 hd.setup(1); hd.setup(1.1); int c; c=hd.getcount();/有几家分店? for(int i=0;ic;i+)/显示各个分店价格 hd.showPrice(i); hd.setPrice(300);/重新设置总店价格为300 for(int i=0;ic;i+)/显示各个分店价格 hd.showPrice(i); 第六题参考答案第七题参考答案

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

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