C#vs Java Enum(适用于C#的新手)
我一直在Java中编程一段时间,刚刚被抛到一个完全用C#编写的项目上。 我试图在C#中提高速度,并注意到在我的新项目中有几个地方使用了枚举,但乍一看,C#的枚举似乎比Java 1.5+实现更简单。 任何人都可以枚举C#和Java枚举之间的区别,以及如何克服差异? (我不想开始一场语言的火焰战,我只想知道如何在C#中做一些我曾经在Java中做过的事情)。 例如,有人可以发布与Sun着名的Planet枚举示例相对应的C#对象吗?
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 ...]
CLR中的枚举只是简单的命名常量。 基础类型必须是不可分割的。 在Java中,枚举更像是一个类型的命名实例。 这种类型可能相当复杂,并且 - 正如您的示例所示 - 包含多个不同类型的字段。
要将示例移植到C#,我只需将枚举更改为不可变类,并公开该类的静态只读实例:
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#中,你可以在枚举上定义扩展方法,这弥补了一些缺失的功能。
您可以将Planet
定义为枚举,并且也可以使用等效于surfaceGravity()
和surfaceWeight()
扩展方法。
我已经使用Mikhail建议的自定义属性,但使用字典也可以实现相同的属性。
using System;
using System.Reflection;
class PlanetAttr: Attribute
{
internal PlanetAttr(double mass, double radius)
{
this.Mass = mass;
this.Radius = radius;
}
public double Mass { get; private set; }
public double Radius { get; private set; }
}
public static class Planets
{
public static double GetSurfaceGravity(this Planet p)
{
PlanetAttr attr = GetAttr(p);
return G * attr.Mass / (attr.Radius * attr.Radius);
}
public static double GetSurfaceWeight(this Planet p, double otherMass)
{
return otherMass * p.GetSurfaceGravity();
}
public const double G = 6.67300E-11;
private static PlanetAttr GetAttr(Planet p)
{
return (PlanetAttr)Attribute.GetCustomAttribute(ForValue(p), typeof(PlanetAttr));
}
private static MemberInfo ForValue(Planet p)
{
return typeof(Planet).GetField(Enum.GetName(typeof(Planet), p));
}
}
public enum Planet
{
[PlanetAttr(3.303e+23, 2.4397e6)] MERCURY,
[PlanetAttr(4.869e+24, 6.0518e6)] VENUS,
[PlanetAttr(5.976e+24, 6.37814e6)] EARTH,
[PlanetAttr(6.421e+23, 3.3972e6)] MARS,
[PlanetAttr(1.9e+27, 7.1492e7)] JUPITER,
[PlanetAttr(5.688e+26, 6.0268e7)] SATURN,
[PlanetAttr(8.686e+25, 2.5559e7)] URANUS,
[PlanetAttr(1.024e+26, 2.4746e7)] NEPTUNE,
[PlanetAttr(1.27e+22, 1.137e6)] PLUTO
}
在C#中,属性可以与枚举一起使用。 这里有详细描述的编程模式的好例子(Codeproject)
public enum Planet
{
[PlanetAttr(3.303e+23, 2.4397e6)]
Mercury,
[PlanetAttr(4.869e+24, 6.0518e6)]
Venus
}
编辑:这个问题最近再次被问及由Jon Skeet回答:在C#中Java的枚举相当于什么? C#中的私有内部类 - 为什么不经常使用它们?
编辑2:查看接受的答案,以非常出色的方式扩展了这种方法!
链接地址: http://www.djcxy.com/p/91539.html上一篇: C# vs Java Enum (for those new to C#)
下一篇: State seems to be getting overwritten instead of merged