如何在C ++中编写一个具有多个数据字段的类似Java枚举的类? [英] How to write a Java-enum-like class with multiple data fields in C++?
问题描述
来自Java背景,我发现C ++的枚举很跛脚。我想知道如何在C ++中编写类似Java的枚举(其中枚举值是对象,并且可以有属性和方法)。
例如,将以下Java代码(足以展示该技术的一部分)翻译为C ++:
public enum Planet {
MERCURY(3.303e + 23,2.4397e6),
VENUS(4.869e + 24,6.0518e6),
地球(5.976e + 24,6.37814e6),
MARS e + 23,3.3972e6),
JUPITER(1.9e + 27,7.1492e7),
SATURN(5.688e + 26,6.0268e7),
URANUS(8.686e + 25,2.5559 e7),
NEPTUNE(1.024e + 26,2.4746e7);
私人最终双质量// in kilograms
private final double radius; // in meters
Planet(double mass,double radius){
this.mass = mass;
this.radius = radius;
}
private double mass(){return mass; }
private double radius(){return radius; }
//通用引力常数(m3 kg-1 s-2)
public static final double G = 6.67300E-11;
double surfaceGravity(){
return G * mass /(radius * radius);
}
double surfaceWeight(double otherMass){
return otherMass * surfaceGravity();
}
public static void main(String [] args){
if(args.length!= 1){
System.err.println(用法:java Planet ; earth_weight>);
System.exit(-1);
}
double earthWeight = Double.parseDouble(args [0]);
double mass = earthWeight / EARTH.surfaceGravity();
for(Planet p:Planet.values())
System.out.printf(您在%s上的权重是%f%n,
p,p.surfaceWeight(mass)) ;
}
}
任何帮助将非常感谢!
谢谢!
模拟Java枚举的一种方法是创建一个类作为静态变量实例化自身副本的私有构造函数:
class Planet {
public:
/ /枚举值DECLARATIONS - 它们稍后定义
static const Planet MERCURY;
static const Planet VENUS;
// ...
private:
double mass; // in kilograms
double radius; // in meters
private:
Planet(double mass,double radius){
this-> mass = mass;
this-> radius = radius;
}
public:
//属性和方法到这里
};
//枚举值定义
//初始化发生在类的范围
//中,因此可以使用专用Planet构造函数。
const Planet Planet:MERCURY = Planet(3.303e + 23,2.4397e6);
const Planet Planet :: VENUS = Planet(4.869e + 24,6.0518e6);
// ...
然后你可以使用这样的枚举:
double gravityOnMercury = Planet :: MERCURY.SurfaceGravity();
Coming from a Java background, I find C++'s enums very lame. I wanted to know how to write Java-like enums (the ones in which the enum values are objects, and can have attributes and methods) in C++.
For example, translate the following Java code (a part of it, sufficient to demonstrate the technique) to 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);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java Planet <earth_weight>");
System.exit(-1);
}
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
}
Any help would be greatly appreciated!
Thanks!
One way to simulate Java enums is to create a class with a private constructor that instantiates copies of itself as static variables:
class Planet {
public:
// Enum value DECLARATIONS - they are defined later
static const Planet MERCURY;
static const Planet VENUS;
// ...
private:
double mass; // in kilograms
double radius; // in meters
private:
Planet(double mass, double radius) {
this->mass = mass;
this->radius = radius;
}
public:
// Properties and methods go here
};
// Enum value DEFINITIONS
// The initialization occurs in the scope of the class,
// so the private Planet constructor can be used.
const Planet Planet::MERCURY = Planet(3.303e+23, 2.4397e6);
const Planet Planet::VENUS = Planet(4.869e+24, 6.0518e6);
// ...
Then you can use the enums like this:
double gravityOnMercury = Planet::MERCURY.SurfaceGravity();
这篇关于如何在C ++中编写一个具有多个数据字段的类似Java枚举的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!