当前位置: 技术问答>java相关
请问哪位大虾做过Thinking in Java 2/e的exercises? 求教!这是我能给的最大分值,抱歉
来源: 互联网 发布时间:2015-02-15
本文导语: 恕小弟愚昧,在本书的Chapter4里的exercise13各位大虾是怎么做的? 我是越想越复杂,好象牵涉到蛮多的Garbage Collector,但书里说的还是不够详细。 能否post出你们的做法? 不尽感激!!! | public class Tank { pr...
恕小弟愚昧,在本书的Chapter4里的exercise13各位大虾是怎么做的?
我是越想越复杂,好象牵涉到蛮多的Garbage Collector,但书里说的还是不够详细。
能否post出你们的做法?
不尽感激!!!
|
public class Tank
{
private final int FULL=100;
int contant;
public void fill(int c) {
if (c>0)
{
if (c+contant>FULL)
{
contant=FULL;
}
else
{
contant+=c;
}
}
System.out.println("current contant="+contant);
}
public void empty(int c) {
if (c>0)
{
if (contant-c>0)
{
contant-=c;
}
else
{
contant=0;
}
}
System.out.println("current contant="+contant);
}
protected void finalize()
throws Throwable {
if (contant!=0)
{
System.out.println("not null!");
System.out.println("emptyit force!");
empty(FULL);
}
else {
System.out.println("null!");
}
}
public static void main(String[] args)
{
Tank a=new Tank();
Tank b=new Tank();
a.fill(10);
a.empty(10);
a=null;
b.fill(10);
b=null;
System.gc();
}
}
{
private final int FULL=100;
int contant;
public void fill(int c) {
if (c>0)
{
if (c+contant>FULL)
{
contant=FULL;
}
else
{
contant+=c;
}
}
System.out.println("current contant="+contant);
}
public void empty(int c) {
if (c>0)
{
if (contant-c>0)
{
contant-=c;
}
else
{
contant=0;
}
}
System.out.println("current contant="+contant);
}
protected void finalize()
throws Throwable {
if (contant!=0)
{
System.out.println("not null!");
System.out.println("emptyit force!");
empty(FULL);
}
else {
System.out.println("null!");
}
}
public static void main(String[] args)
{
Tank a=new Tank();
Tank b=new Tank();
a.fill(10);
a.empty(10);
a=null;
b.fill(10);
b=null;
System.gc();
}
}