At run-time
, inside base class
, how to find the current child class name ?
To access an element of a base class from an inherited class, you need to use the base keyword. The base keyword is used in inherited classes in the following cases: when from the constructor of a derived class you need to call the constructor of the base class.
The Class object has a getName() method that returns the name of the class. So your displayClass() method can call getClass(), and then getName() on the Class object, to get the name of the class of the object it finds itself in.
When a class has a parent class in Python, we can get the child class name from the parent class. Product, not Product instance, has the __subclasses__ method that returns a list of child classes. So t has Book class information and t. __name__ is Book, Product's child class name.
Get the type of the current object, then its name.
this.GetType().Name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With