Java里这个东西叫什么?
What is this thing called in Java?
Google今天不太友好,正在研究一些以前没用过的OOP技术
基本上,我注意到一些库有带有预设选项的变量,比如 new Website(Websites.Whosebug)
。当你有这样明显不可变的值时,那叫什么?如果我想创建自己的 Colours.RED
和 Colours.GREEN
怎么办?
希望大家能告诉我这叫什么,好让我继续研究!谢谢。
编辑:我没有将此标记为重复项,因为我无法弄清楚如何准确描述我正在寻找的内容 - 我认为其他人很可能有类似的困难并且可能会找到这个有用。如果我错了,没关系。
它叫做枚举。检查 here 了解更多信息。
它们被称为枚举。您可以找到详细信息 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.
没有看到你引用的确切代码我从语法风格中假设 Websites.Whosebug
是一个常量; Website(Websites.Whosebug)
正在创建一个名为 Whosebug 的知名网站。
class Website
和 Websites
可能看起来像:
public class Website {
public WebSite(String wellKnownWebSite) {
..
}
}
public class Websites {
public static final String Whosebug = "http://www.whosebug.com";
}
也有可能这个语法使用了Java Enumerations,技术上是枚举(它只是一个缩写)。
它可能是两件事,一个枚举器(又名枚举)或一个包含常量字段的 class。
枚举示例:
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 包含常量字段。 (虽然这不太可能,因为那不是正确的命名约定,但是......你永远不知道。)
因此,具有常量字段的 class 示例:
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.
}
希望对您有所帮助。
Google今天不太友好,正在研究一些以前没用过的OOP技术
基本上,我注意到一些库有带有预设选项的变量,比如 new Website(Websites.Whosebug)
。当你有这样明显不可变的值时,那叫什么?如果我想创建自己的 Colours.RED
和 Colours.GREEN
怎么办?
希望大家能告诉我这叫什么,好让我继续研究!谢谢。
编辑:我没有将此标记为重复项,因为我无法弄清楚如何准确描述我正在寻找的内容 - 我认为其他人很可能有类似的困难并且可能会找到这个有用。如果我错了,没关系。
它叫做枚举。检查 here 了解更多信息。
它们被称为枚举。您可以找到详细信息 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.
没有看到你引用的确切代码我从语法风格中假设 Websites.Whosebug
是一个常量; Website(Websites.Whosebug)
正在创建一个名为 Whosebug 的知名网站。
class Website
和 Websites
可能看起来像:
public class Website {
public WebSite(String wellKnownWebSite) {
..
}
}
public class Websites {
public static final String Whosebug = "http://www.whosebug.com";
}
也有可能这个语法使用了Java Enumerations,技术上是枚举(它只是一个缩写)。
它可能是两件事,一个枚举器(又名枚举)或一个包含常量字段的 class。 枚举示例:
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 包含常量字段。 (虽然这不太可能,因为那不是正确的命名约定,但是......你永远不知道。) 因此,具有常量字段的 class 示例:
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.
}
希望对您有所帮助。