zoukankan      html  css  js  c++  java
  • C# 与 Java 中的枚举

    C#代码: 利用扩展方法,扩展枚举功能

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Linq;
    
    namespace EnumExtension
    {
        // Define an extension method in a non-nested static class.
        public static class Extensions
        {        
            public static Grades minPassing = Grades.D;
            public static bool Passing(this Grades grade)
            {
                return grade >= minPassing;
            }
        }
    
        public enum Grades { F = 0, D=1, C=2, B=3, A=4 };
        class Program
        {       
            static void Main(string[] args)
            {
                Grades g1 = Grades.D;
                Grades g2 = Grades.F;
                Console.WriteLine("First {0} a passing grade.", g1.Passing() ? "is" : "is not");
                Console.WriteLine("Second {0} a passing grade.", g2.Passing() ? "is" : "is not");
    
                Extensions.minPassing = Grades.C;
                Console.WriteLine("
    Raising the bar!
    ");
                Console.WriteLine("First {0} a passing grade.", g1.Passing() ? "is" : "is not");
                Console.WriteLine("Second {0} a passing grade.", g2.Passing() ? "is" : "is not");
            }
        }
      }
    }
    /* Output:
        First is a passing grade.
        Second is not a passing grade.
    
        Raising the bar!
    
        First is not a passing grade.
        Second is not a passing grade.
     */

    Java代码:

    package com.mmb.csharp;
    
    /**
     * 颜色枚举
     * 
     */
    public enum Color {
    
        /**
         * 1
         */
        RED("红色", 1),
        /**
         * 2
         */
        GREEN("绿色", 2), 
        /**
         * 3
         */
        BLACK("黑色", 3), 
        /**
         * 4
         */
        YELLOW("黄色", 4);
    
        // 成员变量
        private String name;
        private int index;
    
        // 构造方法
        private Color(String name, int index) {
            this.name = name;
            this.index = index;
        }
    
        // 普通方法
        public static String getName(int index) {
            for (Color c : Color.values()) {
                if (c.getIndex() == index) {
                    return c.name;
                }
            }
            return null;
        }
        
        public static Color getColor(int index) {
            for (Color c : Color.values()) {
                if (c.getIndex() == index) {
                    return c;
                }
            }
            return null;
        }
    
        // get set 方法
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getIndex() {
            return index;
        }
    
        public void setIndex(int index) {
            this.index = index;
        }
    }

    单单只着眼于枚举,Java要比C#做的好一些、要更加地方便使用。

  • 相关阅读:
    No Hibernate Session bound to thread, and configuration does not allow
    谈谈数据库中MyISAM与InnoDB区别
    hibernate实体的几种状态:
    解决Eclipse导出javadoc乱码问题
    freemarker截取字符串
    many-to-one和one-to-many的配置比较
    one-to-many many-to-one配置解释
    extends:类似于java中的继承特征,extends="struts-default"
    eclipse 中创建maven web项目
    java.lang.ClassNotFoundException: javax.persistence.EntityListeners
  • 原文地址:https://www.cnblogs.com/08shiyan/p/3554519.html
Copyright © 2011-2022 走看看