C#和Java枚举(对于那些新的C#) [英] C# vs Java Enum (for those new to C#)

查看:129
本文介绍了C#和Java枚举(对于那些新的C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的Java编程了一段时间,刚刚抛到了完全的C#编写的一个项目。我想拿出来加速在C#中,并注意到我的新项目中多处使用枚举,但乍看之下,C#的枚举似乎是比Java 1.5 +实现更简单。任何人都可以枚举C#和Java枚举,以及如何克服分歧之间的区别是什么? (我不想开始语言火焰战争,我只是想知道如何做到在C#中的一些事情,我用在Java中做的)。例如,可以有人张贴C#对应于Sun的著名行星枚举的例子吗?

 公共枚举星球{
  MERCURY(3.303e + 23,2.4397e6)
  VENUS(4.869e + 24,6.0518e6)
  EARTH(5.976e + 24,6.37814e6)
  MARS(6.421e + 23,3.3972e6)
  JUPITER(1.9E + 27,7.1492e7)
  SATURN(5.688e + 26,6.0268e7)
  优瑞纳斯(8.686e + 25,2.5559e7)
  海王星(1.024e + 26,2.4746e7)
  PLUTO(1.27e + 22,1.137e6);  私人最终双质量; //(千克)
  私人最终双半径; //米
  星球(双质体,双半径){
      this.mass =质量;
      this.radius =半径;
  }
  公共双质体(){返回质量; }
  公共双半径(){返回半径; }  //万有引力常数(立方米千克-1 S-2)
  公共静态最后的双G = 6.67300E-11;  公共双surfaceGravity(){
      返回G *量/(半径*半径);
  }
  公共双surfaceWeight(双otherMass){
      返回otherMass * surfaceGravity();
  }
}//用法示例(Sun的例子稍作修改):
公共静态无效的主要(字串[] args){
    星球pEarth = Planet.EARTH;
    双earthRadius = pEarth.radius(); //只是把它展现用法    //参数传递是地球重量。重量计算在每个星球:
    双earthWeight = Double.parseDouble(参数[0]);
    双质量= earthWeight / pEarth.surfaceGravity();
    对于(行星号码:Planet.values​​())
       System.out.printf(您在%s重量%F%N,
                         P,p.surfaceWeight(质量));
}//输出示例:
$ java的星球175
你在水星上的重量为66.107583
你的金星上的重量158.374842
[等等...]


解决方案

在CLR枚举被简单地命名为常数。基础类型必须是整数。在Java中枚举是更像是一种类型的命名实例。这种类型可以是相当复杂的, - 因为你的例子显示 - 包含各种类型的多个字段

要口的例子为C#我只想改变枚举的不可变类和揭露这个类的静态只读实例:

 使用系统;
使用System.Collections.Generic;命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            星球pEarth = Planet.MERCURY;
            双earthRadius = pEarth.Radius; //只是把它展现用法            双earthWeight = double.Parse(123);
            双质量= earthWeight / pEarth.SurfaceGravity();
            的foreach(在Planet.Values​​星球P)
                Console.WriteLine(你的体重{0} {1},P,p.SurfaceWeight(质量));            Console.ReadKey();
        }
    }    公共类行星
    {
        公共静态只读水星=新行星(水星,3.303e + 23,2.4397e6);
        公共静态只读金星=新行星(维纳斯,4.869e + 24,6.0518e6);
        公共静态只读地球=新行星(大地,5.976e + 24,6.37814e6);
        公共静态只读火星=新行星(火星人,6.421e + 23,3.3972e6);
        公共静态只读木星=新行星(木星,1.9E + 27,7.1492e7);
        公共静态只读土星=新行星(土星,5.688e + 26,6.0268e7);
        公共静态只读天王星=新行星(天王星,8.686e + 25,2.5559e7);
        公共静态只读海王星=新行星(海王星,1.024e + 26,2.4746e7);
        公共静态只读冥王星=新行星(冥王星,1.27e + 22,1.137e6);        公共静态的IEnumerable<&星球GT;值
        {
            得到
            {
                产量返回MERCURY;
                产生回报VENUS;
                产生回地球;
                产生回报MARS;
                产生回报JUPITER;
                产生回报SATURN;
                产生回报天王星
                产生回报NEPTUNE;
                产生回报PLUTO;
            }
        }        私人只读字符串名称;
        私人只读双质量; //(千克)
        私人只读双半径; //米        星球(字符串名称,双质体,双半径)
        {
            this.name =名称;
            this.mass =质量;
            this.radius =半径;
        }        公共字符串名称{{返回名称; }}        公共双质体{{返回质量; }}        公共双半径{{返回半径; }}        //万有引力常数(立方米千克-1 S-2)
        公共常量双G = 6.67300E-11;        公共双SurfaceGravity()
        {
            返回G *量/(半径*半径);
        }        公共双SurfaceWeight(双otherMass)
        {
            返回otherMass * SurfaceGravity();
        }        公共重写字符串的ToString()
        {
            返回名称;
        }
    }
}

I've been programming in Java for a while and just got thrown onto a project that's written entirely in C#. I'm trying to come up to speed in C#, and noticed enums used in several places in my new project, but at first glance, C#'s enums seem to be more simplistic than the Java 1.5+ implementation. Can anyone enumerate the differences between C# and Java enums, and how to overcome the differences? (I don't want to start a language flame war, I just want to know how to do some things in C# that I used to do in Java). For example, could someone post a C# counterpart to Sun's famous Planet enum example?

public enum Planet {
  MERCURY (3.303e+23, 2.4397e6),
  VENUS   (4.869e+24, 6.0518e6),
  EARTH   (5.976e+24, 6.37814e6),
  MARS    (6.421e+23, 3.3972e6),
  JUPITER (1.9e+27,   7.1492e7),
  SATURN  (5.688e+26, 6.0268e7),
  URANUS  (8.686e+25, 2.5559e7),
  NEPTUNE (1.024e+26, 2.4746e7),
  PLUTO   (1.27e+22,  1.137e6);

  private final double mass;   // in kilograms
  private final double radius; // in meters
  Planet(double mass, double radius) {
      this.mass = mass;
      this.radius = radius;
  }
  public double mass()   { return mass; }
  public double radius() { return radius; }

  // universal gravitational constant  (m3 kg-1 s-2)
  public static final double G = 6.67300E-11;

  public double surfaceGravity() {
      return G * mass / (radius * radius);
  }
  public double surfaceWeight(double otherMass) {
      return otherMass * surfaceGravity();
  }
}

// Example usage (slight modification of Sun's example):
public static void main(String[] args) {
    Planet pEarth = Planet.EARTH;
    double earthRadius = pEarth.radius(); // Just threw it in to show usage

    // Argument passed in is earth Weight.  Calculate weight on each planet:
    double earthWeight = Double.parseDouble(args[0]);
    double mass = earthWeight/pEarth.surfaceGravity();
    for (Planet p : Planet.values())
       System.out.printf("Your weight on %s is %f%n",
                         p, p.surfaceWeight(mass));
}

// Example output:
$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
[etc ...]

解决方案

Enumerations in the CLR are simply named constants. The underlying type must be integral. In Java an enumeration is more like a named instance of a type. That type can be quite complex and - as your example shows - contain multiple fields of various types.

To port the example to C# I would just change the enum to an immutable class and expose static readonly instances of that class:

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Planet pEarth = Planet.MERCURY;
            double earthRadius = pEarth.Radius; // Just threw it in to show usage

            double earthWeight = double.Parse("123");
            double mass = earthWeight / pEarth.SurfaceGravity();
            foreach (Planet p in Planet.Values)
                Console.WriteLine("Your weight on {0} is {1}", p, p.SurfaceWeight(mass));

            Console.ReadKey();
        }
    }

    public class Planet
    {
        public static readonly Planet MERCURY = new Planet("Mercury", 3.303e+23, 2.4397e6);
        public static readonly Planet VENUS = new Planet("Venus", 4.869e+24, 6.0518e6);
        public static readonly Planet EARTH = new Planet("Earth", 5.976e+24, 6.37814e6);
        public static readonly Planet MARS = new Planet("Mars", 6.421e+23, 3.3972e6);
        public static readonly Planet JUPITER = new Planet("Jupiter", 1.9e+27, 7.1492e7);
        public static readonly Planet SATURN = new Planet("Saturn", 5.688e+26, 6.0268e7);
        public static readonly Planet URANUS = new Planet("Uranus", 8.686e+25, 2.5559e7);
        public static readonly Planet NEPTUNE = new Planet("Neptune", 1.024e+26, 2.4746e7);
        public static readonly Planet PLUTO = new Planet("Pluto", 1.27e+22, 1.137e6);

        public static IEnumerable<Planet> Values
        {
            get
            {
                yield return MERCURY;
                yield return VENUS;
                yield return EARTH;
                yield return MARS;
                yield return JUPITER;
                yield return SATURN;
                yield return URANUS;
                yield return NEPTUNE;
                yield return PLUTO;
            }
        }

        private readonly string name;
        private readonly double mass;   // in kilograms
        private readonly double radius; // in meters

        Planet(string name, double mass, double radius)
        {
            this.name = name;
            this.mass = mass;
            this.radius = radius;
        }

        public string Name { get { return name; } }

        public double Mass { get { return mass; } }

        public double Radius { get { return radius; } }

        // universal gravitational constant  (m3 kg-1 s-2)
        public const double G = 6.67300E-11;

        public double SurfaceGravity()
        {
            return G * mass / (radius * radius);
        }

        public double SurfaceWeight(double otherMass)
        {
            return otherMass * SurfaceGravity();
        }

        public override string ToString()
        {
            return name;
        }
    }
}

这篇关于C#和Java枚举(对于那些新的C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆