Convert derived class to base class

后端 未结 6 1302
渐次进展
渐次进展 2020-12-01 13:31

I\'m trying to refresh my memory but can\'t find answers with Google.

public class BaseClass
{
    public virtual void DoSomething()
    {
        Trace.Writ         


        
相关标签:
6条回答
  • 2020-12-01 14:13

    Try using the new keywor instead of override As far as i know this should enable that desired behavior. I'm not realy sure about that so please don't blame me if i'm wrong!

    public class BaseClass
    {
        public virtual void DoSomething()
        {
            Trace.Write("base class");
        }
    }
    
    public class DerivedClass : BaseClass
    {
        public new void DoSomething()
        {
            Trace.Write("derived class");
        }
    }
    
    0 讨论(0)
  • 2020-12-01 14:18

    The solutions with new instead of override break the polymorphism. Recently I came to the same problem and implemented it the following way. My solution has the following advantages:

    • virtual and override stays in place;
    • name BaseClass is not used directly in the type cast, so if I introduce an intermediate MiddleClass in the hierarchy between BaseClass and DerivedClass, which also implements DoSomething(); then the MiddleClass's implementation won't be skipped.

    This is the implementation:

    public class BaseClass
    {
        public virtual void DoSomething()
        {
            Trace.Write("base class");
        }
    }
    
    public class DerivedClass : BaseClass
    {
        public override void DoSomething()
        {
            Trace.Write("derived class");
        }
    
        public void BaseDoSomething()
        {
            base.DoSomething();
        }
    }
    

    The usage is:

        DerivedClass dc = new DerivedClass();
    
        dc.DoSomething();
    
        dc.BaseDoSomething();
    
    0 讨论(0)
  • 2020-12-01 14:24

    Although this sounds irrational but it works

     DerivedClass B = new DerivedClass();
    
    BaseClass bc = JsonConvert.DeserializeObject<BaseClass>(JsonConvert.SerializeObject(B));
    
    0 讨论(0)
  • 2020-12-01 14:27

    You can't - that's entirely deliberate, as that's what polymorphism is all about. Suppose you have a derived class which enforces certain preconditions on the arguments you pass to an overridden method, in order to maintain integrity... you don't want to be able to bypass that validation and corrupt its internal integrity.

    Within the class itself you can non-virtually call base.AnyMethod() (whether that's the method you're overriding or not) but that's okay because that's the class itself deciding to potentially allow its integrity to be violated - presumably it knows what it's doing.

    0 讨论(0)
  • 2020-12-01 14:28

    For VB.net, I've used the following code to do the conversion (shown with Lists of Objects):

            Dim tempPartialList As New List(Of clsBaseData)
    
            For Each iterClsDerivedData As clsDerivedData In ListOfDerivedDataObjects
                tempPartialList.Add(CType(iterClsDerivedData, clsBaseData))
            Next
    

    Where clsBaseData is the Base Class from which clsDerivedData is made by Inheriting clsBaseData.

    ListOfDerivedDataObjects is a List(Of clsDerivedData).

    I have found this useful where I have Lists of several Derived Classes and I would like to operate on a property of the Base Class for all the objects in the Lists of Derived Classes. The tempPartialList is, for me, a temporary List meant to facilitate changing this property.

    0 讨论(0)
  • 2020-12-01 14:35

    You absolutely CAN (call the base method), just read up on Polymorphism:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism

    Example:

    public class BaseClass
    {
        public void DoWork() { }
        public int WorkField;
        public int WorkProperty
        {
            get { return 0; }
        }
    }
    
    public class DerivedClass : BaseClass
    {
        public new void DoWork() { }
        public new int WorkField;
        public new int WorkProperty
        {
            get { return 0; }
        }
    }
    

    And how to call it:

    DerivedClass B = new DerivedClass();
    B.DoWork();  // This calls the new method.
    
    BaseClass A = (BaseClass)B;
    A.DoWork();  // This calls the old method.
    
    0 讨论(0)
提交回复
热议问题