测试程序
例6是测试交易类型的程序,它会创建一个具体交易类型的数组、遍历此数组、调用每个元素的PostTransaction函数。插1是某次执行后的输出,默认使用的是美国式的日期时间格式,即为,月、日、年、12小时制。
例6:
| using namespace System; int main() { array<Transaction^>^ list = gcnew array<Transaction^> { gcnew Deposit(123.05, 12345), gcnew Transfer(Decimal::Parse("1256.40"), 1111, 222), gcnew Withdrawal(34.54, 232323), gcnew Deposit(56.12, 14321) }; for each (Transaction^ t in list) { t->PostTransaction(); } } |
插1:例6某次执行后的输出
| 3/20/2005 12:36:16 AM -- Dep: 123.05 12345 3/20/2005 12:36:18 AM -- Xfer: 1256.40 222 1111 3/20/2005 12:36:19 AM -- With: 34.54 232323 3/20/2005 12:36:21 AM -- Dep: 56.12 14321 |
枚举与继承
一个CLI enum类型通常实现为一个值类型,且隐式继承自System::Enum。同样地,此类型的静态与实例成员,它们的基类System::ValueType与类型的基类System::Object,在CLI enum类型或此类型的任意实例中,都可以访问到。插2是例7的输出。
例7:
| using namespace System; public enum class Color {Black, White, Red}; public enum class TransactionType : unsigned char {Deposit, Withdrawal, Transfer}; int main() { Color c = Color::White; /*1*/ Console::WriteLine("Color::Red's name is >{0}<", Enum::GetName(c.GetType(), Color::Red)); Console::Write("Color's members are:"); /*2*/ array<String^>^ names = Enum::GetNames(Type::GetType("Color")); for each (String^ s in names) { Console::Write(" {0}", s); } Console::WriteLine(); /*3*/ Console::WriteLine("The type underlying Color is >{0}<", Enum::GetUnderlyingType(Color::typeid)); /*4*/ Console::WriteLine("The type underlying TransactionType is >{0}<", Enum::GetUnderlyingType(TransactionType::typeid)); } |
| Color::Red's name is >Red< Color's members are: Black White Red The type underlying Color is >System.Int32< The type underlying TransactionType is >System.Byte< |

