I am trying to understand inheritance for my unity project but seem to have found a limitation to my setup. I got my self confused whilst writing it as i am still learning to understand C# properly.
I have a set of classes that inherit, and they split based on two different behaviors that way i have the correct reference.
I then need to cast them so i can have access to a method in one of these classes. So my structure looks like this:
public class Behaviour : Position {
public Handler reference;
public Behaviour(int tx, int ty, Handler refer) : base (tx,ty){
reference = refer;
}
// overload
public Behaviour(int tx, int ty) : base (tx,ty){}
}
public class Behaviour2 : Position {
public SettingsHandler reference;
public Behaviour2(int tx, int ty, SettingsHandler refer) : base (tx,ty) {
reference = refer;
}
}
public class SettingsHandler : Handler {
public Settings level {get;set;}
}
public class Handler : MonoBehaviour{
virtual public void Enter(List<Node> n,Vector3 p){}
virtual public void Exit(List<Node> n, Node curNode){}
}
Now this was working fine until i had to access Handler.Enter or Handle.Exit. Then i got lost on how to set the type properly.
So I was doing something like this:
//need to set temp :
??? temp;
if(path[i] is Behaviour2){
temp = (Behaviour2)path[i];
} else {
temp = (Behaviour)path[i];
}
temp.reference.Enter();
What should temp type be set to here?
I am thinking i might have misunderstood inheritance as i seem to get type issues. Does C# have a solution for this - i can't be the only one who has got stuck. But my brain is getting confused trying to follow it all.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…