Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法通过使用关键字";确定如何调用属性访问器;这";_C# - Fatal编程技术网

C# 无法通过使用关键字";确定如何调用属性访问器;这";

C# 无法通过使用关键字";确定如何调用属性访问器;这";,c#,C#,提前谢谢各位。我对编码很陌生,所以请耐心等待。下面是我正在编写的一段代码。这是下面的完整代码。内容如下: using System; public class Product { private string name; private decimal price; public string Name { get { return name; } set { name = value; }

提前谢谢各位。我对编码很陌生,所以请耐心等待。下面是我正在编写的一段代码。这是下面的完整代码。内容如下:

using System;

public class Product
{

    private string name;
    private decimal price;


    public string Name
    {
        get
        { return name; }
        set
        { name = value; }
    }

    public decimal Price
    {
        get
        { return price; }
        set
        { price = value; }
    }



    public Product(string name, decimal price)
    {

        this.name = name;
        this.price = price;
    }
}
 private void Page_Load(object sender, EventArgs e)

    {

        Product saleProduct = new Product("Kitchen Garbage", 49.99M);

    }
在page load方法中,实例化一个对象并按如下方式调用构造函数:

using System;

public class Product
{

    private string name;
    private decimal price;


    public string Name
    {
        get
        { return name; }
        set
        { name = value; }
    }

    public decimal Price
    {
        get
        { return price; }
        set
        { price = value; }
    }



    public Product(string name, decimal price)
    {

        this.name = name;
        this.price = price;
    }
}
 private void Page_Load(object sender, EventArgs e)

    {

        Product saleProduct = new Product("Kitchen Garbage", 49.99M);

    }
创建产品时,使用上述值初始化构造函数。现在,如果查看构造函数,您有:

this.name = name;
this.name = name;
this.name = name;
this.name = name;
this.name = name;
this.name = name;

执行上述两行代码后,属性访问器会自动为私有变量(“name”和“price”)分配预期值

那么,台词是怎样的呢

能够调用名为“名称”和“价格”的公共访问器吗

我只看到当显式调用公共访问器时,例如:

saleProduct.Name = "Kitchen Garbage";
我不明白如何:

可以直接访问私有变量。我认为属性访问器的全部要点是,您必须通过属性名称访问私有变量,例如:

saleProduct.Name = "Kitchen Garbage";
你能帮我解释一下怎么做吗

是否能够访问公共属性?当执行上述两行代码时,甚至属性的名称(即“name”和“Price”)也分别更改为“Kitchen Garbage”和“49.99”

还有,你是怎么做的

知道要调用哪个公共访问器吗

我到处寻找解释,但运气不好


谢谢。

此名称
指的是
私有字符串名称。它是私有的,因此只有类中的代码才能引用它

之所以需要
this.
前缀,是因为如果没有前缀,代码将不明确,因为还有一个名为
name
的参数

公共访问器
公共字符串名称
没有自己的存储。它传递到私有字段
name

代码
this.name=name不接触属性
名称
-相反

当您执行
this.name=name
,您正在设置名为
name
的私有字段的值

无论何时访问公共属性
Name
,都是在间接访问同一字段(
Name

查看属性的定义:

get { return name; }

请注意,此处,
name
所指的字段与
this.name
所指的字段相同。您可以将其更改为
this.name
,但不会产生任何效果。这里不需要它,因为没有歧义。

在构造函数中,您不是通过公共setter设置值,而是直接设置私有支持字段

仅供参考,我认为通常认为最好的做法是用大写字母命名类级项目(有时在成员/支持字段的情况下用下划线),用小写字母命名函数级变量和参数。(不要在这一段中引用我的话:P)

因此,要通过setter设置字段,只需执行以下操作:

public class Foo
{
    private string _bar;
    public string Bar
    {
        set
        {
            this._bar = value;
        }    
        get
        {
            return _bar;
        }
    }

    public Foo(string bar)
    {
        this.Bar = bar;
    }
}
vs

以上内容与您在代码中所做的是等效的。请注意,对于您当前的实现来说,这并没有真正的区别,因为您的setter中没有额外的逻辑。然而,如果您这样做了,那么在您的实现中,您将错过额外的逻辑。 请注意,我对变量做了一些更改,以使其更清楚地显示正在执行的操作

证明这一点考虑如下:

public class Foo

    private string _bar;
    public string Bar
    {
        set
        {
            this._bar = value + " blah blah";
        }    
        get
        {
            return _bar;
        }
    }

    public Foo()
    {
        this.Bar = "test";
        Console.WriteLine(Bar); // outputs "test blah blah"

        this._bar = "test";
        Console.WriteLine(Bar); // outputs "test"
    }
}

私有成员
name
price
可以通过类
Product
中的所有非静态方法访问。由于
Product
的构造函数是类的一部分,因此也可以在那里访问成员

您的构造函数未使用公共属性
名称
价格
。它们从相应的私有成员检索其值

在产品类的构造函数中使用。可以从构造函数(以及类上的任何非静态方法)访问私有成员

基本上,当您“新建”一个对象时,会分配该对象的内存结构,然后调用构造函数以允许对该对象进行任何初始化。将name和price成员变量设为私有只意味着您只能从类成员本身内部访问它们,构造函数就是其中之一


这里实现的公共属性Name和Price只是这些私有名称和Price变量的包装器,允许您在设置或获取时执行其他操作(想象一下想要计算一个值被引用的次数:您可以在公共属性的get{}中增加一些计数器).

小写的
name
price
是公共
name
price
变量的私有“支持”变量。只能从类本身内部访问它们。之所以使用
this.price
,是因为该方法采用了一个名为“price”的参数,因此您需要指定您正在设置classes字段,而不是分配给局部变量。你经常在构造函数中看到这一点,就我个人而言,我会选择将参数命名为与我的类中的字段不同的名称,这样就没有必要了。

我到处搜索过
可疑:他们没有调用公共访问器。@FredBurnett,因为
name
name
中提取,当您设置
name
=“whatever”时,
name
现在返回值“whatever”Ok,我明白了。非常感谢。但是有一件事,当我使用debug逐步完成代码时,只要行this.name=name;当执行时,属性“Name”也将采用分配给“Name”的新值。那么,如果如您所说,构造函数没有使用公共属性名称和价格,那么为什么它们的值也会更改为构造函数中提供的值,为什么
public class Foo
{
    private string _bar;
    public string Bar
    {
        set
        {
            this._bar = value;
        }    
        get
        {
            return _bar;
        }
    }

    public Foo(string bar)
    {
        this.Bar = bar;
    }
}
public Foo(string bar)
{
    this._bar = bar
}
public class Foo

    private string _bar;
    public string Bar
    {
        set
        {
            this._bar = value + " blah blah";
        }    
        get
        {
            return _bar;
        }
    }

    public Foo()
    {
        this.Bar = "test";
        Console.WriteLine(Bar); // outputs "test blah blah"

        this._bar = "test";
        Console.WriteLine(Bar); // outputs "test"
    }
}
this.name = name;
this.price = price;