A nested class that inherits its common parent class

Is it possible to somehow have this scenario where AN inherits the code from A using this code example?

The reason for this setup is that I need several classes that inherit from Base<TType>and Nested : Base<TType>, where the server has only the base, and the client has the extended Nested. Thus, it would be easy to use code where they would have common code between themselves and with each other.

The problem is that I would have to write identical code inside

A and A.N  
B and B.N  
C and C.N 

etc..

I solved this temporarily by replacing the nested abstract class with an interface and doing it A.N : A, INested, but now I have to rewrite the code Base<TType>.Nestedagain in all Nested classes. Currently, the nested class is small and manageable.

hope this is not a confusing question ...

public abstract class Base<TType> where TType : class
{
    public TType data;
    internal void CommonCodeForAll() { }
    public abstract void Update();
    public abstract class Nested : Base<TType>
    {
        public abstract void Input();
    }
}

public class A : Base<someClass>
{
    public float Somevariable;

    public void SpecificFunctionToA() { }

    public override void Update()
    {
        // code that gets executed on server & client side that is unique to A
    }

    public class N : A.Nested
    {
        public override void Input()
        {
            if (data.IsReady()) { Somevariable *= 2; }
            SpecificFunctionToA();
        }
    }
}
public class B : Base<anotherClass>
{
    public float Somevariable;
    public int index;
    public int[] Grid;

    public void SomethingElse() { }

    public override void Update()
    {
        // code that gets executed on server & client side that is unique to B
    }

    public class N : B.Nested
    {
        public override void Input()
        {
            if (Grid[index] == -1) { SomethingElse(); }
            data.Somevariable = Grid[index];
        }
    }
}

Edit: , , .
, , . , , .

+4
1

, .

public abstract class BaseGeneric<T>
{
    T data;
    // ctor
    protected BaseGeneric(T data)
    {
        this.data=data;
    }
    // methods
    public abstract void Update();
    // properties
    public T Data
    {
        get { return data; }
        set { data=value; }
    }

    // base nested class
    public abstract class BaseNested<B> where B : BaseGeneric<T>
    {
        protected B @base;
        // ctor
        protected BaseNested(B @base)
        {
            this.@base=@base;
        }
        // methods
        public abstract void Input(T data);
        public void Update() { @base.Update(); }
        // properties
        public T Data
        {
            get { return @base.data; }
            set { @base.data=value; }
        }
    }
}

// implementation base
public class Base : BaseGeneric<int>
{
    // ctor
    protected Base(int data) : base(data) { }
    //methods
    public override void Update()
    {
        this.Data+=1;
    }
    // implemented nested class
    public class Nested : Base.BaseNested<Base>
    {
        // ctor
        public Nested(int data) : base(new Base(data)) { }
        public Nested(Base @base) : base(@base) { }
        // methods
        public override void Input(int data)
        {
            this.Data=data;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // new implemented class with value 0
        var nested=new Base.Nested(0);
        // set value to 100
        nested.Input(100);
        // call update as implemented by `Base`.
        nested.Update();
    }
}
+1

Source: https://habr.com/ru/post/1525341/


All Articles