标签:not turn sdn summary 资料 tor rar net weight
将Cat实例对象转换为Animal实例对象
在Animal类中实现自定义类型转换
/// <summary>
/// 隐式转换
/// </summary>
/// <param name="cat"></param>
public static implicit operator Animal(Cat cat)
{
return new Animal(cat.Name, cat.Weight);
}
/// <summary>
/// 显示转换:不支持隐式转换的写法
/// </summary>
/// <param name="cat"></param>
//public static explicit operator Animal(Cat cat)
//{
// return new Animal(cat.Name, cat.Weight);
//}
类型转换
Cat cat = new Cat("喵喵",15);
var animal = (Animal)cat;//显示转换
Animal animal12 = cat;//隐式转换(explicit写法不支持)
C#/.NET 隐式显式转换(自定义类型强制转换)——explicit,implicit
标签:not turn sdn summary 资料 tor rar net weight
原文地址:https://www.cnblogs.com/Lulus/p/12521144.html