如何混合泛型和实数类型

如何解决如何混合泛型和实数类型

| 为什么不允许有这样的泛型类:
class SomeClass<T,int> { ..... }
也可以将某些类型T强制为数字,并支持+,-,*和/等基本运算符。     

解决方法

因为泛型与C ++模板不同;允许部分专业化。 当然,您可以拥有:
public class Generic<T,U> {}

public class Generic2<T> : Generic<T,int> {}
只要第一个泛型中出现在
T
上的任何泛型约束至少对第二个泛型重复或更严格。 至于您要求的限制,不可以。但是,您可以限制为值类型(当然是
where T : struct
);但这并不真正限制为“数字”。 由于运算符是静态定义的,因此对于运算符
a op b
可以出现在两种类型
T(a)
或ѭ6either中,因此运算符不包含在类合同中(无论是通过基数还是通过接口)。 结果,不可能基于它们定义类型约束。 但是,您可以定义自己的合同-像这样(此处只是天真和完全不完整的实现,仅用于演示,
in a wall of code(tm)
): (在最后检查通用名称,以查看它是否有效)
public abstract class MyNumeric
{
  public abstract object ValueObject { get; }
  public abstract MyNumeric Add(MyNumeric other);
  public abstract MyNumeric Substract(MyNumeric other);
  public abstract MyNumeric Multiply(MyNumeric other);
  public abstract MyNumeric Divide(MyNumeric other);

  public static MyNumeric operator +(MyNumeric a,MyNumeric b)
  {
    return a.Add(b);
  }
  public static MyNumeric operator -(MyNumeric a,MyNumeric b)
  {
    return a.Substract(b);
  }
  public static MyNumeric operator *(MyNumeric a,MyNumeric b)
  {
    return a.Multiply(b);
  }
  public static MyNumeric operator /(MyNumeric a,MyNumeric b)
  {
    return a.Divide(b);
  }
  //etc
}

public abstract class MyNumeric<T> : MyNumeric
  where T : struct
{
  public override object ValueObject { get{ return this.Value;}}
  public new T Value { get; private set; }
  public MyNumeric(T value) { Value = value; }
}

public class MyInt : MyNumeric<int>
{
  public MyInt(int value) : base(value) { }
  public override MyNumeric Add(MyNumeric other)
  {
    //could be really crafty here and use an interface instead that
    //gives access to the Value part only - that way you could
    //have MyDouble,for example,implement INumeric<int> explicitly 
    //via a c# explicit conversion.
    MyNumeric<int> otherInt = other as MyNumeric<int>;
    if (otherInt == null)
      throw new ArgumentException(
        \"Need to handle numeric promotion/demotion for all types\",\"other\");
    return new MyInt(Value + otherInt.Value);
  }
  public override MyNumeric Divide(MyNumeric other)
  {
    throw new NotImplementedException();
  }
  public override MyNumeric Multiply(MyNumeric other)
  {
    throw new NotImplementedException();
  }
  public override MyNumeric Substract(MyNumeric other)
  {
    throw new NotImplementedException();
  }
}

public class MyGeneric<TNumeric> where TNumeric : MyNumeric
{
  public TNumeric WillNowCompile(TNumeric a,TNumeric b)
  {
    //cast is still required on the result.
    //however - you can set the return type to MyNumeric instead if you want.
    return (TNumeric)(a + b);
  }

  public TNumeric AnotherOne<TNumeric2>(TNumeric a,TNumeric2 b)
    where TNumeric2 : MyNumeric
  {
    return (TNumeric)(a / b);
  }
}
但是,正如我在代码中所说的那样,您将不得不考虑如何处理诸如
int
+ѭ10situations之类的情况。但是可以以此为起点来完成。     ,通用“ 2”参数允许与类使用的参数类型无关。 如果您想使用
int
,请在课程中使用
int
。如果您要规定类中的某些内容必须为“ 9”,则可以通过公共方法或构造函数的参数来实现。 对运算符的支持将取决于它自己传递的类型。如果您为该类型重写了运算符,则一旦在类中使用过它,它将支持该运算符。您不能对未知的通用名称执行此操作。     ,对于问题的第二部分,您可以执行以下操作:
class MyClass<T>  where T : IEquatable<T>,IComparable<T>
没有所有数字的父类,但是它们确实实现了这些接口。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?