【发布时间】:2015-04-16 05:21:24
【问题描述】:
using System;
public class Shurli
{
public class MyTest
{
private string name = "qwe"; // the name field
public string Name // the Name property
{
get
{
return this.name;
}
}
}
public class yourJs : MyTest
{
private string name = "chah";
}
static void Main()
{
var met = new yourJs();
string acer = met.Name;
Console.WriteLine(acer);
}
}
我完全期望输出是“chah”,但输出是“qwe”。几个小时前我一直在尝试解决这个问题,但仍然没有弄清楚。有人可以帮忙吗?
Visual Studio 发出警告:“字段 'Shurli.yourJs.name' 已分配,但其值从未使用过”
【问题讨论】:
-
尝试设置断点?
-
您已将 yourJS.name 添加为私有 ..
-
当met.Name被调用时,
this.name不应该引用met.name吗? met 是从类 yourJs 实例化的对象,yourJs 包含代码private string name = "chah";。那么 met.name 不应该评估为“chah”吗? -
@bit
MyTest.name也是私有的
标签: c# variables inheritance variable-assignment