我刚开始学习C++。我正在尝试将我编写的Java应用程序转换为C++。我不明白的是如何将Enumeration类从Java重新创建为C++。我希望能够创建具有构造函数和toString()
方法的Enumeration对象。
下面是我的Java枚举的一个例子:
public enum InteractionType {
// Enumeration Values
PRINTACTIONS("Print Available Actions"),
PRINTBANKNAME("Display Bank Name"),
PRINTBANKACTIONS("Display Bank Actions"),
CREATEBRANCH("Create a Branch"),
CREATECUSTOMER("Create a Customer"),
READBRANCH("Query Bank Branches"),
READCUSTOMER("Query Customers and/or Customer Transactions"),
UPDATEBRANCH("Update Branch Name"),
UPDATECUSTOMER("Update Customer Information"),
DELETEBRANCH("Delete Branch"),
DELETECUSTOMER("Delete Customer"),
QUITAPP("Quit Application");
// Private Variables
private final String description;
// Constructor
InteractionType(String description) {
this.description = description;
}
// Methods
public String toString() {
return description;
}
}
然后,我将在主文件中创建一个方法,以从值数组中获取特定的InteractionType
。
private static InteractionType getSelectedAction() {
// Creates an array with all the values of the Enumeration Class
InteractionType[] interactionTypes = InteractionType.values();
System.out.print("Select an action: ");
int id = scanner.nextInt();
scanner.nextLine();
// Returns a specific InteractionType
return interactionTypes[id];
}
这是我尝试编写的类,用于在C++中重新创建Java枚举:
InteractionType.h
#include <iostream>
#include <string>
using namespace std;
class InteractionType {
public:
static const InteractionType PRINTACTIONS(string = "Print Actions");
static const InteractionType PRINTBANKNAME(string = "Print Bank Name");
static const InteractionType PRINTBANKACTIONS(string = "Print Bank Actions");
static const InteractionType CREATEBRANCH(string = "Create a Branch");
static const InteractionType CREATECUSTOMER(string = "Create a Customer");
static const InteractionType READBRANCH(string = "Read Branches");
static const InteractionType READCUSTOMER(string = "Read Customers");
static const InteractionType UPDATEBRANCH(string = "Update a Branch");
static const InteractionType UPDATECUSTOMER(string = "Update a Customer");
static const InteractionType DELETEBRANCH(string = "Delete a Branch");
static const InteractionType DELETECUSTOMER(string = "Delete a Customer");
static const InteractionType QUITAPP(string = "Quit Application");
private:
string description;
InteractionType(string description);
};
InteractionType.cpp
#include "InteractionType.h"
const InteractionType InteractionType::PRINTACTIONS(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::PRINTBANKNAME(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::PRINTBANKACTIONS(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::CREATEBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::CREATECUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::READBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::READCUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::UPDATEBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::UPDATECUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::DELETEBRANCH(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::DELETECUSTOMER(string description)
{
return InteractionType(description);
}
const InteractionType InteractionType::QUITAPP(string description)
{
return InteractionType(description);
}
InteractionType::InteractionType(string description) {
this->description = description;
}
1条答案
按热度按时间jc3wubiy1#
您的翻译有点错误。请尝试以下操作:
InteractionType.h
InteractionType.cpp
然后你可以这样使用它:
Online Demo