栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在C ++中编写具有多个数据字段的类Java枚举类?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在C ++中编写具有多个数据字段的类Java枚举类?

模拟Java枚举的一种方法是使用私有构造函数创建一个类,该类将自身的副本实例化为静态变量:

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);  // ...

然后,您可以使用如下枚举:

double gravityonMercury = Planet::MERCURY.SurfaceGravity();


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595321.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号