cool hit counter C# - Upward Transition_Intefrankly

C# - Upward Transition


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/21819771

In the process of learning C#, some of the basics like data types, control structures, arrays, etc. are much the same as the previous exposure to the language and can be understood, but this is the first time I have encountered the concept of upward transformation.

After studying a bit it is roughly understood as follows.

Upward transformation is the transformation of a subclass into a parent class, and the object can be used as its own type.

Let's start with a piece of code.

public class Animal
    {
        public virtual void Eat()
        {
            Console.WriteLine("I'm an animal., I want to eat something.!");
        }
    }
    public class Bird : Animal
    {
        public override void Eat()
        {
            Console.WriteLine(" I'm a bird., I want to eat something.!");
        }
    }
    class Test
    {
        static void Main(string[] args)
        {
             Animal animal = new Bird();   /upward transition
              animal.Eat();
        }
    }

In the above example we have defined an animal class (which has an eating method), the bird class inherits from the animal class (overriding the eating method in the base class), and the main function has a life animal as an object of type Animal. ".

For this example. When declaring an animal, the type of the animal is Animal. But the instance pointed to by animal is a subclass of Animal, i.e. Bird.

Of course there is an upward transition and then there is a downward transition, in the opposite way of course, i.e. converting the parent class to a child class.

I saw a question on the internet, someone said "if the required function in the child class exists in the parent class, use upward transition; if the required function in the parent class exists in the child class, use downward transition", but as mentioned above, Bird is a subclass of Animal, Bird has inherited the methods in Animal, why should we use downward transition? I.e. why define Animal with "Animal animal=new Bird(); " instead of "Bird animal=new Bird(); "What about?

I would like to ask my friends to give their valuable advice and suggestions.


Recommended>>
1、Choosing a programming language for someone else
2、Focus on the Fifth World Internet Conference these black technology will affect your future life
3、The operators are in tears Apple server has a shocking bug locked machines become unlocked in seconds
4、May 22nd Out of Service Update Notice
5、Machinima Season 2 AI Predicts Human Gait at 80 for the First Time

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号