Java编程思想第八章的一个示例,为什么我运行无结果?

limoxin_ 2018-09-15 04:58:42
class Shared{
private int refcount = 0;
private static long counter = 0;
private final long id = counter++;
public Shared() {
System.out.println("Creating "+this);
}
public void addRef() {
refcount++;
}
protected void dispose() {
if(--refcount == 0)
System.out.println("Disposing "+this);
}
public String toString() {
return "Shared "+id;
}
}
class Composing{
private Shared shared;
private static long counter = 0;
private final long id = counter++;
public Composing(Shared shared) {
System.out.println("Creating "+this);
this.shared = shared;
this.shared.addRef();
}
protected void dispose() {
System.out.println("disposing "+this);
shared.dispose();
}
public String toString() {
return "Composing "+id;
}
}
public class ReferenceCounting {
public static void mian(String[]args) {
Shared shared = new Shared();
Composing [] composing = {new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared),};
for(Composing c:composing)
c.dispose();
}
}

代码中的this是什么含义?完全看不懂。。。。还有判断语句if(--refcount == 0)中的“--”是什么意思?拜托好心的大佬给个解答~ 多谢 多谢 多谢!
...全文
786 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
一个治疗术 2018-10-25
  • 打赏
  • 举报
回复
你需要分清 mian main
Silly-77 2018-10-24
  • 打赏
  • 举报
回复
引用 13 楼 q54244125 的回复:
[quote=引用 9 楼 qq_40064948 的回复:]
我就特别好奇 main错了还能继续写下去....
而且 a++ --a 执行顺序是不一样的。最主要的就是前面说的 你的main!!!

为啥不能继续写下去呢,main也只是一个方法而已呀,不是检查时异常;
main方法,是jvm运行程序的入口而已;[/quote]

0.0 有道理。。 脑海里直接想成检查时异常了
天涯若风 2018-10-23
  • 打赏
  • 举报
回复
程序运行的大概意思应该是,首先通过有参构造函数实例化几个对象后,对实例化的对象进行计数, 然后调用销毁方法咯; 另外: i = 0; a = i++; :表示先进行赋值操作,再进行+1操作,此时 a = 0; a = ++i; :表示先执行+1操作,再赋值给a,此时 a = 1;
天涯若风 2018-10-23
  • 打赏
  • 举报
回复
引用 9 楼 qq_40064948 的回复:
我就特别好奇 main错了还能继续写下去.... 而且 a++ --a 执行顺序是不一样的。最主要的就是前面说的 你的main!!!
为啥不能继续写下去呢,main也只是一个方法而已呀,不是检查时异常; main方法,是jvm运行程序的入口而已;
zhangphh 2018-10-21
  • 打赏
  • 举报
回复
先把main给改回来 运行结果如下 Creating Shared 0 Creating Composing 0 Creating Composing 1 Creating Composing 2 Creating Composing 3 Creating Composing 4 disposing Composing 0 disposing Composing 1 disposing Composing 2 disposing Composing 3 disposing Composing 4 Disposing Shared 0
Silly-77 2018-10-19
  • 打赏
  • 举报
回复
我就特别好奇 main错了还能继续写下去....
而且 a++ --a 执行顺序是不一样的。最主要的就是前面说的 你的main!!!
kingmax54212008 2018-10-19
  • 打赏
  • 举报
回复
main是程序的入口函数
kingmax54212008 2018-10-19
  • 打赏
  • 举报
回复
public static void mian(String[]args)
改为
public static void main(String[]args)
effsim 2018-10-17
  • 打赏
  • 举报
回复
a--,--a,a++,++a,基础知识啊
verejava 2018-09-27
  • 打赏
  • 举报
回复
Java 面向对象 之 引用传递 http://www.verejava.com/?id=16992763271038
爱码叔 2018-09-26
  • 打赏
  • 举报
回复
public static void mian(String[]args)
改为
public static void main(String[]args)
>>春风吹 2018-09-18
  • 打赏
  • 举报
回复
引用 2 楼 Surrin1999 的回复:
mian 你让虚拟机情何以堪
正解!
Surrin1999 2018-09-17
  • 打赏
  • 举报
回复
mian 你让虚拟机情何以堪
SunsetFeng 2018-09-15
  • 打赏
  • 举报
回复
首先输出是有的,你的main函数名字都是错的,this指的是当前对象,--refcount等效于refcount=refcount-1,在使用refcount,就是先减一,在使用。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧