- Home /
Having static classes inherit from a static class
Hidiho!
This problem is not too important right now - I could easily do it with some kind of workaround - it is more about designing my code and whether to do some fancy stuff. Wrapping it up, I'm doing some kind of hero-management game, which is mainly some GUI in different scenes. You have different heroes and let them do stuff. Every hero has a character sheet with his stats. I thought about making the heroes' stats scripts static, so that I wouldn't have to bother pushing information from scene to scene - when referencing a hero, I am always talking about the same hero anyways. Every hero has the same stats, so it seems to be a good opportunity (hey, there's UNITY in it!) to do some inheriting. Problem is: my static heroes cannot inherit from a class that's not object.
Some example code of what I would like to do:
public static class Character : object
{
public static int level;
public static int health;
//...
//some other functions they have in common, like how experience translates into levels etc.
}
public static class Aragorn : Character
{
level = 3;
health = 20;
//...
}
public static class Legolas : Character
{
level = 3;
health = 15;
//...
}
What is a good way of doing this? I am realizing right now, that having the fields in Character won't help, because I will have to specify them again anyways, but it would be nice to have the methods where I need them. Are static classes useful in this scenario, or is there some way to do it with interfaces or other things?
I'm looking forward to any input on this whole thing
Paul
Answer by rageingnonsense · Oct 30, 2015 at 11:03 PM
How about something like this:
public static class CharacterManager {
public static List<Character> heroes;
}
public abstract class Character {
int level;
float health;
}
public class Aragorn : Character {
}
fill in with constructors etc. The idea is to keep a static class that keeps track of each character, but the characters themselves are not static.
When you create a character; you add them to the static list. You an add methods to CharacterManager to make accessing the list nicer
This is super hobbled together, but I hope it demonstrates the basic idea. There are a lot of ways to skin this cat, and this is but one of them.
Three years of studying computer science and I completely forgot about abstract classes. Tried to do it with an interface, which didn't work out either. But this is probably a nice way to have the same functionality! It's just a little sad, having to do it via some list ins$$anonymous$$d of just having the characters themselves being static - I mean, not having to think about instances and stuff when I change them is basically what I want in that situation.
On the other hand, this makes it easier to iterate through all the characters for some checks, or gives me easier ways of having multiple save files (a thing I haven't thought about yet).
Thanks so far! But I'm still interested in some other ways of cat skinning ;) Just in order to see some more examples or possibilities
You can't derive a static class at all. It also wouldn't make any sense since a static class only contains static elements. Those would be shared by all derived classes. An example:
public class BaseClass
{
public static int test;
public static string s;
static BaseClass()
{
test = 1;
s = "base";
}
}
public class ChildClass1 : BaseClass
{
public static int someOther = 44;
static ChildClass1()
{
test = 4;
s = "child";
}
}
void Start ()
{
/* #1 */Debug.Log("Base.test = " + BaseClass.test + " Base.s = " + BaseClass.s);
/* #2 */Debug.Log("Child1.test = " + ChildClass1.test + " ChildClass1.s = " + ChildClass1.s);
/* #3 */Debug.Log("Child1.other = " + ChildClass1.someOther);
/* #4 */Debug.Log("Child1.test = " + ChildClass1.test + " ChildClass1.s = " + ChildClass1.s);
/* #5 */Debug.Log("Base.test = " + BaseClass.test + " Base.s = " + BaseClass.s);
}
At #1 the static constructor of our Base class is called since we first access the class. So we get the number "1" and the string "base".
At #2 you might think that Child's static constructor will run, but that's actually wrong. You don't access the child class at all. Visual studio also shows that the access can be "simplified" and suggests to use BaseClass.test
ins$$anonymous$$d of ChildClass1.test
since they represent the same field. Internally you actually access "BaseClass.test" and that's why the static constructor won't run.
At #3 we actually access something from the Child1 class so the static constructor will run. It will overwrite "test" and "s".
So at #4 we will see the number "4" and the string "child"
At #5 we will also see "4" and "child" since static elements only exists once.
That's why it makes no sense at all to inherit a static class. Static classes are sealed automatically by the compiler. The only way to actualy create seperate classes is by using a generic base class and pass the derived class type as type parameter. That will create an actual seperate class which "inherits" the static members of the base class. Actually there is no base class. A generic class is not a complete class without a type for the type parameter.
However never ever think about using generics for this. This will produce tons of other problems due to the fact how static initialization works. It's still the case that when you access an "inherited" static member (that is defined in the generic base class) the static constructor of the child won't be executed until you access an actual member of the child class. So it's not possible to reliably initialize inherited members inside the child class. Initialization is done in the base class
Here are some links that might help you to understand it a bit better:
Thank you very much! Got it now... and looking back, it seems pretty obvious, too.
So once again: thanks guys. I think I'll go with nonsense's idea of storing them in some kind of character manager. :)
Your answer

Follow this Question
Related Questions
inheritance and static properties 1 Answer
Question about static variables and inheritance 2 Answers
Access variables,method from a Mono Behaviour without static use. -1 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
Make all instances inherit the same value from parent and make static 1 Answer