继承有未定义类型的问题 [英] inheritance Problems with undefined types

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

问题描述

我想要能够创建一个可以调用其类型但没有任何约束的构造函数。

I want to be able to create a constructor that can call on its types but without any constraints.

public class Box
{
    public class Command
    {
        public string name;
        public string num;

        public Object PARAMS { get; set; }//<--- HERE
    }
}



PARAMS 是一个未定义的类型,可能会调用其他类别之一

I want PARAMS to be an undefined type that could possibly call one of these other classes

public class Person:Box
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
}

 public class Item:Box
 {
     public string ItemName { get; set; }
     public string Info { get; set; }
 }

如何定义 PARAMS ?我尝试继承,但我不知道如何调用其他类。

How can I define PARAMS? I am trying inheritance, but I am not so sure on how to call on other classes.

推荐答案

将其类型设置为 Box ,或将其通用:

You have two options - either set it's type as a Box, or make it generic:

public class Command<T> where T : Box
{
    public string name;
    public string num;

    public T PARAMS { get; set; }
}

或:

public class Command
{
    public string name;
    public string num;

    public Box PARAMS { get; set; }
}

这篇关于继承有未定义类型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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