【问题标题】:What is this thing called in Java?这个东西在Java中叫什么?
【发布时间】:2015-06-19 11:19:13
【问题描述】:

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

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

希望您能告诉我这叫什么,以便我继续研究!谢谢。

编辑:我没有将此标记为重复,因为我无法弄清楚如何准确描述我正在寻找的内容 - 我认为其他人很可能有类似的困难并且可能会觉得这很有用.如果我错了,没关系。

【问题讨论】:

  • 或者他们会是 CONSTANTS
  • 你的意思是Enum
  • 我不太明白你的问题是什么,但这里有一个enum type 的例子。这就是你要找的东西吗?
  • 是的,它是一个枚举。在得到答案的同时,我真的想通了!
  • 该死的很难选择答案,他们在这里都很好!

标签: java oop object inheritance immutability


【解决方案1】:

它们被称为枚举。你可以找到详细信息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.

【讨论】:

  • 我很快就会接受这个,感谢您的快速回答。
【解决方案2】:

没有看到您引用的确切代码,我从 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,技术上是枚举(它只是一个缩写)。

【讨论】:

  • 或许是Enum
  • 不错的想法和不错的答案。
【解决方案3】:

它被称为枚举。更多信息请查看here

【讨论】:

    【解决方案4】:

    它可能是两件事,一个枚举器(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.
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2019-08-17
      相关资源
      最近更新 更多