通过枚举类型获取对应数字和字符串, 将枚举类型封装到类中, 本文以Qt举例, 程序如下.
season.h
#ifndef SEASON_H
#define SEASON_H
#include <QStringList>
class Season
{
public:
enum SeasonType{
Spring, Summer, Autumn, Winter
};
static QString getItem(SeasonType type);
static int getValue(SeasonType type);
private:
static void initial();
//
static bool m_initFlag;
static QStringList m_seasonList;
};
#endif // SEASON_H
season.cpp
#include "season.h"
bool Season::m_initFlag = true;
QStringList Season:: m_seasonList;
QString Season::getItem(Season::SeasonType type)
{
initial();
return m_seasonList.at(type);
}
int Season::getValue(Season::SeasonType type)
{
return (int)type;
}
void Season::initial()
{
if(m_initFlag){
m_seasonList.append("spring");
m_seasonList.append("summer");
m_seasonList.append("autumn");
m_seasonList.append("winter");
m_initFlag = false;
}
}
main.cpp
#include <QDebug>
#include "season.h"
int main(int argc, char *argv[])
{
Season::SeasonType type = Season::Summer;
qInfo()<<"aaaa11"<<Season::getItem(type)<<Season::getValue(type);
return 0;
}
|