简体   繁体   English

如何使用从Java的嵌套接口继承的静态方法定义嵌套的静态类?

[英]How to define nested static classes with static methods, inherited from a nested interface in Java?

I have a Java problem with nested classes. 我有一个关于嵌套类的Java问题。

My first class structure looked like this: 我的第一堂课结构看起来像这样:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }            

    private interface NestedClass {
        public void method();
    }    

    private class NestedClass1 {
        public void method() {
        }
    }    

    private class NestedClass2 {
        public void method(){
        }
    }    
}

But now I want these method() methods to be static because they should be principally. 但是现在我希望这些method()方法是静态的,因为它们原则上应该是静态的。

I cannot make them static without having them in a static class, but that's no problem, I made the classes static, they should be anyway. 如果不将它们放在静态类中,则无法使它们成为静态,但这没问题,我使类成为静态类,无论如何它们都应该是静态的。

It looks like this right now: 现在看起来像这样:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }            

    private static interface NestedClass {
        public void method();
    }    

    private static class NestedClass1 {
        public static void method() {
        }
    }    

    private static class NestedClass2 {
        public static void method(){
        }
    }    
}

But then the trouble begins. 但是,麻烦开始了。 A static method does not inherit correctly from a non-static interface method, as I get this message This static method cannot hide the instance method from TopClass.NestedClass in Eclipse. 当我收到此消息时,静态方法不能正确地从非静态接口方法继承, This static method cannot hide the instance method from TopClass.NestedClass Eclipse中的This static method cannot hide the instance method from TopClass.NestedClass

When I make the interface method static, it gives me this error: Illegal modifier for the interface method method; only public & abstract are permitted 当我将接口方法设为静态时,它给了我这个错误: Illegal modifier for the interface method method; only public & abstract are permitted Illegal modifier for the interface method method; only public & abstract are permitted

So I thought of an abstract class, and tried this: 因此,我想到了一个抽象类,并尝试了以下方法:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }    

    private static abstract class NestedClass {
        public static abstract void method();
    }    

    private static class NestedClass1 {
        public static void method() {
        }
    }    

    private static class NestedClass2 {
        public static void method(){
        }
    }    
}

But again, seemingly abstract methods cannot be declared static: The abstract method method in type NestedClass can only set a visibility modifier, one of public or protected . 但是同样,看似抽象的方法不能声明为静态的: The abstract method method in type NestedClass can only set a visibility modifier, one of public or protected

Leaving the static away (in the abstract class method), errors this on the method methods in the NestedClass1 & 2: This static method cannot hide the instance method from TopClass.NestedClass . 抛开This static method cannot hide the instance method from TopClass.NestedClass (在抽象类方法中),将其错误化为NestedClass1&2中的方法方法。 This static method cannot hide the instance method from TopClass.NestedClass

Isn't there any way to declare some kind of superstructure for covering static methods? 有没有办法声明某种覆盖静态方法的上层结构?

EDIT: The problem I actually try to solve it the lack of possibility of Java for storing references to methods. 编辑:我实际上试图解决它的问题,缺乏Java来存储对方法的引用的可能性。 So instead I have those classes everyone with just one method, but to store them in a List fe they must be able to be "caught" by a superstructure. 因此,相反,我只用一种方法让每个人都有这些类,但是要将它们存储在List fe中,它们必须能够被上层结构“捕获”。 I got the hint to try anonymous classes or enums, gonna try that now. 我得到了尝试匿名类或枚举的提示,现在就尝试。

Interfaces and statics don't go together. 接口和静态特性不能同时使用。 At all. 完全没有 There is no Java support for creating / imposing patterns on static methods. 没有Java支持在静态方法上创建/强加模式。

A static method declaration must always be followed by a definition. 静态方法声明后必须始终有一个定义。 It cannot be implemented by subclasses. 它不能由子类实现。

I think you're just not approaching your problem right. 我认为您只是没有正确地解决问题。 Try a different approach! 尝试不同的方法!

  • Make NestedClass an interface NestedInterface and store your different implementations as anonymous classes implementing this interface: NestedClass接口NestedInterface和存储您不同的实现为实现此接口的匿名类:

     public static final NestedInterface firstNested = new NestedInterface() { @Override public void method() { // ... } }; 
  • Make NestedClass an enumeration NestedEnum and store your different implementations as enumeration values implementing an abstract method from the enumeration. NestedClass设为枚举NestedEnum ,并将不同的实现存储为枚举值,以实现枚举中的抽象方法。 This only works if you have a fixed number of implementations you which to choose from and you do not want to accept NestedClass implementations from outside sources. 仅当您有固定数量的实现可供选择,并且不想接受外部来源的NestedClass实现时,此方法才有效。

     public enum NestedEnum { FIRST { @Override public void method() { // ... } }; public abstract void method(); } 

EDIT : In reply to your comment: 编辑 :回复您的评论:

The classes itself are static as well.. 类本身也是静态的。

static in the context of a nested class means that this class can be instantiated without an instance of the containing class . 嵌套类上下文中的static表示可以在没有包含类实例的情况下实例该类

A regular nested class such as in your first example can be instantiated through TopClass.this.new NestedClass1() . 可以通过TopClass.this.new NestedClass1()实例化常规嵌套类(例如您的第一个示例TopClass.this.new NestedClass1() Normally you'd simply write new NestedClass1() from within the constructor or an instance method of TopClass , but in this verbose form you can clearly see the dependence on TopClass.this . 通常,您只需从构造函数或TopClass的实例方法中编写new NestedClass1() ,但是以这种冗长的形式,您可以清楚地看到对TopClass.this的依赖。 This can also be seen from any method of NestedClass1 , as you have access to the containing class with TopClass.this . 这也可以从任何方法看作NestedClass1 ,因为你有机会与含有类TopClass.this

A static nested class such as in your second example can be instantiated through new TopClass.NestedClass1() . 静态嵌套类(例如您的第二个示例)可以通过new TopClass.NestedClass1()实例化。 Once again, you could just write new NestedClass1() but the verbose form clearly shows that the construction only depends on TopClass and is not associated with an instance of TopClass . 再一次,您可以编写new NestedClass1()但是详细形式清楚地表明构造仅取决于TopClass并且与TopClass实例TopClass You could even create an instance from an outside class using the same snippet new TopClass.NestedClass1() without ever creating a TopClass instance. 您甚至可以使用相同的片段new TopClass.NestedClass1()从外部类创建实例,而无需创建TopClass实例。

I suggest you take a look at this question on inner classes and static nested classes . 我建议您看一下有关内部类和静态嵌套类的问题

The fact the your interface/abstract class is nested is irrelevant to the problem. 您的接口/抽象类嵌套的事实与该问题无关。
You just can't. 你就是不行 There is no way in Java to enforce some class to implement static methods. Java中没有办法强制某些类来实现静态方法。 Just cry and surrender and use instance methods. 只是哭泣和投降,并使用实例方法。

static abstract is a contradiction. static abstract是一个矛盾。 Static methods are not like other languages' class methods. 静态方法与其他语言的类方法不同。 When you make a static method it goes on a single class, it doesn't get inherited by or have its implementation deferred to subclasses. 当您创建静态方法时,它只在一个类上运行,不会被其继承或将其实现推迟到子类上。

You don't explain why you want these methods to be static. 您无需解释为什么要使这些方法是静态的。 If you want these methods to be defined by subclasses then they shouldn't be. 如果您希望这些方法由子类定义,则不应定义它们。

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

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