Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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/4/oop/2.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#_Oop_Abstract Class - Fatal编程技术网

C# 如何覆盖抽象属性?

C# 如何覆盖抽象属性?,c#,oop,abstract-class,C#,Oop,Abstract Class,如何覆盖抽象属性a 重写属性时,应同时编写get和set。在本例中,您创建了一个新的属性,该属性具有相同的名称,但具有另一个签名 您试图用字段覆盖属性 属性通过get和set值访问器提供对字段的访问。请阅读以更好地理解差异。因此,由于它们不同,IDE建议您使用new-关键字隐藏父属性 如果您想知道为什么new关键字在您的案例中不起作用,请阅读 改进你的设计 在您的问题中,代码中的PropertyA似乎需要在继承的类上设置,但不能从外部更改。所以,或许可以这样做: public abstract

如何覆盖抽象属性a


重写属性时,应同时编写
get
set
。在本例中,您创建了一个新的属性,该属性具有相同的名称,但具有另一个签名

您试图用字段覆盖属性

属性通过
get
set
值访问器提供对字段的访问。请阅读以更好地理解差异。因此,由于它们不同,IDE建议您使用
new
-关键字隐藏父属性

如果您想知道为什么
new
关键字在您的案例中不起作用,请阅读

改进你的设计 在您的问题中,代码中的
PropertyA
似乎需要在继承的类上设置,但不能从外部更改。所以,或许可以这样做:

public abstract class SampleA
{
    public abstract string PropertyA {get;set;}
}
public abstract class SampleA
{
    // no setter -> cant be changed after initialization
    public abstract string PropertyA { get; } 

    // protected setter -> can only be changed from inside SampleA or Sample
    public abstract string PropertyB { get; protected set; } 
}

public class Sample : SampleA
{
    public override string PropertyA { get; } = "override";
    public override string PropertyB { get; protected set; } = "override";
}
您当前的设计是如何实现的 这样做:

public abstract class SampleA
{
    public abstract string PropertyA {get;set;}
}
public abstract class SampleA
{
    // no setter -> cant be changed after initialization
    public abstract string PropertyA { get; } 

    // protected setter -> can only be changed from inside SampleA or Sample
    public abstract string PropertyB { get; protected set; } 
}

public class Sample : SampleA
{
    public override string PropertyA { get; } = "override";
    public override string PropertyB { get; protected set; } = "override";
}
甚至可以通过更多的行为来实现:

public class Sample : SampleA
{
    public override string PropertyA { get; set; } = "override";
}

你检查过了吗:你能解释一下你到底对什么感到困惑吗?你不能用
override
修饰符对它进行子类化并声明一个同名的属性吗?@Sweeper我试着像下面那样重写
公共类示例:samreak{public override string PropertyA=“override”}
但我遇到了一个问题,就是我不能重写,我需要使用新的关键字可能重复Thank@noMagnition,很清楚:-)``public override string PropertyA{get;set;}=“OK”```小挑剔。他没有创建一个新属性,而是创建了一个字段(因为没有
get
set
它只是一个字段。@Nagaraj嘿,我对我的答案做了一些改进,以更好地匹配您的估计需求和问题。请花些时间通读。:)