实验5.内部类(2)

2019-01-19 10:43

}

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 MyListIterator { int pos;//数组list的下标。 Iter(){pos=0;}//局部类构造函数 //返回true如果还有下一个元素。 public boolean hasNext() { return pos

}

}

//局部类定义结束 Iter iter=new Iter(); return iter;

第一题参考答案 第二题参考答案

class Father {

class Son { }

String phone;

Son sons[]=new Son[10]; int count=0;

Father(String p)//外围类Father构造函数 { }

void giveBirth(String p)//生儿子的方法 { }

void CallSon(int i)//给儿子打电话的方法CallSon,打印“给xxx打电话”即可,xxx

sons[count]=new Son(p); phone=p;

String sonphone; Son(String p) { }

void CallFather() {

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

}

sonphone=p; count++;

是儿子电话号。 { }

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

public class Test { }

public static void main(String[]args) { }

Father f=new Father(\); f.giveBirth(\); f.giveBirth(\); f.giveBirth(\);

int c=f.count;

System.out.println(c);

for(int i=0;i

for(int i=0;i

第三题参考答案

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

class BankAccount //银行帐号类定义 {

public static void main(String[]args) { }

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();

private long number;//帐号 private long balance;//余额

//内部类对象.最后一次去银行的动作:存款,取款,转账 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)//内部类构造函数,指出动作和金额 { }

public String toString() { }

return \act=a; amount=amo;

}//内部类定义结束.

public void deposit(long amount)//存款 { }

public void withdraw(long amount)//取款 { }

public void transfer(BankAccount other,long amount)//转账 {

other.withdraw(amount); deposit(amount);

lastaction=new Action(\,amount);//转入 other.lastaction=other.new

Action(\,amount); //转出

balance-=amount;

//动作:\和金额

lastaction=new Action(\,amount); balance+=amount;

//动作:\和金额

lastaction=new Action(\,amount);

} {

public void print()

System.out.println(\System.out.println(\System.out.println(\

}

}

System.out.println();

第四题参考答案 第五题参考答案

class HeadOffice {

//内部类定义开始

private class Branch { }

//内部类定义结束

private double price;//总店价格

private Branch[] b=new Branch[100];//保存已开设的分店 private int count=0;//已开设分店数目

public HeadOffice(){}

public void setup(double r)//开设新分店 { }

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

//注意,外围类访问内部类的数据域,必须指明访问哪个内部类对象的数据域 public void setRatio(int i,double r){b[i].ratio=r;} public void setPrice(double r){price=r;}//设置总店price public int getcount(){return count;}//返回分店个数

b[count++]=new Branch(r);

private double ratio;//分店的折扣率

private Branch(double r){ratio=r;}//分店构造函数

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

public void showPrice(){System.out.println(price*ratio);}

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

public class Test {

public static void main(String[]args) {

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;i

第六题参考答案 第七题参考答案


实验5.内部类(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:导游基础知识-中国民族民俗考试试题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: