简体   繁体   English

在java枚举类型中使用+

[英]Using + in a java enum type

I'm fairly new to Java and I'm trying to use the + character as part of an enum type, but the compiler is complaining about the syntax because I believe it sees it as an operator. 我是Java的新手,我正在尝试将+字符用作枚举类型的一部分,但编译器抱怨语法,因为我认为它将其视为运算符。

I'd like to do the following: 我想做以下事情:

enum mediaType{

   FACEBOOK,GOOGLE+,TWITTER;

}

Any ideas? 有任何想法吗?

Thanks! 谢谢!

Yes, the compiler will treat + as an operator. 是的,编译器会将+视为运算符。 You can however choose a different name there: 但是,您可以在那里选择其他名称:

enum mediaType{
    FACEBOOK,
    GOOGLE_PLUS,
    TWITTER;    
}

And if you want to use the value GOOGLE+ only, then have a field of type String , storing the value, and also a parameterized constructor. 如果您只想使用GOOGLE+值,那么请使用String类型的字段,存储该值,以及参数化构造函数。

PS: As per proper naming convention, the enum name should be MediaType . PS:根据适当的命名约定,枚举名称应为MediaType

You can't use arithmetic symbols in identifiers. 您不能在标识符中使用算术符号。 You need to find something you can use like GOOGLE_PLUS 您需要找到可以使用的内容,例如GOOGLE_PLUS

Maybe reading the official Java tutorial's section on Naming Conventions will help you: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html 也许阅读官方Java教程的命名约定部分将帮助您: http//docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

You could at least define a private String myName = "Google+"; 您至少可以定义一个private String myName = "Google+"; inside the enum, and define a method that a UI can use to get the value you want rather than just displaying the enum's variable itself. 在枚举内部,定义一个UI可用于获取所需值的方法,而不仅仅是显示枚举变量本身。

public String myName() {
    return myName;
}

Many people believe that you are limited to ASCII in Java just like C and C++. 许多人认为你只能像Java和C ++一样使用Java中的ASCII。

Actually you have the full Unicode character set. 实际上你有完整的Unicode字符集。

This is perfectly good Java: 这是非常好的Java:

enum Plus {
  Google,
  GooglePlus,
  Googleᚋ,
  Googleᐩ;
};

Not quite a + (which you cannot have in an enum because it will be confused with the + operator) but it will still carry the impression of a plus. 不是一个+(你不能在enum因为它会与+运算符混淆)但它仍然会带来加号的印象。

It seems you can use the Ogham character called muin which looks a bit like a plus character. 看来你可以使用名为muinOgham字符看起来有点像加号。 Alternatively it seems the Canadian syllabics final plus is also acceptable alhough a number of the other possibilities seem not to be acceptable. 或者看起来加拿大音节最终加上也是可以接受的,尽管其他一些可能性似乎是不可接受的。

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

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