本文共 1052 字,大约阅读时间需要 3 分钟。
class Program{ static void Main(string[] args) { MyClass myClass=new MyClass(2); int value = (int)myClass; Console.WriteLine(value); string str = myClass; Console.WriteLine(str); int count = 20; MyClass myClassint = count; Console.WriteLine(myClassint.Value); perosn perosn=new perosn(); perosn.age = 100; MyClass newMyClass=new MyClass(); Console.WriteLine(newMyClass.Value); Console.ReadKey(); }}class MyClass{ private int value; public int Value { get => value; set => this.value = value; } public MyClass() { } public MyClass(int value) { this.Value = value; } //myclass 转为int类型 public static explicit operator int(MyClass myClass) { return myClass.Value; } //int转为class类 public static implicit operator MyClass(int value) { return new MyClass(value); } //class类转为string public static implicit operator string(MyClass myClass) { return myClass.ToString(); }}
转载地址:http://gbrxo.baihongyu.com/