实验5.内部类.doc_第1页
实验5.内部类.doc_第2页
实验5.内部类.doc_第3页
实验5.内部类.doc_第4页
实验5.内部类.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第一题.目的:验证内部类对象总与创建它的外部类对象关联1第二题.定义一个Father类1第三题.修改BankAccount2第四题.拷贝自身重要数据3第五题.连锁店问题4第六题.修改外围类对象数据,影响内部类行为4第七题.迭代器的局部类实现6第一题参考答案7第二题参考答案7第三题参考答案8第四题参考答案10第五题参考答案10第六题参考答案11第七题参考答案11实验五. 内部类,局部类,匿名类背景知识内部类内部类对象的创建局部类匿名类实验目的1. 掌握内部类,局部类,匿名类概念2. 学习定义内部类,局部类,匿名类。3. 学习创建内部类,局部类,匿名类对象。实验内容和步骤第一题.目的:验证内部类对象总与创建它的外部类对象关联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的域,打印结果应该与对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类添加一个方法,显示number,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(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 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 dataint i;String s;datacpy bak;public data(int j,String t)i=j;s=t;/内部类class datacpyint 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=new 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值可能不同。为了简单起见,假设该连锁店只销售一种商品,从而只有一个价格price。现在要求设计一个表示总店的类,它有一个内部类,表示分店。实现如下目标:1. 总店能够修改price,能够修改各个分店的ratio。2. 每创建内部类的一个对象就相当于开了一家分店,构造函数需要初始化该分店的ratio。3. 总店能够记住它的所有分店。4. 各个分店能够显示商品价格,即price*ratio。5. 写main方法,测试你的实现。第六题.修改外围类对象数据,影响内部类行为读懂如下代码。回答问题:interface additionint add(int x,int y);interface multiplicationint multiply(int x,int y);class outerint part=5;public multiplication f1()/局部类.它有一个方法,计算两个数的积+part。class local implements multiplicationpublic 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 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.println(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方法的实现。它是使用局部类实现的。读懂这个方法的实现,然后尝试使用匿名类重新实现它。/返回一个此List所有元素上的一个Iterator迭代器。public MyListIterator iterator()/局部类定义开始,它实现了Iterator接口,因此它的对象也是一个Iterator对象。class Iter implements MyListIteratorint pos;/数组list的下标。Iter()pos=0;/局部类构造函数/返回true如果还有下一个元素。public boolean hasNext()return poscount;/返回迭代过程中的下一个元素public Object next()return 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 Fatherclass SonString sonphone;Son(String p)sonphone=p;count+;void CallFather()System.out.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 public 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 main(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;/余额/内部类对象.最后一次去银行的动作:存款,取款,转账private Action lastaction; /构造函数public BankAccount(long n,long a)number=n;balance=a;/内部类定义开始private class Actionprivate String act;/描述动作的串private long amount;/存取的金额Action(String a,long amo)/内部类构造函数,指出动作和金额act=a;amount=amo;public String toString()return action:+act+ amount:+amount;/内部类定义结束.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);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 HeadOffice/内部类定义开始private class Branchprivate 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;/保存已开设的

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论