C # succession overloaded interface introduced with (B)

This is in addition to the articles, the difference before and a little planning, multi-level inherited by the need to lean back, or look at the code

namespace ConsoleApp1
{

    public interface It
    {
        void PI();
    }

    public class A:It
    {
        private int _type = 0;
        public int Type = 0;

        public A(int type)
        {
            Type = _type = type;
        }

        public virtual void P()
        {
            Console.WriteLine("这是A的PP!");
        }

        public virtual void PI()
        {
            Console.WriteLine("这是A的PI!");
        }

        public void Dispatch()
        {
            switch (_type)
            {
                case 1:
                    (this as B).P();
                    break;
                case 2:
                    (this as C).P();
                    break;
            }
        }
    }

    public class B: A 
    { 
        public B ( int type): Base (type) {} 

        public  the override  void P () 
        { 
            Console.WriteLine ( " ! This is the PP B " ); 
        } 

        public  the override  void the PI () 
        { 
            Console.WriteLine ( " this is the PI B! " ); 
        } 
    } 

    public  class C: a 
    { 
        public C ( int type): Base (type) {} 

        public override void P()
        {
            Console.WriteLine("这是C的PP!");
        }

        public override void PI()
        {
            Console.WriteLine("这是C的PI!");
        }
    }
}

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            _dictClass.Add(0, new A(0));
            _dictClass.Add(1, new B(1));
            _dictClass.Add(2, new C(2));

            _dictInterface.Add(0, new A(0));
            _dictInterface.Add(1, new B(1));
            _dictInterface.Add(2, new C(2));

            _dictClass[0].P();
            Console.WriteLine(_dictClass[0].Type);
            _dictClass[1].P();
            Console.WriteLine(_dictClass[1].Type);
            _dictClass[2].P();
            Console.WriteLine(_dictClass[2].Type);

            Console.WriteLine();
            Console.WriteLine("ding------------------------------ding");
            Console.WriteLine();

            _dictClass[1].Dispatch();
            _dictClass[2].Dispatch();

            Console.WriteLine();
            Console.WriteLine("ding------------------------------ding");
            Console.WriteLine();

            _dictInterface[0].PI();
            _dictInterface[1].PI();
            _dictInterface[2].PI();
        }

        private static Dictionary<int, It> _dictInterface = new Dictionary<int, It>();
        private static Dictionary<int, A>  _dictClass = new Dictionary<int, A>();
    }

}

Look at the results:

First to explain, this code has been tested once slight improvement, the introduction of two variables, while adding a Dispatch

A b = new B(1);

The introduction of this variable is more intuitive for this line of code is described, first configuration example B, B thus inherited A before B to A is configured to a configuration,

And at the same time, the parent class constructor, the assignment variable Type, _type are 1 and Type is the public can be inherited by subclasses

Therefore b.Type = 1, this should be compared to all understand it, and c and it is the same, not to speak.

As you may have discovered, _type Dispatch associated with the introduction to the piece, and

Yes, I use it for dispatching instructions, that is, when doing dynamic management will be frequently used, and this way, the subclass inherits the parent class method overloading can not be able to achieve some additional function from the parent class , while the parent becomes a transponder.

In fact, such an approach is not good, the parent should not be involved in specific acts of subclasses, so I intend to change it.

namespace ConsoleApp1
{

    public interface It
    {
        void PI();
    }

    public class A:It
    {
        private int _type = 0;
        public int Type = 0;
        public A a;

        public A(int type)
        {
            Type = _type = type;
        }

        public virtual void P()
        {
            Console.WriteLine("这是A的PP!" ); 
        } 

        Public  Virtual  void the PI () 
        { 
            Console.WriteLine ( " This is the PI A! " ); 
        } 

        Public  void the Dispatch () 
        { 
            Console.WriteLine ( " here is an instruction dispatch interface Oh! " ); 
            The aP ( ); 
        } 
    } 

    public  class B: A 
    { 
        public B ( int type): Base (type) { 
            A = the this ; 
        } 

        public the override  void P () 
        { 
            Console.WriteLine ( " This is the PP B! " ); 
        } 

        public  the override  void the PI () 
        { 
            Console.WriteLine ( " This is the PI B! " ); 
        } 
    } 

    public  class C: A 
    { 
        public C ( int type): Base (type) { 
            A = the this ; 
        } 

        public  the override  void P () 
        {
            Console.WriteLine ( " This is a C PP! " ); 
        } 

        Public  the override  void the PI () 
        { 
            Console.WriteLine ( " This is a C the PI! " ); 
        } 
    } 
}

You should look out, here _type removed and switch, switch to the instance itself to instruction dispatch

Look at the results

And the result is the same as before, so that, to avoid interference parent subclasses, but merely as a repeater.

See here, you should be wondering, so write what is it? Directly off overloaded method subclass is not on line yet, why bother it?

Yes, this is indeed superfluous, but if subclass is not overloaded, then the child what will happen?

 

namespace ConsoleApp1
{

    public interface It
    {
        void PI();
    }

    public class A:It
    {
        private int _type = 0;
        public int Type = 0;
        public A a;

        public A(int type)
        {
            Type = _type = type;
        }

        public void P()
        {
            Console.WriteLine("这是A的PP!"); 
        } 

        Public  Virtual  void the PI () 
        { 
            Console.WriteLine ( " This is the PI A! " ); 
        } 

        Public  void the Dispatch () 
        { 
            Console.WriteLine ( " here is an instruction dispatch interface Oh! " ); 
            The aP () ; 
        } 
    } 

    public  class B: A 
    { 
        public B ( int type): Base (type) { 
            A = the this ; 
        } 

        public  new new void P () 
        { 
            Console.WriteLine ( " This is the PP B! " ); 
        } 

        public  the override  void the PI () 
        { 
            Console.WriteLine ( " This is the PI B! " ); 
        } 

        public  void PP () 
        { 

        } 
    } 

    public  class C: A 
    { 
        public C ( int type): Base (type) { 
            A = the this ; 
        } 

        public  new new void P () 
        { 
            Console.WriteLine ( " This is a C PP! " ); 
        } 

        public  the override  void the PI () 
        { 
            Console.WriteLine ( " This is a C the PI! " ); 
        } 

        public  void PP () 
        { 
            Console. the WriteLine ( " What is a C PP! " ); 
        } 
    } 
}

 

Look at the results

Either way, it seems no use, because, when I was receiving all instances into an example A declaration, so that, if not overloaded, even though these instances have the same parent class method , only in the parent class for the performance of a template, in order to achieve characteristics subclass, the instance must be converted back to its type of construction, such as (b as B) .PP ();

Let's look Photo

This picture is very good explanation just said.

Thus, if you want to manage the parent class subclasses, then the subclass method must be managed reload the job, in my understanding, the overload is like associating a mapping, if the mapping exists , then the effect of this method is presented in effect subclass, if not, it displays only the parent class result, the parent class is like a small box, the subclass is like a big box wrapped parent this little box, Ruoguo the mapping between the two, then the parent can be manipulated subclasses along this line performance in the big box, or only in small objects inside the self-direction.

In this case, it seems that this is responsible for managing the transponder is also not very good with ah, maybe you can write a few virtual functions to expand the subclass mapping behavior, but that does not seem consistent with object-oriented programming ideas, and behavior the abstract and the interface is very similar, it will cause a lot of redundancy.

So the question is, what to write this extended sense is it?

Two aspects,

One would like to see exactly how the relationship between object-oriented programming, Father and Son.

Two and framework for the preparation, according to the author of the idea, thinking management framework is from top to bottom, is the most superior managers and coordinators, able to deal with a unified sort of thing, while the lower is the specific performer, looks like inheritance of father and son, in fact, is indeed the case.

The author is probably relatively turn dead end, always hope that all of the objects can be coordinated by a manager, although the final by way of a combination of modules is also achieved, but from the results I want still a little gap, so just be this talent Some studies, though not necessarily the result you want, but the important thing is the way of thinking, as well as during the harvest.

So here the idea of ​​the end result of this is not it, began to study under a multi-level inheritance.

 

Guess you like

Origin www.cnblogs.com/fairy-blonde/p/11562827.html