Derived class , BaseClass, Dictionary
Hello! I have a question. I declared Dictionary<int, BaseClass>, initialized the variables and functions of the BaseClass in a class that inherits from it. When I called a method from the dictionary, I confirmed that the overridden method was invoked. Can you explain how this works and why it's possible? I tried searching but couldn't find much. I'll attach a picture.
6 Replies
No idea why you made this so incredibly convoluted with unity, weird class and method names and using an enum in one place and an int in another..
but what I think your question is about is how
Test002
can be considered an object of type Test001
?
is that correct?Thank you for your response.
I'm currently working on creating a card game as a learning project. As I contemplate how to manage the cards, I wanted to organize them within a data structure. To differentiate between the cards, I intended to assign unique ID values to each card. When I input a card's ID, I aim to retrieve its Name, Cost, and Effect from the data structure for use. I tested this, and the attached picture shows the result.
My question is, within the data structure, there is a BaseClass, and when I make a call, it seems to output the method of the child class that overrides the base class method. I'm curious if this behavior is expected, and I'd like to understand how it works internally.
This is polymorphism at its most basic form in C#
Your base class declares a
virtual
method
your child classes override
that method, providing their own codePobiega
REPL Result: Success
Console Output
Compile: 446.227ms | Execution: 27.035ms | React with ❌ to remove this embed.
this is a more simple example that does the exact same thing as your code does
Thank you. Let me check the official letter about polymorphism