这个东西在Java中叫什么?

时间:2015-06-19 11:19:14

标签: java oop object inheritance immutability

Google今天不是非常友好,而且我正在研究过去没有使用的一些OOP技术。

基本上,我注意到有些库包含预设选项的变量,例如new Website(Websites.STACKOVERFLOW)。当你有明显不可变的价值时,这叫什么?如果我想创建自己的Colours.REDColours.GREEN

,该怎么办?

我希望你能告诉我这是什么让我继续研究!谢谢。

编辑:我没有将此标记为副本,因为我无法弄清楚如何准确描述我所寻找的内容 - 我认为很可能是某人其他可能有类似的困难,可能会发现这有用。如果我错了,那没关系。

4 个答案:

答案 0 :(得分:4)

它们被称为枚举。您可以找到详细信息here

它们被定义为:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

用作:

public class EnumTest {
    Day day;

    public EnumTest(Day day) {
        this.day = day;
    }

    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;

            case FRIDAY:
                System.out.println("Fridays are better.");
                break;

            case SATURDAY: case SUNDAY:
                System.out.println("Weekends are best.");
                break;

            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }

    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

,输出为:

Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.

答案 1 :(得分:3)

没有看到你引用的确切代码我假设Websites.STACKOVERFLOW是一个常量的语法风格;并且Website(Websites.STACKOVERFLOW)正在创建一个名为Stackoverflow的知名网站。

课程WebsiteWebsites可能如下所示:

public class Website {
  public WebSite(String wellKnownWebSite) {
    ..
  }
}


public class Websites {
  public static final String STACKOVERFLOW = "http://www.stackoverflow.com";
}

这种语法也可能使用Java Enumerations,技术上是Enums(它只是一种收缩)。

答案 2 :(得分:1)

它被称为Enum。查看here了解详情。

答案 3 :(得分:0)

它可能是两件事,无论是枚举器(AKA Enum)还是具有常量字段的类。 枚举示例:

public enum ValueSeperationType {
    CSV(","),NEW_LINE("\n"),DASH("-"),DOT("."),SPACE(" "),TAB("\t"),OTHER("_"); //Our values, the parenthesis with the string in it is the value the constructor will get once the enum is called.

    private final String seperator; //A variable to hold the value provided to the constructor by the enum;
    ValueSeperationType(String seperator){// the constructor that gets called when we type for instance "CalueSeperationType.CSV" Then the constructor takes as parameter a String with value ",".
        this.seperator = seperator;// initialising the variable to hold the value.
    }

    public String getSeperator(){ // a method allowing user to get the seperation value, If we type ValueSeperationType.DASH.getSeperator() this will return "-".
        return this.seperator;
    }
    //You can add as many parameters in the constrctor as you want, as many variables and methods as you want.
}

另一种可能性是它是一个包含常量字段的类。 (虽然这不太可能,因为这不是正确的命名惯例,但......你永远不会知道。) 这是一个具有常量字段的类的示例:

class ValueSeperation{
    public static final String CSV = ",";
    public static final String NEW_LINE = "\n";
    public static final String DASH = "-";
    public static final String DOT = ".";
    public static final String SPACE = " ";
    public static final String TAB = "\t";
    public static final String OTHER = "_";

    //Same concept as above, in a more direct way... here you simply invoke ValueSeperation.CSV and that returns ",". Each method has it's benefits and it is up to the developer to decide what to implement depending on the senario.
}

我希望有所帮助。