简体   繁体   English

使用带有枚举 class java 的 getter 和 setter

[英]using getters and setters with enum class java

If I don't declare private Type type;如果我不声明private Type type; and try using getters and setters I'm always getting some kind of error which, of-course, I don't really understand.并尝试使用 getter 和 setter 我总是遇到某种错误,当然,我不太明白。 It's just a red line for me.这对我来说只是一条红线。 I am just trying to understand why I have to declare a separate variable that I didn't think I needed in order to do this.我只是想了解为什么我必须声明一个我认为不需要的单独变量来执行此操作。

Please do ask if I'm missing any information.请务必询问我是否缺少任何信息。 This is my first question ever!这是我有史以来的第一个问题!

Below is part of the practice exercise that I'm trying to do:以下是我正在尝试做的练习的一部分:

Postgraduate.java that contains extra private data members:包含额外私有数据成员的Postgraduate.java

  • type: is an Enum called Type, including Research and Coursework; type:是一个叫Type的Enum,包括Research和Coursework;

Implement Java methods in the file Postgraduate.java that include:在 Postgraduate.java 文件中实现 Java 方法,包括:

  • Initialization constructor that assigns values to all data members;为所有数据成员赋值的初始化构造函数;
  • Public access methods getType() that return values of private data members;返回私有数据成员值的公共访问方法getType()
  • Public update methods setType(Type newType) that update values of private data members;公共更新方法setType(Type newType)更新私有数据成员的值;
  • Public overriding method toString() that returns of postgraduate information.返回研究生信息的公共重写方法toString()
public class Postgraduate {
    private enum Type{Reserch,Coursework;}
    private Type type;

    public Postgraduate(Type newtype) {
        Type type=newtype;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type newType){
        type = newType;
    }
}

An enum is a type, the same as a class or an interface is. 枚举是一种类型,与 class 或接口相同。 Hence this line of your code is declaring a type and not a variable:因此,这行代码声明了一个类型,而不是一个变量:

private enum Type{Reserch,Coursework;}

This line of your code declares a member variable and not a global variable (as you stated in your comment to your question):这行代码声明了一个成员变量,而不是一个全局变量(正如您在对问题的评论中所述):

private Type type;

Also, as @ArunGowda wrote in his comment , you have an error in the code of Postgraduate class constructor.此外,正如@ArunGowda 在他的评论中所写,您在Postgraduate class 构造函数的代码中有一个错误。 The code should be:代码应该是:

public Postgraduate(Type newtype) {
    type = newtype;
}

By convention enum values are written all upper-case.按照惯例,枚举值全部大写。 You also spelt Research wrong.你也拼错了研究 Consider the following code:考虑以下代码:

public class Postgraduate {
    private enum Type {RESEARCH, COURSEWORK}

    private Type  type;

    public Postgraduate(Type type) {
        this.type = type;
    }

    @Override // java.lang.Object
    public String toString() {
        return type.toString();
    }

    public static void main(String[] args) {
        Postgraduate phD = new Postgraduate(Type.RESEARCH);
        System.out.println(phD);
    }
}

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

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