using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class A { public int a = 2; public void f(object obj) { Console.WriteLine("{0}", obj);//这儿不需要强制转换的 Console.WriteLine("{0}", (int)obj); } public static void f1(object obj) { A A2=(A)obj;//这儿必须强制转换的,否则见下图 Console.WriteLine("{0}", A2.a); } } class Program { static void Main(string[] args) { int b = 3; A A1 = new A(); A1.f(b); A.f1(A1); Console.ReadLine(); } } } //任何类型的值都可以被隐式转换为object类型!(装箱操作)
时间: 2024-11-01 09:06:21