For example ,I have an abstract class called Enemy and another two Enemy classes (EnemyA and EnemyB) that inherits from Base Enemy class. Each A and B class has its different algorithm method to find a Direction to move and Moving method is in the Base Enemy Class. I want to use Children Classes’s functions in the Base Abstract Class. What I am doing now is the normal way; using an Abstract Class Method in the Child Classes as usual.But I want to know if it is possible or not :3 (Or if I am doing wrong)
First of all, it’s not possible to use a child funtion in a parent. I mean, how would the compiler know what to do? If an Enemy does not have a FindDir() method, then how is the compiler supposed to know what to do in that situation?
That said, you can just implement a FindDir() method in the abstract Enemy class without adding any body, forcing each child to implement their own version. To do that, just make the method abstract as well. That way Enemy has a FindDir() method, but Enemy A and Enemy B each implement their own version of that. If you had 3 Enemy types, and 2 of them shared the same FindDir() method (thus it beind the “default”) you could also just implement that into Enemy directly, and make the one that needs a different version of FindDir() override it.
Hope this clears all your questions. If not or i missed something, feel free to ask again.
Yeah, that’s just basic inheritance, assuming you define FindDir in Enemy:
public abstract class Enemy : MonoBehaviour {
public abstract Vector3 FindDir();
}
public class EnemyA : Enemy {
public override Vector3 FindDir() {
return whatever;
}
}
.....
Enemy en1 = GetComponent<EnemyA>();
Debug.Log("Moving in direction " + en1.FindDir() );
Oh yeahhh!!! I got it… It’s my first time using Abstract or whatsoever in C# real projects and it makes me confused. Now, I understand some how though I cannot still explain well it verbally. Thanks you two.
I have similar problem. I am trying to use polymorfism in my code’s structure, so I made (similarly) an abstract Base class extended by two classes (lets call them ChildA and ChildB). So, I forced this childs to follow IWork interface (names for example perhaps not too elegant, but please bear with me)…
The first two childs work fine with two of the methods of the interface:
getStep (it’s supposed to return an array of objects)
getBranchSteps (uses getStep recursively to find a target)
The code works fine. The problem is that the code in getBranchSteps is the same in both child classes. So i tried shifting up getBranchSteps to the superclass (the abstract Base class)… it compiles, but it doesn’t behave as expected anymore… perhaps it is because getStep is just abstract in parent class? I don’t know what to do about it…
…do I have any hope to fix this refactoring attempt?
EDIT: Nevermind :-). For some reason, removing the prefix this. from getStep calls fixed the issue. (Well, I like to use this a lot, specially with wild named properties and methods, because I liked Python’s better explicit than implicit philosofy, so I use it in C# as well… have a nice day блин