enum

Java Enum toString() method

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have several Java enums using underscores to separate words. In each of my enums I have a toString method which is implemented as super.toString().replace("_", " ") so that I can display the String values on a GUI without the underscores. Is there some way I can have all my enums use this toString method without duplicating it across all of them? If this were a regular class the answer is simple, implement toString() then have all my classes that need that implementation extend that class, but enums can't extend anything, not even another

.NET: bool vs enum as a method parameter

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Each time I'm writing a method that takes a boolean parameter representing an option, I find myself thinking: "should I replace this by an enum which would make reading the method calls much easier?". Consider the following with an object that takes a parameter telling whether the implementation should use its thread-safe version or not (I'm not asking here if this way of doing this is good design or not, only the use of the boolean): public void CreateSomeObject(bool makeThreadSafe); CreateSomeObject(true); When the call is next to the

Java : Convert Object consisting enum to Json Object

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using org.json library to convert Object to Json format. Kindly check the below code snippet. public enum JobStatus implements Serializable{ INCOMPLETE, INPROGRESS, ABORTED, COMPLETED } public class Job implements Serializable { private string id; private JobStatus status; ... } ... // Create Job Object Job job = new Job("12345", JobStatus.INPROGRESS); // Convert and print in JSON format System.out.println(new JSONObject(job).toString()); It shows the output like this : {"id":"12345", "status" : {}} It shows blank and adds Curly bases.

Accessing a String Enum by index

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an enum in C and the index needs to be represented by a String. How can a Swift enum of String type be used by integer index? I would like to copy the enum to Swift, set the type to string and define all of the raw values to display text, and then use the C enum value to extract the raw value text for the Swift String enum. Otherwise I will just create an array of strings.. But the enum would be more usable. 回答1: In Swift, enum types do not hold its index info of cases (at least, not provided for programmers). So: How can a Swift enum

Where to put the enum in a cpp program?

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a program that uses enum types. enum Type{a,b,}; class A { //use Type }; class B { // also use that Type }; 2 class are located in 2 different files. Should I put the type definition in a headfile or in class definition for each class? 回答1: If the enum is going to be used in more than one .cpp file, you should put it in a header file that will be included by each. If there's a common header file, you should use that, otherwise you may as well create a new header file for this enum 回答2: You should always attempt to limit the scope of

Enum C++ Get by Index

匿名 (未验证) 提交于 2019-12-03 02:52:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I was wondering in C++ if I have an enum can I access the value at the second index? For example I have enum Test{hi, bye}; if I want 'hi', can I do something like Test[0], thanks. 回答1: Yes and no. If your Enum does not have explicit values then it is possible. Without an explicit values, enum values are given numeric values 0-N in order of declaration. For example ... enum Test { hi, // 0 bye // 1 } This means that indexes just translates into a literal value. Test EnumOfIndex(int i) { return static_cast<Test>(i); } This of course does 0

Spring&#039;s @RequestParam with Enum

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have this enum : public enum SortEnum { asc, desc; } That I want to use as a parameter of a rest request : @RequestMapping(value = "/events", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public List<Event> getEvents(@RequestParam(name = "sort", required = false) SortEnum sort) { It works fine when I send these requests /events /events?sort=asc /events?sort=desc But when I send : /events?sort=somethingElse I get a 500 response and this message in the console : 2016-09-29 17:20:51.600 DEBUG 5104 --- [ XNIO-2 task

#define or enum? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Possible Duplicate: Why use enum when #define is just as efficient? When programming in C, is it better practice to use #define statements or enums for states in a state machine? 回答1: Since the states are related elements I think is better to have an enum defining them. 回答2: Technically it doesn't matter. The compiler will most likely even create identical machine code for either case, but an enumeration has three advantages: Using the right compiler+debugger combination, the debugger will print enumeration variables by their enumeration

Use enums for array indexes in Java

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: While reading Effective Java I came across the suggestion to "use enums instead of int constants". In a current project I am doing something similar to that below: int COL_NAME = 0; int COL_SURNAME = 1; table[COL_NAME] = "JONES" How would I use enums instead to achieve this? Due to the interface I'm forced to use, I must use an int for my index. The example above is just an example. I'm actually using an API that takes an int for index values. 回答1: Applying one usefull pattern together with an anti-pattern often fails ;-) In your case using

enum vs constexpr for actual static constants inside classes

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Let me start by stating my intent. In the olden (C++) days, we would have code like: class C { public: enum {SOME_VALUE=27}; }; Then we could use SOME_VALUE throughout our code as a compile time constant and wherever the compiler would see C::SOME_VALUE , it would just insert the literal 27. Now days, it is seems more acceptable to change that code to something like: class C { public: static constexpr int SOME_VALUE=27; }; This looks much cleaner, gives SOME_VALUE a well defined type and seems to be the preferred approach as of C++11. The