How do you get a member of an enum in jython?

2019-08-24 03:08发布

问题:

enum day{ mon,tue}
enum getday(){
  return day;
}

I want to print the day, like "mon" or "tue". Is it possible?

回答1:

Just invoke the name method. For example:

>>> from java.lang import *              
>>> s = Thread.currentThread().getState()
>>> s
RUNNABLE
>>> type(s)
<type 'java.lang.Thread$State'>
>>> s.name()
u'RUNNABLE'


回答2:

Unless I'm mistaken, it's using the actual Java enum type, in which case you should be able to access the name attribute.

Something like day.getName() would be how you would typically do this in Java. In Java, it would return either "mon" or "tue" depending on the value of the enum instance being accessed.



标签: jython