给大家一道题,娱乐下。顺便散分

代码小天王 2012-06-27 05:33:21

public class testClass
{
public virtual string name
{
get { return "111"; }
}

public virtual void Method()
{
string s = this.name; //s是多少
}
}

public class testClass1 : testClass
{
public override string name
{
get
{
return "222";
}
}
public void aaa()
{
this.Method();
}

public override void Method()
{
base.Method();
}
}
testClass1 c1 = new testClass1();
c1.aaa();

...全文
3192 197 打赏 收藏 转发到动态 举报
写回复
用AI写文章
197 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码小天王 2012-07-07
  • 打赏
  • 举报
回复
222
[Quote=引用 194 楼 的回复:]

结果是空么.?
[/Quote]
Kogeo_Guan 2012-07-07
  • 打赏
  • 举报
回复
结果是空么.?
showlie 2012-07-07
  • 打赏
  • 举报
回复
如果你把函数的调用看成inline就比较明了了。
[Quote=引用 53 楼 的回复:]

是,我实例化的是testclass1,但是方法是在testClass中调用的,那在基类方法执行的时候,为什么基类方法中的this不是基类对象呢。
引用 49 楼 的回复:

引用 26 楼 的回复:
我的意思是为什么是testClass1而不是testClass,

引用 23 楼 的回复:

GetType显然是当前实例的类型。

否则所有对象GetType都会返回Ob……
[/Quote]
代码小天王 2012-07-06
  • 打赏
  • 举报
回复
obj.k 很明显是1 虽然Base和Middle都定义了一个名为Method1的方法 但是/Last重写的是Middle的Method1方法,那么在Base的Method1这个方法内部this是指向Last,并且调用Method1的时候,其实是调用方法表中的Base的Method1方法因为子类没有重写,所以为1
[Quote=引用 173 楼 的回复:]

class Base
{
public int k
{
get;
set;
}
public void Method()
{
k =……
[/Quote]
代码小天王 2012-07-06
  • 打赏
  • 举报
回复
还有 我知道你看书是怎么看的 你不就是喜欢照书抄代码么 作者是没提到指针传递 因为这只是个概念 并不是技术 没看到书中讲的方法中的this是指调用方的实例对象? 你不知道每次调用实例方法的步骤? 你不知道调用实例方法会把this实参传递给调用的方法? 我什么时候说C#生成的不是IL了 你怎么这么2,我从不喷人的 今天第一次喷了。。你连概念这2个字是什么意思都不知道 我都不知道怎么说你了 顺便劝你一句 少抄些代码 看书不是你那样看的 。。。。。
[Quote=引用 172 楼 的回复:]

薄发你妹啊
最近看CLR VIA C#
作者比你NB多了
都没说过指针传递这个话来
还有C#编译生成的是IL中间语言代码
你不能理解我说的?
智商有问题?
[/Quote]
代码小天王 2012-07-06
  • 打赏
  • 举报
回复
CSDN的学生这么牛逼? 我哪句话喷你了?
[Quote=引用 172 楼 的回复:]

薄发你妹啊
最近看CLR VIA C#
作者比你NB多了
都没说过指针传递这个话来
还有C#编译生成的是IL中间语言代码
你不能理解我说的?
智商有问题?
[/Quote]
言多必失 2012-07-06
  • 打赏
  • 举报
回复
唉真的是222
言多必失 2012-07-06
  • 打赏
  • 举报
回复
我不相信会是 222,决对是111, 我现在就要测试一下。

[Quote=引用楼主 的回复:]
C# code
public class testClass
{
public virtual string name
{
get { return "111"; }
}

public virtual void Method()
{
string s = this.name; //s是多少
}
}

public class testClass1 : testClass
{
public override string name
{
get
{
return "222";
}
}
public void aaa()
{
this.Method();
}

public override void Method()
{
base.Method(); }
}
testClass1 c1 = new testClass1();
c1.aaa();

[/Quote]
ls5879167 2012-07-06
  • 打赏
  • 举报
回复
s=222 我测试哦了 ,而且这段代码是有错的 c1.aaa()无答案的,应该把所以方法的返回值都改成string
代码小天王 2012-07-06
  • 打赏
  • 举报
回复
我咋水了[Quote=引用 184 楼 的回复:]

lz水货
[/Quote]
xiaoyao1212121 2012-07-04
  • 打赏
  • 举报
回复
认同他的....我也这样觉得!

[Quote=引用 44 楼 的回复:]
public override string name
{
get
{
return "222";
}
}

得到父类的name是"222",

public void aaa()
{
this.Method();
}

public override void Method()
{
base.Method();
……
[/Quote]
c2606050113 2012-07-04
  • 打赏
  • 举报
回复
貌似你这俩都是引用类型吧!
其实你想表达的是接口提供方法,继承类实现具体方法吧.基础类库都这么设计的
实例化
interface1 c1=new class();
[Quote=引用 10 楼 的回复:]

不是吧 testClass只是一个引用的类型 真正的数据类型是testClass1得嘛
引用 4 楼 的回复:

不是啦,因为你使用的时候定义的是testClass1,如果这样写:
testClass c1 = new testClass1();
c1.aaa();
那样才有可能会理解为111
[/Quote]
小陆瓶子 2012-07-04
  • 打赏
  • 举报
回复
弱弱的问下:

无论方法还是属性,只要基类是virtual修饰的,而子类中的方法是用override修饰,无论是子类还是父类中,当前的this都是指向实例化对象的。

不过new修饰的方法和属性不怎么熟悉,还需指教
Mark1- 2012-07-04
  • 打赏
  • 举报
回复
public class testClass
{
public virtual string name
{
get { return "111"; }
}

public virtual void Method()
{
string s = this.name; //s是多少
}
}

public class testClass1 : testClass
{
public new string name//here!!!
{
get
{
return "222";
}
}
public void aaa()
{
this.Method();
}

public override void Method()
{
base.Method();
}
}
testClass cc = new testClass1();
cc.aaa();//this will be 111
buzhidao945 2012-07-04
  • 打赏
  • 举报
回复
Mark1- 2012-07-04
  • 打赏
  • 举报
回复
public class testClass
{
public virtual string name
{
get { return "111"; }
}

public virtual void Method()
{
string s = this.name; //s是多少
}
}

public class testClass1 : testClass
{
public new string name//here!!!
{
get
{
return "222";
}
}
public void aaa()
{
this.Method();
}

public override void Method()
{
base.Method();
}
}
testClass1 c1 = new testClass1();
c1.aaa();//this will be 111
Mark1- 2012-07-04
  • 打赏
  • 举报
回复
目测222
前段时间研究这个了!理解类型引用和内存的分配就知道了!
岁月无情_1 2012-07-04
  • 打赏
  • 举报
回复
lz水货
cc19851 2012-07-03
  • 打赏
  • 举报
回复
这个题不用纠结this之类的,就是一个虚函数的覆盖,子类覆盖了虚函数,当然调用的是覆盖后的函数。
this指针就是一个函数参数,不是类的成员变量,不是在哪个CLASS里就是什么类型,指向的是托管堆上那块内存,所以 new testClass1()确定了this就是testClass1。
宿于松下 2012-07-03
  • 打赏
  • 举报
回复
class Base
{
public int k
{
get;
set;
}
public void Method()
{
k = this.Method1();
}
public virtual int Method1()
{
return 1;
}
}

class Middle:Base
{
public override int Method1()
{
return 2;
}
}

class Last:Middle
{
public new int Method1()
{
return 3;
}
}

static void Main(string[] args)
{
Base obj = new Last();
obj.Method();
Console.WriteLine("{0}",obj.k);
}
what is obj.k?
加载更多回复(155)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧