实验5内部类.docx

上传人:b****7 文档编号:10208017 上传时间:2023-02-09 格式:DOCX 页数:17 大小:20.10KB
下载 相关 举报
实验5内部类.docx_第1页
第1页 / 共17页
实验5内部类.docx_第2页
第2页 / 共17页
实验5内部类.docx_第3页
第3页 / 共17页
实验5内部类.docx_第4页
第4页 / 共17页
实验5内部类.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

实验5内部类.docx

《实验5内部类.docx》由会员分享,可在线阅读,更多相关《实验5内部类.docx(17页珍藏版)》请在冰豆网上搜索。

实验5内部类.docx

实验5内部类

第一题.目的:

验证内部类对象总与创建它的外部类对象关联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方法,首先创建两个账户,然后执行存款,取款,转账动作,每个动作之后都显示账户信息。

体会内部类的作用。

publicclassBankAccount//银行帐号类定义

{

privatelongnumber;//帐号

privatelongbalance;//余额

//内部类对象.最后一次去银行的动作:

存款,取款,转账

privateActionlastaction;

publicclassAction//内部类.

{

privateStringact;//描述动作的串

privatelongamount;//存取的金额

Action(Stringa,longamo)//构造函数,指出动作和金额

{

act=a;

amount=amo;

}

}

publicvoiddeposit(longamount)//存款

{

balance+=amount;

//动作:

"deposit"和金额

lastaction=newAction("deposit",amount);

}

publicvoidwithdraw(longamount)//取款

{

balance-=amount;

//动作:

"wuthdraw"和金额

lastaction=newAction("wuthdraw",amount);

}

publicvoidtransfer(BankAccountother,longamount)//转账

{

other.withdraw(amount);

deposit(amount);

lastaction=newAction("transfer",amount);//转入

other.lastaction=other.new

Action("transfer",amount);//转出

}

}

第四题.拷贝自身重要数据

读如下java程序,回答问题。

一个类希望能够拷贝对象自身的重要数据,以便在必要的时候恢复这个对象。

为此该类有一个datacpybak域,它保存重要数据的拷贝,其中datacpy是一个内部类。

classdata

{

inti;

Strings;

datacpybak;

publicdata(intj,Stringt){i=j;s=t;}

//内部类

classdatacpy

{

intb;

publicdatacpy()

{

b=i;

}

}

publicvoidcopy()

{

bak=newdatacpy();

}

publicvoidresume()

{

i=bak.b;

}

publicvoidchange(){i++;}

publicvoidprint()

{System.out.println(i+""+s);}

}

publicclasstest_main

{

publicstaticvoidmain(String[]args)

{

datax=newdata(1,"zzz");

x.copy();

x.print();

x.change();

x.print();

x.resume();

x.print();

}

}

1.data类如何备份重要数据?

2.给出main方法每一语句的注释。

3.修改程序,使得也备份域s。

4.修改main方法,测试你的修改。

5.体会内部类的作用。

 

第五题.连锁店问题

尝试用内部类解决下面问题。

一个连锁店有许多分店,这些分店对外公布的商品价格是price*ratio,其中price是总店制定的统一价格,ratio是总店下发给各个分店的系数,各个分店的ratio值可能不同。

为了简单起见,假设该连锁店只销售一种商品,从而只有一个价格price。

现在要求设计一个表示总店的类,它有一个内部类,表示分店。

实现如下目标:

1.总店能够修改price,能够修改各个分店的ratio。

2.每创建内部类的一个对象就相当于开了一家分店,构造函数需要初始化该分店的ratio。

3.总店能够记住它的所有分店。

4.各个分店能够显示商品价格,即price*ratio。

5.写main方法,测试你的实现。

第六题.修改外围类对象数据,影响内部类行为

读懂如下代码。

回答问题:

interfaceaddition

{

intadd(intx,inty);

}

interfacemultiplication

{

intmultiply(intx,inty);

}

classouter

{

intpart=5;

publicmultiplicationf1()

{

//局部类.它有一个方法,计算两个数的积+part。

classlocalimplementsmultiplication

{

publicintmultiply(intx,inty){returnx*y+part;}

}

returnnewlocal();//创建局部类对象,并返回

}

publicadditionf2()

{

//匿名类,继承自Object,实现接口addition。

additionsum=newaddition()

{

//实现接口addition。

publicintadd(intx,inty){returnx+y+part;}

};

returnsum;

}

}

publicclassmain_class

{

publicstaticvoidmain(String[]args)

{

outerout=newouter();

multiplicationmul=out.f1();

outerout1=newouter();

multiplicationmul1=out1.f1();

out.part=8;

out1.part=10000;

System.out.println(mul.multiply(10,12));

System.out.println(mul1.multiply(10,12));

additionadd=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迭代器。

publicMyListIteratoriterator()

{

//局部类定义开始,它实现了Iterator接口,因此它的对象也是一个Iterator对象。

classIterimplementsMyListIterator

{

intpos;//数组list的下标。

Iter(){pos=0;}//局部类构造函数

//返回true如果还有下一个元素。

publicbooleanhasNext()

{

returnpos

}

//返回迭代过程中的下一个元素

publicObjectnext()

{

returnlist[pos++];

}

//从当前集群中删除最后一次调用next方法时返回的元素,

//每调用一次next方法,至多调用一次remove方法。

publicvoidremove()

{

for(intj=pos-1;j

count--;

}

}

//局部类定义结束

Iteriter=newIter();

returniter;

}

}

第一题参考答案

第二题参考答案

classFather

{

classSon

{

Stringsonphone;

Son(Stringp)

{

sonphone=p;

count++;

}

voidCallFather()

{

System.out.println(sonphone+"给"+phone+"打电话号");

}

}

Stringphone;

Sonsons[]=newSon[10];

intcount=0;

Father(Stringp)//外围类Father构造函数

{

phone=p;

}

voidgiveBirth(Stringp)//生儿子的方法

{

sons[count]=newSon(p);

}

voidCallSon(inti)//给儿子打电话的方法CallSon,打印“给xxx打电话”即可,xxx是儿子电话号。

{

System.out.println("给"+sons[i].sonphone+"打电话号");

}

}

publicclassTest

{

publicstaticvoidmain(String[]args)

{

Fatherf=newFather("father");

f.giveBirth("son1");

f.giveBirth("son2");

f.giveBirth("son3");

intc=f.count;

System.out.println(c);

for(inti=0;i

for(inti=0;i

}

}

第三题参考答案

下面是整个程序,增加部分用红字标出,红字粗体是重点

publicclassTest

{

publicstaticvoidmain(String[]args)

{

BankAccountb1=newBankAccount(1,0);

BankAccountb2=newBankAccount(2,10000);

b1.deposit(1000);

b1.print();

b2.withdraw(2000);

b2.print();

b1.transfer(b2,200);

b1.print();

b2.print();

}

}

classBankAccount//银行帐号类定义

{

privatelongnumber;//帐号

privatelongbalance;//余额

//内部类对象.最后一次去银行的动作:

存款,取款,转账

privateActionlastaction;

//构造函数

publicBankAccount(longn,longa){number=n;balance=a;}

//内部类定义开始

privateclassAction

{

privateStringact;//描述动作的串

privatelongamount;//存取的金额

Action(Stringa,longamo)//内部类构造函数,指出动作和金额

{

act=a;

amount=amo;

}

publicStringtoString()

{

return"action:

"+act+"amount:

"+amount;

}

}//内部类定义结束.

publicvoiddeposit(longamount)//存款

{

balance+=amount;

//动作:

"deposit"和金额

lastaction=newAction("deposit",amount);

}

publicvoidwithdraw(longamount)//取款

{

balance-=amount;

//动作:

"wuthdraw"和金额

lastaction=newAction("wuthdraw",amount);

}

publicvoidtransfer(BankAccountother,longamount)//转账

{

other.withdraw(amount);

deposit(amount);

lastaction=newAction("transfer_in",amount);//转入

other.lastaction=other.new

Action("transfer_out",amount);//转出

}

publicvoidprint()

{

System.out.println("number:

"+number);

System.out.println("balance:

"+balance);

System.out.println("action:

"+lastaction);

System.out.println();

}

}

第四题参考答案

第五题参考答案

classHeadOffice

{

//内部类定义开始

privateclassBranch

{

privatedoubleratio;//分店的折扣率

privateBranch(doubler){ratio=r;}//分店构造函数

//显示分店售价,注意它可以访问外围类的price。

publicvoidshowPrice(){System.out.println(price*ratio);}

}

//内部类定义结束

privatedoubleprice;//总店价格

privateBranch[]b=newBranch[100];//保存已开设的分店

privateintcount=0;//已开设分店数目

publicHeadOffice(){}

publicvoidsetup(doubler)//开设新分店

{

b[count++]=newBranch(r);

}

//设置第i个分店的ratio,

//注意,外围类访问内部类的数据域,必须指明访问哪个内部类对象的数据域

publicvoidsetRatio(inti,doubler){b[i].ratio=r;}

publicvoidsetPrice(doubler){price=r;}//设置总店price

publicintgetcount(){returncount;}//返回分店个数

publicvoidshowPrice(inti){b[i].showPrice();}//显示第i个分店的价格

}

publicclassTest

{

publicstaticvoidmain(String[]args)

{

HeadOfficehd=newHeadOffice();

hd.setPrice(200);//设置总店价格为200

hd.setup(0.9);//开三个分店,折扣分别为0.9,1,1.1

hd.setup

(1);

hd.setup(1.1);

intc;

c=hd.getcount();//有几家分店?

for(inti=0;i

hd.showPrice(i);

hd.setPrice(300);//重新设置总店价格为300

for(inti=0;i

hd.showPrice(i);

}

}

第六题参考答案

第七题参考答案

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

当前位置:首页 > PPT模板 > 商务科技

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

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