c#中object的用法
请大家帮忙看一下下面的这个程序usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSyst...
请大家帮忙看一下下面的这个程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace object_sample
{
class object_sample
{
public int i = 10;
}
class MainClass
{
static void Main(string[] args)
{
object a;
a = 100;
Console.WriteLine(a);
Console.WriteLine(a.GetType());
Console.WriteLine(a.ToString());
a=new object_sample();
object_sample classRef;
classRef=(object_sample)a;
Console.WriteLine(classRef.i);
}
}
}
后四行不明白,还请各位高手指点一下,分行解释,越详细越好,麻烦了 展开
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace object_sample
{
class object_sample
{
public int i = 10;
}
class MainClass
{
static void Main(string[] args)
{
object a;
a = 100;
Console.WriteLine(a);
Console.WriteLine(a.GetType());
Console.WriteLine(a.ToString());
a=new object_sample();
object_sample classRef;
classRef=(object_sample)a;
Console.WriteLine(classRef.i);
}
}
}
后四行不明白,还请各位高手指点一下,分行解释,越详细越好,麻烦了 展开
2个回答
展开全部
首先你要明白一点:object是所有类的基类
a=new object_sample(); 把a实例化为object_sample
object_sample classRef; 定义一个object_sample类型的变量classRef
classRef=(object_sample)a; 把a赋值给classRef
Console.WriteLine(classRef.i);输出classRef实例中的i值
例外:
(object_sample)a是强制转化的格式
a=new object_sample(); 把a实例化为object_sample
object_sample classRef; 定义一个object_sample类型的变量classRef
classRef=(object_sample)a; 把a赋值给classRef
Console.WriteLine(classRef.i);输出classRef实例中的i值
例外:
(object_sample)a是强制转化的格式
展开全部
a=new object_sample();
//因为a是一个object类型,所以可以直接new一个object_sample类型,并赋值给a,这个是个向上转型的操作.
object_sample classRef;
//创建另一个object_sample类型的对象:classRef.
classRef=(object_sample)a;
//将a赋值给classRef,这个是向下转型了.a是object类型的对象,但a实际为object_sample类型的,所以可以向下转型(强制转换)并赋值给同样为object_sample类型的对象classRef
Console.WriteLine(classRef.i);
//....
//因为a是一个object类型,所以可以直接new一个object_sample类型,并赋值给a,这个是个向上转型的操作.
object_sample classRef;
//创建另一个object_sample类型的对象:classRef.
classRef=(object_sample)a;
//将a赋值给classRef,这个是向下转型了.a是object类型的对象,但a实际为object_sample类型的,所以可以向下转型(强制转换)并赋值给同样为object_sample类型的对象classRef
Console.WriteLine(classRef.i);
//....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询