定义显式类型转换和隐式类型转换
class Program
{
static void Main(string[] args)
{
Stone stone = new Stone();
stone.Age = 5000;
//Monkey wukongSun = stone;//调用隐式的类型转换
Monkey wukongSun = (Monkey)stone;//调用显式的类型转换
Console.WriteLine(wukongSun.Age);
}
}
class Stone
{
public int Age;
//定义一个隐式的类型转换用到关键字implicit
//定义一个显式的类型转换用到关键字explicit
public static explicit operator Monkey(Stone stone)
{
Monkey monkey = new Monkey();
monkey.Age = stone.Age / 500;
return monkey;
}
}
class Monkey
{
public int Age;
}