简体   繁体   中英

Overwriting a property's accessors

I'll just jump right in.

Suppose AudioFile is an abstract class like so:

abstract class AudioFile 
{
    public string Title { get; set; }
}

Now, in most cases, using a.Title is perfectly suited for other classes that inherit from AudioFile. However, in the case of MPEG files, this is stored in a different object in the id3 variable:

class MPEG : AudioFile 
{
    private ID3 id3;

    public new string Title {
        get {
            return id3.Title; 
        }
        set {
            id3.Title = value;
        }
    }
}

class WMA : AudioFile
{

}

What I would like is to do the following:

AudioFile a;

if( isMPEG ) {
    a = LoadMPEG(); // Returns a new MPEG instance.
} else
if( isWMA ) {
    a = LoadWMA();  // Return a new WMA instance.
}

Console.WriteLine( a.Title );
// Other stuff with a.

I would expect the output to be, whether an MPEG or a WMA, the song's title. However, when its MPEG, it doesn't work as expected (because it isn't using the id3 object). The only way it works is:

if( isMPEG ) {
    MPEG a = LoadMPEG();    // Returns a new MPEG instance.
    Console.WriteLine( a.Title );
    // Other stuff with a.
} else
if( isWMA ) {
    WMA a = LoadWMA();  // Return a new WMA instance.
    Console.WriteLine( a.Title );
    // Other stuff with a.
}

Which is not what I want. And ideas on how to do what I want to do?

Make the abstract class property virtual so that derived classes can override it if they need different behaviors.

abstract class AudioFile
{
    public virtual string Title { get; set; } 
}

class MpegFile : AudioFile
{
    public override string Title { /* your custom getter and setter */ }
}

AudioFile file = new MpegFile();
string title = file.Title; // will use override

In your version, you leave the abstract class property non-virtual and mark the derived class property as new . This allows you to use your custom behavior only via the reference of the derived class. You lose the ability to experience the polymorphic behavior via the base. The new modifier hides the base behavior only through the derived reference. Base references use the base behavior.

AudioFile file = new MPEG(); // will use base behavior for non-virtual methods
MPEG file = new MPEG(); // will use derived behavior when non-virtual methods are hidden by new

You may happen to come from a Java background. In C#, members are not virtual by default. You must mark them as virtual , and use override to replace or augment the base implementations in a polymorphic way. (For abstract members in abstract classes, you would use the keyword abstract instead of virtual .) The new keyword is useful when the base method is not virtual, but you have already seen its limitation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM