public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
// this is my singleton class
}
public abstract class A : Singleton<A>
{
// this is my base abstract class.
}
public B : A
{
// I want this class to be derived from A, also to derive from Singleton and MonoBehaviour
}``
Since c# does not support multiple inheritance. I am trying to make a class which is derived from class A and singleton class. When I call an instance of B. It creates an instance of A and that gives a null exception. Is there a solution for this or do I have to use interface ? Thanks in advance.
The first question is : What do you want to share between this classes ?
Waiting this answer, I will try to give you a generic explanation with as much detail as possible.
1 - Sharing only methods
If you want to share only methods, the best practice is to use Interfaces to split your logic as you want and give it to concrete classes :
public interface IInterface1{
//some methods
}
public interface IInterface2{
//some methods
}
public class MyClass : IInterface1, IInterface2{
}
MyClass can be abstract if you want and you can put some methods as virtual to override them if you want too
2 - Sharing methods and properties
You will do it from the same way, but the Interfaces need to be added to the correct class
public interface IInterface1{
//some methods
}
public interface IInterface2{
//some methods
}
public abstract class MyAbstractClass {
//some methods and properties
}
public class MyClass : MyAbstractClass, IInterface1, IInterface2{
}
You can add the interfaces to MyAbstractClass instead of MyClass , it depends your architecture and your needs
3 - Singleton
For the singleton, as say in the comments, this is only a way to gain global accessibility, so you need to do it in your "last" class, here MyClass
I Hope I expressed myselft enought correctly for this explanation and I hope this answer will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.