English 中文(简体)
在C#子类中,我是如何凌驾于成员变量(领域)之上的,而不是隐藏的?
原标题:How do I override, not hide, a member variable (field) in a C# subclass?

I want this to tell me the Name of both ItemA and ItemB. It should tell me "Subitem Subitem", but instead it tells me "Item Subitem". This is because the "Name" variable defined in the Subitem class is technically a different variable than the "Name" variable defined in the base Item class. I want to change the original variable to a new value. I don t understand why this isn t the easiest thing ever, considering virtual and override work perfectly with methods. I ve been completely unable to find out how to actually override a variable.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine(ItemA.Name);
            System.Console.WriteLine(ItemB.Name);
            System.Console.ReadKey();
        }
        static Item ItemA = new Subitem();
        static Subitem ItemB = new Subitem();
    }
    public class Item
    {
        public string Name = "Item";
    }
    public class Subitem : Item
    {
        new public string Name = "Subitem";
    }
}
最佳回答

您不能在C#中的overe>变量中选取,但你可以压倒以下特性:

public class Item
{
    public virtual string Name {get; protected set;}
}
public class Subitem : Item
{
    public override string Name {get; protected set;}
}

另一种做法是改变次等值:

public class Item
{
    public string Name = "Item";
}
public class Subitem : Item
{
    public Subitem()
    {
        Name = "Subitem";
    }
}
问题回答

不存在多变概念(这就是override)。 做的是田地(你称之为变量)。 这里需要的是财产。

public class Item
{
    public virtual string Name
    {
        get { return "Item"; }
    }
}

public class SubItem : Item
{
    public override string Name
    {
        get { return "Subitem"; }
    }
}

“变数”的含义是什么?

变数是概念记忆中一个名列的位置(“概念”),因为它可能是一个登记册,尽管不大可能在田里。

过度使用一种方法可以替代一套操作。

由于记名的所在地没有一套行动,你如何替代? 期望在记忆中使用指定地点的东西是什么?

如果你想要改变记忆中储存的内容,那么,当衍生物的构造时就这样做了:

public class Item
{
  public string Name = "Item";
}
public class Subitem : Item
{
  public SubItem()
  {
    Name = "Subitem";
  }
}

如果你想要凌驾于一套行动之上,那么就界定了一套行动(一种方法或财产),以压倒一切。

这个问题相对旧,提交人可能使用C# 5或更低。 但是,我也有同样的问题,并达成了一项非常根本的解决办法,与C# 6.0和更高的一道工作,我想与大家分享:

C# 6.0使自动经营能力得以启动。 因此,人们只能使用:

public class Item
{
    public virtual string Name { get; set; } = "Item";
}

public class Subitem : Item
{
    public override string Name { get; set; } = "Subitem";
}

一种可能的解决办法是使用一种财产,凌驾于getter之上,如:

public class Item
{
    public virtual string Name { get { return "Item"; } }
}
public class Subitem : Item
{
    public override string Name { get { return "Subitem"; } }
}

与此类似,成员变量本身是相互继承的物体。 在进行了大量研究和细化之后,我为我提出了一个很好的解决办法。 在这里,我与大家分享,希望这将有助于其他人。 (如果是的话,我认识到,这是个老员额,但正如我对山洞的搜查一样。)

<>Mygoal> 是团结在编辑中显示每个项目类别的具体统计类别。 换言之,在选择显示武器储存时,我想有一个武器目标,在选择显示停战状态时,一个停战物体,而武器与军舰都有共同的变数,武器储存和军舰也有共同的变数。

具体地说,我试图使用新的关键词,以便(某类)基数变量能够被衍生类别变量(来自基数变量类别)所推翻。 如:

public class ItemStats {
    // variables like size, weight, etc
}

public class WeaponStats : ItemStats {
    // Additional variables like damage, rate of fire, etc
}

public class ArmorStats : ItemStats {
    // Additional variables like degree of damage reduction, etc

public class Item {
    public ItemStats stats;
    protected float degredation;
    // etc.
}

public class Weapon : Item {
    public new WeaponStats stats; // *The problem*
    protected Ammo ammo;
    // etc
}

public class Armor : Item {
    public new ArmorStats stats; // *Same problem*
    // etc
}

However, this didn t actually hide the base ItemStats variable and instead hid the new WeaponStats variable. As well as creating a lot of headache about what variable stats was referring to in the Weapon class. tldr it was a bad idea.

最后,在更多地了解继承情况之后,我最后提出了更好的解决办法:

// Make the base class abstract, so each item has its own subclass.
// This avoids having two variables for stats, the problem I was trying to avoid.
public abstract class Item {
    public abstract ItemStats Stats();
    // notice that there is no ItemStats variable here anymore
    protected float degredation;
    // etc.
}

public class Weapon : Item {
    public WeaponStats stats;
    protected Ammo ammo;

    // This function lets other items interface with its stats.
    public override ItemStats Stats() {
        return stats;
    }
    // etc
}

// The same idea for Armor as for Weapon

这使团结编辑只能够展示相应的雕像(例如,与军械和军械库交织的武器和军械库);使每个州都继承了所有物品共同的变数;还让所有物品都知道其他物品已经存在。

希望在其他方面:





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签