使用类DerivedClass编译错误:BaseClass [英] Compile Error with class DerivedClass : BaseClass

查看:57
本文介绍了使用类DerivedClass编译错误:BaseClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class  BaseClass< T> 
{
public void 重置(){data.Clear(); }
public void 添加(T value ){data.Add( value ); }
public int 计数{ get { return (data.Count); }
public T this [ int index]
{
get { return (data [index] ); }
set {data [index] = value ; }
}

列表< T> data = new List< T>();
}

class DerivedClass< T> :BaseClass< T>
{
public DerivedClass()
{
this .DoSomething();
}

私人 void DoSomething()
{
Add( 200 );
}
}



当我这样做时

DerivedClass< int> inst = new DerivedClass< int>();



inst.Add(200)将在方法 DoSomething()<中编译



添加(200) / code>将给出编译错误无法将'int'转换为'T'

解决方案

尝试此代码。 ..使用System;
使用System.Collections.Generic;
使用System.Data;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

名称空间console_poc
{
class程序
{
static void Main(string [] args)
{
DerivedClass inst = new DerivedClass();

inst。添加(200);



}
}

类BaseClass< T>
{
public void Reset(){data.Clear();}
public void Add(T value){data.Add(value);}
public int Count { get {return(data.Count);
public T this [int index]
{
get {return(data [index]); }
set {data [index] = value; }
}

列表< T> data = new List< T>();
}

类DerivedClass:BaseClass< int>
{
public DerivedClass()
{
this.DoSomething();
}

private void DoSomething()
{
Add(200);
}
}

}


你需要施放它:



  private   void  DoSomething() 
{
Add((T) 200 );
}





但要注意,如果200不能转换为T,它将抛出异常在运行时。


你不能在派生类中做Add(200),因为你怎么知道T是一个值类型?



编译器试图告诉你200不能转换为T因为它不满足约束条件。



为了做你想要什么,你需要做解决方案2所具有的,或者在这个中进行冗长的讨论CodeProject文章 [ ^ ]。



许多人说,对数字类型有一个通用约束被排除在框架之外。它会很高兴,但它不存在。


class BaseClass<T>
{
    public void Reset() { data.Clear(); }
    public void Add(T value) { data.Add(value); }
    public int Count { get { return(data.Count); } }
    public T this[int index]
    {
        get { return (data[index]); }
        set { data[index] = value; }
    }

    List<T> data = new List<T>();
}

class DerivedClass<T> : BaseClass<T>
{
    public DerivedClass()
    {
        this.DoSomething();
    }

    private void DoSomething()
    {
        Add(200);
    }
}


When I do
DerivedClass<int> inst = new DerivedClass<int>();

inst.Add(200) will compile

but Add(200) in method DoSomething() will give a compile error "cannot convert 'int' to 'T'.

解决方案

try this code...


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace console_poc
{
    class Program
    {
        static void Main(string[] args)
        {
            DerivedClass inst = new DerivedClass();

            inst.Add(200);
 


        }
    }

    class BaseClass<T>
    {
        public void Reset() { data.Clear(); }
        public void Add(T value) { data.Add(value); }
        public int Count { get { return (data.Count); } }
        public T this[int index]
        {
            get { return (data[index]); }
            set { data[index] = value; }
        }

        List<T> data = new List<T>();
    }

    class DerivedClass : BaseClass <int> 
    {
        public DerivedClass()
        {
            this.DoSomething();
        }

        private void DoSomething()
        {
            Add(200);
        }
    }

}


You need to cast it:

private void DoSomething()
{
    Add((T)200);
}



But be careful, if 200 isn't convertable to T, it will throw an exception at runtime.


You can't do Add(200) in the derived class because how do you know that T is a value type?

The compiler is trying to tell you that 200 can't be converted to T because it doesn't satisfy the constraints.

In order to do what you want, you need to either do what Solution 2 has, or there is a lengthy discussion in this CodeProject article[^].

Many have said, having a generic constraint for numeric types was left out of the framework. It would be nice to have but its just not there.


这篇关于使用类DerivedClass编译错误:BaseClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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