Java – 用枚举扩展类

huangapple go评论80阅读模式
英文:

Java - Extend class with enum

问题

我有一个名为 BaseNumber 的类,其中有一个 protected static enum Base,还有另一个名为 ArithmeticBaseNumber 的类,它继承自 BaseNumber。<br>
我可以从 BaseNumber 访问 Base 枚举,但不能从 ArithmeticBaseNumber 访问。<br>
有没有办法访问这个枚举?<br>
谢谢!

编辑 - 代码:

public class BaseNumber {
    protected static enum Base {
        X, Y, Z
    }
}
public class ArithmeticBaseNumber extends BaseNumber {}

问题在于 BaseNumber.Base.X 可以正常工作,但 ArithmeticBaseNumber.Base.X 报错 "The type ArithmeticBaseNumber.Base is not visible"。

英文:

I have a class BaseNumber with a protected static enum Base, and another class ArithmeticBaseNumber extends BaseNumber. <br>
I can access the Base enum from BaseNumber, however not from ArithmeticBaseNumber. <br>
Is there a way to access the enum? <br>
Thank you!

Edit - The code:

public class BaseNumber {
    protected static enum Base {
        X, Y, Z
    }
}
public class ArithmeticBaseNumber extends BaseNumber {}

The problem is that BaseNumbe.Base.X works fine, however ArithmeticBaseNumber.Base.X gives "The type ArithmeticBaseNumber.Base is not visible" error.

答案1

得分: 1

Java不支持静态成员的继承,静态成员可以在创建它的类上调用。

英文:

Java doesn't support the inheritance of static members, a static member is callable on a class in which it has been created.

答案2

得分: 0

以下是翻译好的内容:

它可以被访问。尝试下面的示例。

    package com.example.demo.enumexample;

    public class BaseNumber {
        protected static enum Base {
            FIRST("first"),
            SECOND("second"),
            THIRD("third");
            String httpMethodType;
            Base(String s) {
                httpMethodType = s;
            }
            public String getHTTPMethodType() {
                return httpMethodType;
            }
        }

        public static void main(String[] args) {
            Base baseNum = Base.FIRST;
            System.out.println(baseNum);
        }
    }


在ArithmeticBaseNumber.java中

    package com.example.demo.enumexample;

    import com.example.demo.enumexample.BaseNumber.Base;

    public class ArithmeticBaseNumber extends BaseNumber {
        public static void main(String[] args) {
            Base baseNum = Base.FIRST;
            System.out.println(baseNum);
        }
    }


输出将会是:

>FIRST

在你的示例代码中,

    package com.example.demo.enumexample;

    public class BaseNumber {
        protected static enum Base {
            X, Y, Z;
        }
        
        public static void main(String[] args) {
            Base baseNum = Base.X;
            System.out.println(baseNum);
        }
    }

以及

    package com.example.demo.enumexample;

    public class ArithmeticBaseNumber extends BaseNumber {
        public static void main(String[] args) {
            Base baseNum = Base.X;
            System.out.println(baseNum);
        }
    }


输出将会是:

>X
英文:

It can be accessed. Try below example.

package com.example.demo.enumexample;

public class BaseNumber {
protected static enum Base{
FIRST(&quot;first&quot;),
SECOND(&quot;second&quot;),
THIRD(&quot;third&quot;);
String httpMethodType;
Base(String s) {
    httpMethodType = s;
}
public String getHTTPMethodType() {
    return httpMethodType;
}
}

public static void main(String[] args) {
	Base baseNum = Base.FIRST;
	System.out.println(baseNum);
}
}

And in ArithmeticBaseNumber.java

package com.example.demo.enumexample;

import com.example.demo.enumexample.BaseNumber.Base;

public class ArithmeticBaseNumber extends BaseNumber{
public static void main(String[] args) {
	Base baseNum = Base.FIRST;
	System.out.println(baseNum);
}
}

Output will be :

>FIRST

In your example code,

package com.example.demo.enumexample;

public class BaseNumber {
protected static enum Base{
	X, Y, Z;

}

public static void main(String[] args) {
	Base baseNum = Base.X;
	System.out.println(baseNum);
}
}

And

package com.example.demo.enumexample;


public class ArithmeticBaseNumber extends BaseNumber{
public static void main(String[] args) {
	Base baseNum = Base.X;
	System.out.println(baseNum);
}
}

Output will be :

>X

答案3

得分: 0

你正在定义的枚举类型是 BaseNumber.Base,而不是 ArithmeticBaseNumber.Base 或者简单的 Base

因此,请使用它的正确名称 BaseNumber.Base 进行引用,一切都会正常。

你可以(但可能不应该)通过导入以下方式使简单名称 Base 生效:

import mypackage.BaseNumber.Base;

注意:如果你想使用短名称,我通常建议将 Base 枚举移到自己的文件中 - 对我来说,点号语法是一个重要的提示,它提醒我们正在讨论 BaseNumber 的一些内部元素。

类似的情况是,出于可读性的原因,我总是使用 Map.Entry 而不是 Entry(不清楚它与 Map 相关)或者 HashMap.Entry(完全错误,Entry 不是 HashMap 的一部分)。

英文:

The one enum type you're defining is BaseNumber.Base and not ArithmeticBaseNumber.Base nor simply Base.

So just reference it by its proper name BaseNumber.Base, and everything is fine.

You can (but probably shouldn't) make the simple name Base work by importing:

    import mypackage.BaseNumber.Base;

Caveat: if you want to use the short name, I'd typically recommend moving the Base enum out into its own file - to me, the dotted syntax is an important reminder that we're talking about some internal element of BaseNumber.

As a similar case, for readability reasons, I always stay with Map.Entry instead of Entry (unclear that it's related to Maps) or HashMap.Entry (plain wrong, Entry isn't part of HashMap).

huangapple
  • 本文由 发表于 2020年10月21日 14:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64458116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定