简体   繁体   English

返回对具有静态方法和静态字段的类的引用,而无需实例化

[英]Return a Reference to a Class with Static Methods and Static Fields Without Instantiation

I want to create a wrapper class that calls static methods and member fields from a class that is provided by a library I am unable to view the code. 我想创建一个包装器类,该包装器类从无法查看代码的库提供的类中调用静态方法和成员字段。

This is to avoid boilerplate setting code of the global member fields when I need to use a static method in a specific context. 当我需要在特定上下文中使用静态方法时,这是为了避免全局成员字段的样板设置代码。

I want to try to avoid creating wrapper methods for each static method. 我想尝试避免为每个静态方法创建包装器方法。

My question: 我的问题:

Is it possible to return a class with static methods from a method to access just the static methods without instantiating it? 是否可以从方法中返回带有静态方法的类以仅访问静态方法而无需实例化它?

Code is below with comments in-line. 代码如下,带有注释。

The code is used to demonstrate a change in a static value when the method getMath() is invoked. 该代码用于演示调用getMath()方法时静态值的变化。

I want to avoid the setting of the value before calling the static method. 我想避免在调用静态方法之前设置值。

StaticMath.setFirstNumber(1);
StaticMath.calc(1);

StaticMath.setFirstNumber(2);
StaticMath.calc(1);

I am using the Eclipse IDE and it comes up with Warnings, which I understand, but want to avoid. 我使用的是Eclipse IDE,它附带警告,我了解但希望避免。

I tried searching for something on this subject, so if anyone can provide a link I can close this. 我尝试搜索有关此主题的内容,因此,如果有人可以提供链接,我可以关闭此链接。

public class Demo {
    // Static Methods in a class library I don't have access to. 
    static class StaticMath {
        private static int firstNum;

        private StaticMath() {  
        }

        public static int calc(int secondNum) {
            return firstNum + secondNum;
        }

        public static void setFirstNumber(int firstNum) {
            StaticMath.firstNum = firstNum;
        }
    }

    // Concrete Class
    static class MathBook {
        private int firstNum;

        public MathBook(int firstNum) {
            this.firstNum = firstNum;
        }

        // Non-static method that gets the class with the static methods.
        public StaticMath getMath() {
            StaticMath.setFirstNumber(firstNum);
            // I don't want to instantiate the class.
            return new StaticMath();
        }
    }

    public static void main(String... args) {
        MathBook m1 = new MathBook(1);
        MathBook m2 = new MathBook(2);

        // I want to avoid the "static-access" warning.
        // Answer is 2
        System.out.println(String.valueOf(m1.getMath().calc(1)));
        // Answer is 3
        System.out.println(String.valueOf(m2.getMath().calc(1)));
    }
}

I'd just wrap it to make for an atomic operation: 我只是将其包装起来以进行原子操作:

public static class MyMath{

    public static synchronized int myCalc( int num1 , int num2 ){
         StaticMath.setFirstNum(num1);
         return StaticMath.calc(num2);
    }

}

Drawback: You'll have to make sure, StaticMath is not used avoiding this "bridging" class. 缺点:您必须确定,不会使用StaticMath来避免此类“桥接”类。

Usage: 用法:

int result1 = MyMath.myCalc( 1, 1 );
int result1 = MyMath.myCalc( 2, 1 );

You shouldnt call a static method through an object reference. 您不应该通过对象引用来调用静态方法。 You should directly use class reference to call a static method like this: 您应该直接使用类引用来调用静态方法,如下所示:

StaticMath.calc(1)

But if you still need it for some reason, you can return null in getMath method, but you will still get warning in Eclipse: 但是,如果由于某些原因仍然需要它,则可以在getMath方法中返回null,但是在Eclipse中仍然会收到警告:

public StaticMath getMath() {
    StaticMath.setFirstNumber(firstNum);
    return null;
}

I infer that question is not properly asked if the answer is not 我推断问题是否得到了正确回答

   StaticMath.calc(1)

Other issue you may be facing due to package visibility to static inner classes. 由于包对静态内部类的可见性,您可能面临的其他问题。 Which is a design choice by the writer of Demo class. 这是Demo类作者的设计选择。 If you can mark your classes MathBook and StaticMath public then you can access them like below: 如果可以将MathBook和StaticMath类标记为公共,则可以按以下方式访问它们:

   Demo.StaticMath.calc(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM