public static explicit operator Monkey(Stone stone) { }
public static implicit operator Monkey(Stone stone) { }
explicit MSDN
static void Main(string[] args)
{
Stone stone = new Stone();
Monkey m = (Monkey)stone;
m.Age = 5000;
Monkey wukongSun = (Monkey)stone; // explicit 明確轉型 (Monkey)
//Monkey wukongSun = stone; // implicit 隱含轉型
Console.WriteLine(wukongSun.Age);
}
class Stone
{
public int Age;
// Monkey wukongSun = (Monkey)stone;
public static explicit operator Monkey(Stone stone)
{
Monkey m = new Monkey();
m.Age = stone.Age / 500;
return m;
}
}
class Monkey
{
public int Age;
}
implicit MSDN
// Monkey wukongSun = stone;
public static implicit operator Monkey(Stone stone)
{
Monkey m = new Monkey();
m.Age = stone.Age / 500;
return m;
}
https://www.youtube.com/watch?v=q6yAgdzGAJo
沒有留言:
張貼留言