New Features of jdk5.0-Enumeration

Source: Internet
Author: User
Tags switch case

// Static import: import the static method in this class <br/> Import static Java. lang. system. out; </P> <p>/** <br/> * simple examples of enumeration classes. <br> <br/> * @ author he Shaohui. <br> <br/> * Create 2010-07-19. <br/> */<br/> public class testenum {</P> <p> Public static void main (string [] ARGs) {<br/> // obtain all enumeration objects <br/> SEASON2 [] Ss = season2.values (); <br/> for (SEASON2 S: SS) {<br/> out. println (S. ordinal () + "--->" + S. name (); <br/>}< br/> // obtain a single enumerated object. <br/> Season1 S = enum. valueof (season1.class, "Spring"); <br/> SEASON2 S2 = season2.spring; <br/> // application instance <br/> // 1: addition, subtraction, multiplication, and Division <br/> operator [] OP = Operator. values (); <br/> for (operator O: op) {<br/> out. println (O. getname () + "(" + O. name () + "):" + O. calculate (4, 2); <br/>}< br/> // 2: Switch case <br/> string action = ""; <br/> switch (season1.valueof (Action) {<br/> case AUTUMN: <br/> break; <br/>}</P> <p> // The simplest enumeration definition. <br/> Enum Season1 {<br/> spring, <br/> summer, <br/> autumn, <br/> Winter; <br/>}</P> <p> Enum SEASON2 {<br/> spring ("Spring"), <br/> summer ("Summer "), <br/> autumn ("Autumn"), <br/> Winter ("Winter"); <br/> // attributes can be set by the constructor and set methods. <br/> private string name; <br/> private string age; <br/> private SEASON2 (string name) {<br/> This. name = Name; <br/>}< br/> Public String getname () {<br/> return this. name; <br/>}< br/> Public void setage (string age) {<br/> This. age = age; <br/>}< br/> Public String getage () {<br/> return this. age; <br/>}</P> <p> Class A {}< br/> // Enum season3 extends a {}// error enumeration cannot inherit one class <br/> // Class B extends SEASON2 {}// error class cannot inherit an enumeration <br/> interface Ia {<br/> Public void print (); <br/>}</P> <p> // enumeration implementation interface: 1: The interface method is implemented by the enumeration object. 2: enumeration objects can be implemented without interfaces. 3: When an external call is not implemented, no error is reported. <br/> Enum season3 implements Ia {<br/> spring {<br/> Public void print () {<br/>}< br/> }, <br/> summer, <br/> autumn, <br/> Winter; <br/> Public void print () {}< br/>}</P> <p> Enum operator {<br/> Add ("plus") {<br/> Public int calculate (int I, int J) {<br/> return I + J; <br/>}< br/>}, <br/> minus (" ") {<br/> Public int calculate (int I, Int J) {<br/> return I-j; <br/>}< br/> }, <br/> multiply ("multiplication") {<br/> Public int calculate (int I, Int J) {<br/> return I * J; <br/>}< br/>}, <br/> divide ("except") {<br/> Public int calculate (int I, Int J) {<br/> return I/J; <br/>}< br/>}; <br/> private string name; <br/> private operator (string name) {<br/> This. name = Name; <br/>}< br/> Public String getname () {<br/> return this. name; <br/>}< br/> public abstract int calculate (int I, Int J); <br/>}</P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.