在从子类构造函数进行调用之前更改超类中定义的静态变量

问题描述 投票:-1回答:1

我有带有静态classA变量的PooledConnectionFactoryclassB扩展了A。

public class A{
    private static PooledConnectionFactory con;
    public A(){
        this("gets URL from defaults properties file");
    }
    public A(final String url){
       if(con==null){
         //Initialize pooled connection       
       }
//code for connection here
    }
}
public class B extends A {

    private PooledConnectionFactory conn;

    public B(String url){
        super(url);

    }


    public B(){
        super();
    }

}

我想将PooledConnectionFactory作为一个非静态变量,以便对于拾取的每个服务,分别对其进行初始化。由于它扩展了A,因此调用转到A的构造函数。

我应该在B类中进行哪些更改以实现此目标?

我有带有静态PooledConnectionFactory变量的classA。 classB扩展了A。公共类A {private static PooledConnectionFactory con; public A(){this(“从默认值获取URL ...

java inheritance constructor
1个回答
0
投票

使用单例而不是A构造函数

© www.soinside.com 2019 - 2024. All rights reserved.