Java 父类和子类对象销毁实例
2013-12-02 14:32
471 查看
1)定义一个共享类Shared
使用long而不使用int,是为了防止溢出,refcount是共享类被其他类引用的次数,只要还有其他的类引用这个类,共享类就不会被GC回收。
package cn.javass.user;
public 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);
}
}
//重写toString方法
public String toString(){return "Shared"+id;};
}
2)定义一个组合类
package cn.javass.user;
public class Composing {
private Shared shared;
private static long counter=0;
private final long id=counter++;
public Composing(Shared shared) {
// TODO Auto-generated constructor stub
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; };
}
3)定义引用计数类
package cn.javass.user;
public class ReferenceCounting {
public static void main(String[] args) {
Shared shared=new Shared();
Composing[] composings={new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared)};
for(Composing c:composings)
c.dispose();
}
}
使用long而不使用int,是为了防止溢出,refcount是共享类被其他类引用的次数,只要还有其他的类引用这个类,共享类就不会被GC回收。
package cn.javass.user;
public 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);
}
}
//重写toString方法
public String toString(){return "Shared"+id;};
}
2)定义一个组合类
package cn.javass.user;
public class Composing {
private Shared shared;
private static long counter=0;
private final long id=counter++;
public Composing(Shared shared) {
// TODO Auto-generated constructor stub
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; };
}
3)定义引用计数类
package cn.javass.user;
public class ReferenceCounting {
public static void main(String[] args) {
Shared shared=new Shared();
Composing[] composings={new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared)};
for(Composing c:composings)
c.dispose();
}
}
相关文章推荐
- java中实例子类会有父类对象产生吗?如果不产生父类对象为什么子类有父类的属性和方法?
- java 父类访问子类对象的实例变量 继承过程中的执行顺序
- 在java中怎么理解“用instanceof操作符判断父类对象是否为子类的一个实例”?
- java基础问题----------TreeSet里面放对象,如果同时放入了父类和子类的实例对象
- 【Java面试题】42 TreeSet里面放对象,如果同时放入了父类和子类的实例对象,那比较时使用的是父类的compareTo方法,还是使用的子类的compareTo方法,还是抛异常!
- Java 中深层理解父类引用指向子类对象
- java子类对象在子类方法中使用父类属性 使用this和super都可以
- Java 父类引用指向子类对象
- Java 父类子类的对象初始化过程
- Java对象初始化步骤&&子类继承父类后变量与方法调用情况
- 个人学习-java-子类对象实例化
- 多态的三个条件: 1、要有继承 2、要有重写 3、父类引用指向子类对象----java
- java 访问子类对象的实例变量
- Java访问子类对象的实例变量
- 面向对象之父类变量指向子类实例对象分析
- java中父类对象引用指向子类对象
- 关于dao设计模式,父类对象实例化指向子类,xxxDao xxx = new xxxDaoImpl的理解
- Java反射:通过父类对象调用子类方法
- Java父类子类的对象初始化过程
- Java 父类的构造函数执行要早于子类的实例变量初始化