C# 虚拟财产

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9238795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:30:03  来源:igfitidea点击:

virtual properties

c#

提问by Mackintoast

I have used and learned only virtual methods of the base class without any knowledge of virtual properties used as

我只使用和学习了基类的虚拟方法,而没有任何用作虚拟属性的知识

class A
{
   public virtual ICollection<B> prop{get;set;}
}

Could someone tell me what that means ?

有人能告诉我这是什么意思吗?

采纳答案by Zenexer

public virtual ICollection<B> Prop { get; set; }

Translates almost directly to:

几乎直接翻译为:

private ICollection<B> m_Prop;

public virtual ICollection<B> get_Prop()
{
    return m_Prop;
}

public virtual void set_Prop(ICollection<B> value)
{
    m_Prop = value;
}

Thus, the virtual keyword allows you to override the property in sub-classes just as you would the above get/set methods:

因此,virtual 关键字允许您像上面的 get/set 方法一样覆盖子类中的属性:

public override ICollection<B> Prop
{
    get { return null; }
    set { }
}

回答by Chriseyre2000

It's a collection that's implementation can vary in a descendant class.

这是一个集合,其实现在后代类中可能会有所不同。

回答by loodakrawa

Properties are a shortened form of accessor methods (Get & Set). That means that the virtual keyword has the same meaning as with any other method. That means you can override it in derived classes.

属性是访问器方法(Get & Set)的缩写形式。这意味着 virtual 关键字与任何其他方法具有相同的含义。这意味着您可以在派生类中覆盖它。

回答by Tarik

Properties are actually specials cases of Getter and Setter methods. So they are like combinations of Getter and Setter methods as shown below:

属性实际上是 Getter 和 Setter 方法的特殊情况。所以它们就像 Getter 和 Setter 方法的组合,如下所示:

private string _name;

public string GetName()
{
   return _name;
}

public void SetName(string value)
{
   this._name = value;
}

So virtualkeyword is same for properties as well which means it is overrideable by the child classes and initial implementation can be changed.

所以virtual关键字对于属性也是相同的,这意味着它可以被子类覆盖并且可以更改初始实现。

回答by Husein Roncevic

In Entity Framework (which I believe your example refers to), your POCO classes are created and wrapped into a proxy class. Proxy class is a descendant of the class that you declare, so your class A becomes a base class. This proxy class is populated with data and returned back to you. This is necessary in order to track changes. Have a look at this article http://technet.microsoft.com/en-us/query/dd456848

在实体框架(我相信你的例子所指的)中,你的 POCO 类被创建并包装到一个代理类中。代理类是您声明的类的后代,因此您的类 A 成为基类。这个代理类填充了数据并返回给您。这是跟踪更改所必需的。看看这篇文章http://technet.microsoft.com/en-us/query/dd456848

I had a similar problem in trying to understand this and after a few debugging sessions and seeing the proxy classes and reading about tracking changes it made be figure out why it is declared the way it is.

我在尝试理解这一点时遇到了类似的问题,经过几次调试会话并查看代理类并阅读有关跟踪更改的内容后,我才弄清楚为什么它是这样声明的。

回答by Stephane Halimi

You can have methods (often), properties, indexers or events, the virtual keyword has the same meaning : modifying the meaning (override) of the base class item. With properties, you can change the get/set accessors.

您可以拥有方法(通常)、属性、索引器或事件,virtual 关键字具有相同的含义:修改基类项的含义(覆盖)。使用属性,您可以更改 get/set 访问器。

回答by Behnoud Sherafati

In object-oriented programming, a virtual property is a property whose behavior can be overridden within an inheriting class. This concept is an important part of the polymorphism portion of object-oriented programming (OOP).

在面向对象的编程中,虚拟属性是一种可以在继承类中覆盖其行为的属性。这个概念是面向对象编程 (OOP) 多态性部分的重要组成部分。

look at the example below:

看看下面的例子:

public class BaseClass
{

    public int Id { get; set; }
    public virtual string Name { get; set; }

}

public class DerivedClass : BaseClass
{
    public override string Name
    {
        get
        {
            return base.Name;
        }

        set
        {
            base.Name = "test";
        }
    }
}

at the presentation level:

在演示级别:

        DerivedClass instance = new DerivedClass() { Id = 2, Name = "behnoud" };

        Console.WriteLine(instance.Name);

        Console.ReadKey();

the output will be "test" because the "Name" property has been overridden in the derived class(sub class).

输出将是“test”,因为“Name”属性已在派生类(子类)中被覆盖。