简体   繁体   English

如何访问“未命名”类的方法?

[英]How can I access a method of an “unnamed” class?

public class Test {
    public static void main(String[] args) {
        DemoAbstractClass abstractClass = new DemoAbstractClass() {
            private String val;

            @Override
            public void runner() {
                val = "test";
                System.out.println(val);
                this.run();
            }

            public String getVal() {
                return val;
            }
        };

        abstractClass.runner();

        /**
         * I want to access getVal method here
         */
    }
}

abstract class DemoAbstractClass {
    public void run() {
        System.out.println("running");
    }

    public abstract void runner();
}

Here, I'm declaring an abstract class DemoAbstractClass. 在这里,我声明一个抽象类DemoAbstractClass。 I can obviously create a new class that extends this class and add this method to it. 很明显,我可以创建一个扩展该类的新类并将此方法添加到其中。 But, I would prefer not doing that in my scenario. 但是,我不希望在我的情况下这样做。 Is there any other way to access getVal method in above code?? 上面的代码中还有其他方法可以访问getVal方法吗?

You can't. 你不能 You need to make a proper (non-anomous) class out of it. 您需要从中做出适当的(非一致)类。 Make it an inner private class if you want to limit its scope. 如果要限制其范围,请使其成为内部私有类。

Alternatively, you could use a StringBuffer and share a referense to it between the methods. 另外,您可以使用StringBuffer并在方法之间共享对其的引用。 Not extremely clean however. 但是不是很干净。

Related question: 相关问题:

Short of using reflection, you cannot as you have no access to the concrete type of the object to be able to bind the methodcall to 缺少使用反射,您无法访问对象的具体类型,因此无法将方法调用绑定到

If you don want to do something like this in a sane manner, declare a named class and use that as the type of abstractClass 如果您不想以理性的方式执行此类操作,请声明一个命名类并将其用作abstractClass的类型。

Unfortunately, if you cannot name the type, you cannot access the methods at the language level. 不幸的是,如果您不能命名类型,则无法在语言级别访问方法。

What you can do, though, is use the reflection API to get a Method object and invoke it on this object. 但是,您可以使用反射API获取Method对象并在该对象上invoke它。

This, however, is pretty slow. 但是,这非常慢。 A private class or private interface would be much faster. 私有类或私有接口会更快。

I can obviously create a new class that extends this class and add this method to it. 很明显,我可以创建一个扩展该类的新类并将此方法添加到其中。

You've already done this; 您已经做到了; the end result was an anonymous inner class: new DemoAbstractClass() { ... }; 最终结果是一个匿名的内部类: new DemoAbstractClass() { ... }; If you just moved that declaration into its own class -- you can even make it a private class -- you can access getVal. 如果只是将该声明移到其自己的类中(甚至可以将其设为私有类),则可以访问getVal。

Per your example above: 根据上面的示例:

public class Test {
    public static void main(String[] args) {
        DemoClass abstractClass = new DemoClass();

        abstractClass.runner();

        /**
         * I want to access getVal method here
         */
        abstractClass.getVal(); // can do this here now
    }

    private class DemoClass extends DemoAbstractClass {

            private String val;

            @Override
            public void runner() {
                val = "test";
                System.out.println(val);
                this.run();
            }

            public String getVal() {
                return val;
            }
        }
    }
}

Another option is to make a StringBuilder a member of the main method and use the closure nature of anonymous inner methods: 另一种选择是使StringBuilder成为main方法的成员,并使用匿名内部方法的闭包特性:

public static void main(String[] args) {
    final StringBuilder value = new StringBuilder();
    DemoAbstractClass abstractClass = new DemoAbstractClass() {
        @Override
        public void runner() {
            value.append( "test" );
            System.out.println(val);
            this.run();
        }
    };

    abstractClass.runner();

    // use val here...
    String val = value.toString();
}

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

相关问题 如何从通用方法访问 class 的方法 - How can I access a method of a class from a generic method 如何找到“已在未命名的包中定义”的Java类? - How can I find a Java class that's “already defined in an unnamed package”? 如何直接访问m1()方法中的A类变量? - How can i access Class A variable in m1() method directly? 如何从内部类方法访问局部变量? - How can I access to local variable from inner class method? 如何从测试类中访问私有方法 - how can I access a private method from the test class 我如何从不同的 class 方法访问变量 - How can i access the variable from different class method 如何访问位于Java中另一个类的另一个方法内的类内的方法 - How can I access a method that is inside a class that is inside another method of another class in Java Java Swing:如何在另一个类中使用未命名的按钮? - Java Swing: how do I use an unnamed button in another class? 在静态工厂方法中使用覆盖方法创建实例时,如何访问封闭类中的私有字段? - How can I access private field in enclosing class when creating instance with overridden method in static factory method? 如何在方法外部访问已在其他类中实例化的类A的成员变量? - How can I access the member variable of class A outside the method, that has been instantiated in other class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM