[C# in Unity] What is polymorphism

Hello everyone, I am floating pointer.

Because I hate many textbooks that talk about Gods and gods and still don't know how to write code after reading it, I decided to use the most plain language to introduce the usage of C#.

This tutorial is only for beginners, not for advanced people.


What is polymorphism?

The same operation acts on different objects, can have different interpretations, and produce different execution results, which is polymorphism. In other words, in fact, instances of the same type call the "same" method, and the results are different.

by Polymorphism in C#|

 At first glance, newcomers will definitely feel very confused. What is an object? What is the explanation? What is a type? What is an instance?

In fact, we can completely ignore the above statement.

Let's look at the Test9.cs script, where Test02 is a method with parameters.

public class Test9 : MonoBehaviour
{
    public void Test02(int i)
    {
    }
}

We call Test02 in the Start life cycle.

 private void Start()
    {
        Test02(1);
        Test02(2);
    }

 The so-called polymorphic "state" refers to Test02. And "polymorphism" refers to passing in different values ​​to Test02 to produce different results.

So we can directly understand "polymorphism" as "code reusability". Polymorphism means running the same piece of code repeatedly, which will produce the same or different results due to the same or different values ​​passed in.

Guess you like

Origin blog.csdn.net/makyocute/article/details/131008793