从其通用父类继承的嵌套类 [英] Nested class that inherits from its generic parent class

查看:106
本文介绍了从其通用父类继承的嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的,有这种情况下,AN从这个代码示例继承A的代码?



设置它的原因是,我需要多个继承自 Base< TType> 嵌套:Base< TType> 的类,其中服务器具有仅基地,并且客户端具有扩展的嵌套。这样,使用代码将会很容易,在代码中它们之间会有一些共享的代码。每个其他人。



问题是我必须在

  A和AN 
B和BN
C和CN

等。

我暂时解决了这个问题,用一个接口替换嵌套的抽象类,并执行

AN:A, INested ,但现在我必须在所有嵌套类中再次重写 Base< TType> .Nested 代码。目前,嵌套类是小&可以管理。



希望这不是一个令人困惑的问题...

 公共抽象类Base< TType> TType:class 
{
public TType data;
internal void CommonCodeForAll(){}
public abstract void Update();
public abstract class嵌套:Base< TType>
{
public abstract void Input();
}
}

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

public void SpecificFunctionToA(){}
$ b $ public override void Update()
{
//在服务器上执行的代码& A

$ b $ 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(){}
$ b $ public override void Update()
{
//在服务器上执行的代码& B

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

编辑:
我更新了代码示例以显示我想要实现的功能。

为什么我正在尝试这样做,是为了保持物理,网络和应用程序的安全。用户输入分离。
有多个不同的控制器,每个控制器都有自己的包装和放大器。解包功能,控制器身份&访问物理引擎。

解决方案

我有一个使用类的ecapsulation而不是继承的解决方案。 b

  public abstract class BaseGeneric< T> 
{
T data;
// ctor
保护BaseGeneric(T数据)
{
this.data = data;
}
//方法
public abstract void Update();
//属性
public T Data
{
get {return data; }
set {data = value; }
}

//基本嵌套类
public abstract class BaseNested< B>其中B:BaseGeneric< T>
{
保护B @base;
// ctor
protected BaseNested(B @base)
{
this。@ base = @ base;
}
//方法
public abstract void Input(T data);
public void Update(){@ base.Update(); }
//属性
public T Data
{
get {return @ base.data; }
set {@ base.data = value; }
}
}
}

//实现基
公共类Base:BaseGeneric< int>
{
// ctor
protected Base(int data):base(data){}
//方法
public override void Update()
{
this.Data + = 1;
}
//实现的嵌套类
public class嵌套:Base.BaseNested< Base>
{
// ctor
public Nested(int data):base(new Base(data)){}
public Nested(Base @base):base(@base) {}
//方法
public override void Input(int data)
{
this.Data = data;




类程序
{
static void Main(string [] args)
{
//新值为0的实现类
var nested = new Base.Nested(0);
//设定值为100
nested.Input(100);
//由'Base'执行的调用更新。
nested.Update();
}
}


is this possible to somehow, have this scenario, where A.N inherits code from A with this code example?

The reason for setting it up like this, is that I need multiple classes that inherit from Base<TType> and the Nested : Base<TType> where the server has the base only, and the client has the extended Nested. This way, it would be easy to use the code, where they would have some shared code between themselves & each other.

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

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

etc.

I have solved this temporarily, by replacing the Nested abstract class, with an Interface and doing
A.N : A, INested, but now I have to rewrite the Base<TType>.Nested code again inside all the Nested classes. For now, the nested class is small & managable.

hope this isn't 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: I updated the code example to show what I'm trying to achieve.
Why I am trying to do this, is to keep the physics, networking & User input seperate. There are multiple different controllers where each one has their own pack & unpacking functions, controller identity & access to the physics engine.

解决方案

I have a solution using ecapsulation of classes instead of inheritance.

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();
    }
}

这篇关于从其通用父类继承的嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆