Java派生类不会自动创建实例

发布于 2024-11-02 07:14:02 字数 2462 浏览 0 评论 0原文

我有一个基本的抽象记录器类,它有一个实例变量,我希望在创建代码时由派生类自动设置该实例变量。所以这是基类:

abstract public class CLog 
{
    /** Maintains the call stack level for each thread */
    private static HashMap<Integer, Integer> callStackLevel = new HashMap<Integer, Integer>();

    /** Static instance to be set by the derived class */
    private static CLog instance = null;

    /** Logs in verbose */
    public static void v(String message) { if(instance != null) instance.verbose(getMessage(message)); }
    /** Logs in debug */
    public static void d(String message) { if(instance != null) instance.debug(getMessage(message)); }
    /** Logs in informational */
    public static void i(String message) { if(instance != null) instance.info(getMessage(message)); }
    /** Logs in warning */
    public static void w(String message) { if(instance != null) instance.warn(getMessage(message)); }
    /** Logs in error */
    public static void e(String message) { if(instance != null) instance.error(getMessage(message)); }

    /**
     * Calculates the message (with header)
     */
    private static String getMessage(String message)
    {
        ...
    }

    /** Constructor sets instance */
    protected CLog() { instance = this; }

    /** Logs in verbose */
    protected abstract void verbose(String message);
    /** Logs in debug */
    protected abstract void debug(String message);
    /** Logs in informational */
    protected abstract void info(String message);
    /** Logs in warning */
    protected abstract void warn(String message);
    /** Logs in error */
    protected abstract void error(String message);
}

我为 android 记录器创建了派生类。我希望它自动调用构造函数,但似乎这不起作用,因为我的所有日​​志记录功能都没有产生任何结果。

public class AndroidLog extends CLog 
{
    protected static AndroidLog derived = new AndroidLog();

    @Override
    protected void debug(String message) {
        Log.d("Crystal", message);
    }

    @Override
    protected void error(String message) {
        Log.e("Crystal", message);
    }

    @Override
    protected void info(String message) {
        Log.i("Crystal", message);
    }

    @Override
    protected void verbose(String message) {
        Log.v("Crystal", message);
    }

    @Override
    protected void warn(String message) {
        Log.w("Crystal", message);
    }
}

为什么这不起作用?当我调用基类中的静态函数时,我没有收到任何日志。

无论如何,是否可以通过仅编辑 AndroidLog 类或在不依赖于 AndroidLog 的 CLog 类中进行编辑来实现此工作?

I have a base abstract logger class, that has an instance variable that I want to be set by the derived class automatically when the code is create. So here is the base class:

abstract public class CLog 
{
    /** Maintains the call stack level for each thread */
    private static HashMap<Integer, Integer> callStackLevel = new HashMap<Integer, Integer>();

    /** Static instance to be set by the derived class */
    private static CLog instance = null;

    /** Logs in verbose */
    public static void v(String message) { if(instance != null) instance.verbose(getMessage(message)); }
    /** Logs in debug */
    public static void d(String message) { if(instance != null) instance.debug(getMessage(message)); }
    /** Logs in informational */
    public static void i(String message) { if(instance != null) instance.info(getMessage(message)); }
    /** Logs in warning */
    public static void w(String message) { if(instance != null) instance.warn(getMessage(message)); }
    /** Logs in error */
    public static void e(String message) { if(instance != null) instance.error(getMessage(message)); }

    /**
     * Calculates the message (with header)
     */
    private static String getMessage(String message)
    {
        ...
    }

    /** Constructor sets instance */
    protected CLog() { instance = this; }

    /** Logs in verbose */
    protected abstract void verbose(String message);
    /** Logs in debug */
    protected abstract void debug(String message);
    /** Logs in informational */
    protected abstract void info(String message);
    /** Logs in warning */
    protected abstract void warn(String message);
    /** Logs in error */
    protected abstract void error(String message);
}

I creates the derived class for an android logger. I want it to automatically call the constructor, but it seems this isn't working because nothing is resulting from all my logging functions.

public class AndroidLog extends CLog 
{
    protected static AndroidLog derived = new AndroidLog();

    @Override
    protected void debug(String message) {
        Log.d("Crystal", message);
    }

    @Override
    protected void error(String message) {
        Log.e("Crystal", message);
    }

    @Override
    protected void info(String message) {
        Log.i("Crystal", message);
    }

    @Override
    protected void verbose(String message) {
        Log.v("Crystal", message);
    }

    @Override
    protected void warn(String message) {
        Log.w("Crystal", message);
    }
}

Why is this not working? When I call the static function in the base class, I am not getting any logs.

Is there anyway by just editting the AndroidLog class or by making an edit in the CLog class that isn't dependent on AndroidLog to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-09 07:14:02

AndroidLog 类的存在不会导致它自行引导!

在Java中,类是在使用时加载和初始化的,而不是之前!因此,除非您的任何类以某种相关方式引用 AndroidLog,否则它永远不会被加载,它的静态字段永远不会初始化,并且它的构造函数永远不会被调用。

The mere existence of the AndroidLog class won't cause it to bootstrap itself!

In Java classes are loaded and initialized when they are used, not before! So unless any class of yours somewhere references AndroidLog in some relevant way, it will never be loaded, it's static fields never initialized and its constructor will never be called.

半山落雨半山空 2024-11-09 07:14:02

另一个重要的方面是私有静态类成员不能被继承!您的 AndroidLog 类将无权访问基类中的任何私有静态成员。

Another important aspect is the fact that private static class members are not inherited! Your AndroidLog class will not have access to any of the private static members in the base class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文