开发者

Rename variable on inheritance

I'm just wondering if it is possible..

I have a base class, with a variable called Level. I then derive a class from it, but here Level should be called Points.

Is there any way to rename the Level variable in the derived class?

Edit - Excuse me for calling a property a variable. I'm quite new to programming, 开发者_运维百科and especially to classes (Which our teacher doesn't teach us for some reason)


The closest you could get with inheritance would be:

class BaseClass
{
  protected int Level { get; set; }
}

class SubClass : BaseClass
{
  public int Points { get { return Level; } set { Level = value; } }
}

But that doesn't actually expose BaseClass.Level, since a protected property is only visible to subclasses.

As @Jason explains, a subclass can't obscure the public interface of its base class. If you really wanted to do that, you could encapsulate the base in a new object that exposes its properties:

class A
{
   public Foo { get; set; }
   public Bar { get; set; }
   public Level { get; set;}
}

class B
{
  private A _internalA;

  public Foo { get { return _internalA.Foo; }  set { _internalA.Foo = value; } }
  public Bar { get { return _internalA.Bar; }  set { _internalA.Bar= value; } }
  public Points { get { return _internalA.Level; }  set { _internalA.Level = value; } }
}

This is typically referred to as composition: B is composed of A, rather than inheriting from it.


Classes do not contain variables; they contain fields and properties (and methods, indexers, etc.)

I assume you're referring to properties, because fields shouldn't be public anyway.

There is no way to change the name of a property inherited from a base class.

What you can do:

Option 1

Add a property Points in addition to the property Level inherited from the base class:

class MyBaseClass
{
    public int Level { get; set; }
}

class MyDerivedClass : MyBaseClass
{
    public int Points
    {
        get { return this.Level; }
        set { this.Level = value; }
    }
}

Example:

MyDerivedClass x = new MyDerivedClass();
x.Points = 42;
Console.WriteLine(x.Level); // prints "42"

Option 2

Turn your base class into an interface, so you can implement it explicitly, thereby hiding the property Level:

interface IMyInterface
{
    int Level { get; set; }
}

class MyClass : IMyInterface
{
    int IMyInterface.Level
    {
        get { return this.Points; }
        set { this.Points = value; }
    }

    public int Points { get; set; }
}

Example:

MyClass y = new MyClass();
y.Points = 42; // works
y.Level = 99; // doesn't work

IMyInterface z = y;
z.Level = 99; // works
Console.WriteLine(y.Points); // prints "99"


Is there any way to rename the Level variable in the derived class?

No, stop and think about why for a second. If you have an instance of Base, you could refer to the property Level. Now if you have an instance of Derived, it is also a Base, and therefore it should have property named Level. It's that simple: a Derived is also a Base so whatever you can do with Base you should be able to do with Derived.

But, you could do this:

class Base {
    protected int Level { get; set; }
}

class Derived : Base {
    public int Points { get { return this.Level; } set { this.Level = value; } }
}

Note that Level is no longer publicly visible.


No, that's not possible. You can override the behavior using virtual properties, but not rename it.


As someone coming to this question in over a decade, I want to see if I can answer the original form I think it was in, which is; "Can you rename an inherited property in VB.Net?", and the answer I've found is yes.

Let's say you have class A:

Public Class A
    Public Property OriginalProperty
        Get
            ' Special Get implmenetation
        End Get
        Set (value As Object)
            ' Special Set implementation
        End Set
    End Property
End Class

And class B:

Public Class B
    Inherits A

    Public Property NewProperty
        Get
            Return OriginalProperty
        End Get
        Set(value As Object)
            MyBase.OriginalProperty = value
        End Set
    End Property
End Class

This retains the original get and set logic while allowing the calling code to use the new Property name. It may be worth asking why you have to refer to the same property by a different name in code, instead of just referring to the original property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜