C#如何定义与类或结构之间的转换的代码
2021-01-09 07:30
标签:xpl ted stat not sys 代码 bsp operator roman 下边资料是关于C#如何定义与类或结构之间的转换的代码。 struct RomanNumeral class Test numeral = 10; Console.WriteLine((int)numeral); Console.WriteLine(numeral); Console.WriteLine(s); using System; struct RomanNumeral struct BinaryNumeral private int value; class Test C#如何定义与类或结构之间的转换的代码 标签:xpl ted stat not sys 代码 bsp operator roman 原文地址:https://www.cnblogs.com/whoamboys/p/13098511.html
using System;
{
public RomanNumeral(int value)
{
this.value = value;
}
static public implicit operator RomanNumeral(int value)
{
return new RomanNumeral(value);
}
static public explicit operator int(RomanNumeral roman)
{
return roman.value;
}
static public implicit operator string(RomanNumeral roman)
{
return("Conversion not yet implemented");
}
private int value;
}
{
static public void Main()
{
RomanNumeral numeral;
short s = (short)numeral;
}
}
{
public RomanNumeral(int value)
{
this.value = value;
}
static public implicit operator RomanNumeral(int value)
{
return new RomanNumeral(value);
}
static public implicit operator RomanNumeral(BinaryNumeral binary)
{
return new RomanNumeral((int)binary);
}
static public explicit operator int(RomanNumeral roman)
{
return roman.value;
}
static public implicit operator string(RomanNumeral roman)
{
return("Conversion not yet implemented");
}
private int value;
}
{
public BinaryNumeral(int value)
{
this.value = value;
}
static public implicit operator BinaryNumeral(int value)
{
return new BinaryNumeral(value);
}
static public implicit operator string(BinaryNumeral binary)
{
return("Conversion not yet implemented");
}
static public explicit operator int(BinaryNumeral binary)
{
return(binary.value);
}
}
{
static public void Main()
{
RomanNumeral roman;
roman = 10;
BinaryNumeral binary;
binary = (BinaryNumeral)(int)roman;
roman = binary;
Console.WriteLine((int)binary);
Console.WriteLine(binary);
}
}
文章标题:C#如何定义与类或结构之间的转换的代码
文章链接:http://soscw.com/index.php/essay/41116.html