Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.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中#_C#_Delphi_C# 4.0_Types_Accessor - Fatal编程技术网

C# 不带'的属性;属性类型';在C中#

C# 不带'的属性;属性类型';在C中#,c#,delphi,c#-4.0,types,accessor,C#,Delphi,C# 4.0,Types,Accessor,我正在将Delphi代码转换为C# 我有一个复杂的类结构,其中一个类是其所有子类的主要“主干” 在Delphi中,我可以用类型定义私有/受保护字段,并用相同类型定义该字段的属性,而不再在子类中写入该类型 下面是一个bit(和功能)示例: 程序项目1; {$APPTYPE控制台} 使用 SysUtils; 类型 父=类 严格保护 _myFirstField:Int64; 公众的 属性MyFirstField:Int64 write\u MyFirstField; 结束; Child1=类(父) 公

我正在将Delphi代码转换为C#

我有一个复杂的类结构,其中一个类是其所有子类的主要“主干”

在Delphi中,我可以用类型定义私有/受保护字段,并用相同类型定义该字段的属性,而不再在子类中写入该类型

下面是一个bit(和功能)示例:

程序项目1;
{$APPTYPE控制台}
使用
SysUtils;
类型
父=类
严格保护
_myFirstField:Int64;
公众的
属性MyFirstField:Int64 write\u MyFirstField;
结束;
Child1=类(父)
公众的
//继承写入/设置行为。。
//它不需要在所有子类上定义类型“over And over”。
//
//******注意此处的MyFirstField未键入************
属性MyFirstField已读\u MyFirstField;//将读取行为添加到属性。
结束;
变量
Child1状态:Child1;
开始
Child1Instance:=Child1.Create;

//Child1Instance.MyFirstField:=“一个字符串” 不,没有。公共API上的类型必须是显式的。唯一不明确的时间是使用
var
,它仅限于方法变量

此外,您不能更改C#中的签名(在子类中添加公共getter)-您必须重新声明它:

// base type 
protected string Foo {get;set;}

// derived type
new public string Foo {
    get { return base.Foo; }
    protected set { base.Foo = value; }
}

但正如新的
所示:这是一个不相关的属性,不需要具有相同的类型。

据我所知,您可以这样做:

public class Parent
{
    protected Int64 MyCounter{ get; set; }
}

public class Child : Parent
{
    protected string ClassName 
   { 
        get 
        {
            return "Child";
        }
    }
}

public class Runner
{
    static void Main(string[] args)
    {
        var c = new Child();
        c.Counter++;

        Console.WriteLIne(c.Counter);
    }
}

请展示你用C#试过的东西。按照通常的OOP,您可以在基类中有一个受保护的属性,这将与set/get一起在子类中可用,并保留其类型定义。C#private/protected properties行为不是完全相同吗?确实,我必须继续我想做的事情:我在祖先类,我只想“发布”子类上的一组属性。没有重新申报全部财产。。。既不是类型也不是访问器。不,在C#中没有模式来增加派生类中成员的可见性。你需要像Marc Gravell的回答中那样创建一个全新的属性。谢谢Marc。。。。然后,我必须为“相同”Foo属性重写所有行为,即使这种行为是相同的,不是吗-(@FerPt-no,只需代理
base。
就像我的例子一样
public class Parent
{
    protected Int64 MyCounter{ get; set; }
}

public class Child : Parent
{
    protected string ClassName 
   { 
        get 
        {
            return "Child";
        }
    }
}

public class Runner
{
    static void Main(string[] args)
    {
        var c = new Child();
        c.Counter++;

        Console.WriteLIne(c.Counter);
    }
}