如何在其他 类 中使用私有枚举
How to use private enums in other classes
这是我创建私有枚举的超类
public class Media {
private String Title;
private enum Genre { Comedy, Horror, Action, Romance, Documentary, Thriller, Drama}
Genre genre;
public Media ()
{
}
public Media(String t, Genre g){
this.Title=t;
genre=g;
}
现在我想在子类中使用它
public class Movies extends Media{
private String regi;
private int playtime;
public Movies(){
}
public Movies(String t, Genre g, String r, int playt)
{
setTitle(t);
genre=g;
this.regi=r;
this.playtime=playt;
}
但它不起作用,因为它告诉我当我为创建 Movie 对象的方法发送流派时枚举是私有的?是否不可能拥有私有枚举而不在其他 类 或 sub类 中使用它?
将枚举设为 protected
而不是 private
protected enum Genre { Comedy, Horror, Action, Romance, Documentary, Thriller, Drama}
Private
表示只能在其声明的 class 中访问。
Protected
与 private 相同,但派生的 classes 可以访问。
Public
表示访问全部
我不是 Java 的专家,但在定义可见性的情况下。如果要将范围限制为当前 class,则定义一个 private 成员(属性,属性,方法)。如果想要从派生 class 访问但将范围保持在 class 或派生,您应该将其定义为 protected。 我强烈建议阅读一些相关内容,因为理解所有这些基本概念至关重要。
这是我创建私有枚举的超类
public class Media {
private String Title;
private enum Genre { Comedy, Horror, Action, Romance, Documentary, Thriller, Drama}
Genre genre;
public Media ()
{
}
public Media(String t, Genre g){
this.Title=t;
genre=g;
}
现在我想在子类中使用它
public class Movies extends Media{
private String regi;
private int playtime;
public Movies(){
}
public Movies(String t, Genre g, String r, int playt)
{
setTitle(t);
genre=g;
this.regi=r;
this.playtime=playt;
}
但它不起作用,因为它告诉我当我为创建 Movie 对象的方法发送流派时枚举是私有的?是否不可能拥有私有枚举而不在其他 类 或 sub类 中使用它?
将枚举设为 protected
而不是 private
protected enum Genre { Comedy, Horror, Action, Romance, Documentary, Thriller, Drama}
Private
表示只能在其声明的 class 中访问。
Protected
与 private 相同,但派生的 classes 可以访问。
Public
表示访问全部
我不是 Java 的专家,但在定义可见性的情况下。如果要将范围限制为当前 class,则定义一个 private 成员(属性,属性,方法)。如果想要从派生 class 访问但将范围保持在 class 或派生,您应该将其定义为 protected。 我强烈建议阅读一些相关内容,因为理解所有这些基本概念至关重要。