【问题标题】:how to set a counter for parent class without child class accessing the counter in Java?java - 如何在没有子类访问Java中的计数器的情况下为父类设置计数器?
【发布时间】:2013-09-25 21:05:09
【问题描述】:

我有以下代码,其中有一个static counter field 来跟踪创建了多少Parent 对象。当我创建子类的实例时,parent 的计数器也会增加,这是我不希望发生的。有什么帮助吗?

这里是代码

class Parent1 {
    private String name;
    private int id;
    public static int count=0;

    public Parent1(String name, int id){
        this.name=name;
        this.id=id;
        count++;
    }
}

class Child1 extends Parent1{
    private int age;

    public Child1(String name, int id, int age){
        super(name, id);
        this.age=age;
    }
}
public class App {
    public static void main(String[] args){
    Parent1 par= new Parent1("aa",5);
    Parent1 par2=new Parent1("bb",10);
    System.out.println(Parent1.count);
    Child1 chi1= new Child1("aa",5,4);
    Child1 chi2=new Child1("bb",5,10);
    System.out.println(Child1.count);
    }
}

The output is 
2
4

【问题讨论】:

  • @paulsm4 所以?这只会使 Child1.count 无效,当他创建一个孩子时它仍然会增加它
  • @paulsm4 那行不通。超构造函数仍将在 Child1 中调用,并且计数仍将递增。

标签: java inheritance static subclass super


【解决方案1】:

Parent1的构造函数中:

if (getClass() == Parent1.class)  // <--
    count++; 

【讨论】:

  • @Prateek 这不是不言自明吗?在Parent1 构造函数中,我们检查我们正在实例化的类是否具体为Parent1,而不是子类。
  • 我不熟悉反射因此我的问题
【解决方案2】:

这样做:

class Parent1 {
    private String name;
    private int id;
    public static int count=0;

    public Parent1(String name, int id) {
        this(name, id, true);
    } 

    protected Parent1(String name, int id, boolean incrementCount){
        this.name=name;
        this.id=id;
        if( incrementCount )
            count++;
    }
}

class Child1 extends Parent1{
    private int age;

    public Child1(String name, int id, int age){
        super(name, id, false);
        this.age=age;
    }
}

//....

Parent1 par= new Parent1("aa",5);
Parent1 par2=new Parent1("bb",10);

【讨论】:

  • 两者都更好,并让所有子类都调用您描述的那个,然后使用public Parent1(String name, int id ){ this( name, id, true ); }
【解决方案3】:

count--; 添加到子类构造函数中。

【讨论】:

    【解决方案4】:

    您可以使用以下模式来解决问题。

    class Parent1 {
        private String name;
        private int id;
        public static int count=0;
    
        public Parent1(String name, int id){
            this(name,id,true);
            count++;
        }
        public Parent1(String name, int id,boolean noIncrement){
            this.name=name;
            this.id=id;
        }
    }
    
    class Child1 extends Parent1{
        private int age;
    
        public Child1(String name, int id, int age){
            super(name, id,true);
            this.age=age;
        }
    }
    public class App {
        public static void main(String[] args){
        Parent1 par= new Parent1("aa",5);
        Parent1 par2=new Parent1("bb",10);
        System.out.println(Parent1.count);
        Child1 chi1= new Child1("aa",5,4);
        Child1 chi2=new Child1("bb",5,10);
        System.out.println(Child1.count);
        }
    }
    

    【讨论】:

    • 您需要在 child1 类的超级构造函数中设置为 false。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2011-03-18
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多