通用实施建议 [英] Generic implementation suggestion

查看:63
本文介绍了通用实施建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的实现,

Following is my implementation,

interface iStudent
    {
        Int32 StudentID { get; set; }
    }

    class GraduateStudent : iStudent
    {
        Int32 _PassingoutYear;
        public Int32 PassingoutYear { get { return _PassingoutYear; } set { _PassingoutYear = value; } }
    }

    class CurrentStudent : iStudent
    {
        Int32 _EnrolmentYear;
        public Int32 EnrolmentYear { get { return _EnrolmentYear; } set { _EnrolmentYear = value; } }
    }



用法:想要创建一个既可以存储CurrenStudent也可以存储GraduateStudent的通用集合,并且应该在设计时支持从通用集合元素隐式转换为对象.

例如,



Usage: Want to create a generic collection which can store both CurrenStudent and GraduateStudent and should support implicit cast to the objects from the generic collection elements at design time.

So for example,

AllStudents.Add(new CurrentStudent());
AllStudents.Add(new GraduateStudent());
//Should throw compiler warning, since the underlying type is Current Student.
GraduateStudent grad1 = (GraduateStudent)AllStudents[0];
//Should not throw compiler warning, since the underlying type is GraduateStudent Student.
GraduateStudent grad1 = (GraduateStudent)AllStudents[1];



这可行吗?请注意;



Is this doable? Please note; the generic collection should be type safe and should not allow wrong conversions at design time.

推荐答案

不,没有办法做你想做的事并防止无效的强制转换编译时间.泛型集合必须用于某些基类或IStudent接口,才能同时包含CurrentStudentGraduateStudent对象,因此编译器无法知道集合中对象的哪种派生类型.
No, there is no way to do what you want and prevent invalid casts at compile time. The generic collection will have to be for some base class or the IStudent interface in order to be able to contain both CurrentStudent and GraduateStudent objects, so the compiler has no way of knowing which derived type an object in the collection might be at compile time.


我通常的解决方法是为每个类型创建一个带有成员的枚举,并在构造函数中设置一个具有该类型的抽象类.通常,可以删除接口并将属性等放置在抽象类中,但是如果没有,则抽象类可以实现该接口.

类似于:
The way I normally aproach this is to create an enum with a member for each type and an abstract class that has this type set in the constructor. Often the interface can be removed and the properties etc placed in the abstract class, but if not the abstract class can implement the interface.

Something like:
enum StudentType
{
    Graduate,
    Current
}
interface IStudent
{
    int StudentID { get; set; }
}
abstract class StudentBase : IStudent
{
    private StudentType studentType;
    private int studentID;

    internal StudentBase(StudentType studentType)
    {
        this.studentType = studentType;
    }

    public virtual int StudentID
    {
        get { return studentID; }
        set { studentID = value; }
    }
    StudentType StudentType
    {
        get { return studentType; }
    }
}
class GraduateStudent : StudentBase
{
    int passingoutYear;

    GraduateStudent()
        : base(StudentType.Graduate)
    { }

    public int PassingoutYear
    {
        get { return passingoutYear; }
        set { passingoutYear = value; }
    }
}
class CurrentStudent : StudentBase
{
    int enrolmentYear;

    CurrentStudent()
        : base(StudentType.Current)
    { }

    public int EnrolmentYear
    {
        get { return enrolmentYear; }
        set { enrolmentYear = value; }
    }
}



[添加]
AllStudents必须是StudentBase的集合,例如List<StudentBase>



[Added]
AllStudents would need to be a collection of StudentBase for example List<StudentBase>


这篇关于通用实施建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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