将Java泛型转换为C#泛型
这是我的通用bubbleSorter的Java代码:将Java泛型转换为C#泛型,c#,generics,C#,Generics,这是我的通用bubbleSorter的Java代码: public class BubbleSorter<E extends Comparable<E>> { E[] a; void swap(int i, int j) { E temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } void bubbleSort(E[] a) { t
public class BubbleSorter<E extends Comparable<E>> {
E[] a;
void swap(int i, int j) {
E temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void bubbleSort(E[] a) {
this.a=a;
for (int i=0 ;i<a.length;i++) {
for (int j=0;j<a.length;j++) {
if ( a[i].compareTo(a[j]) > 0) swap(i,j);
}
}
}
}
public interface Comparable<E> {
public int compareTo(E e);
}
公共类BubbleSorter{
E[]a;
无效交换(整数i,整数j){
E温度;
温度=a[i];
a[i]=a[j];
a[j]=温度;
}
void bubbleSort(E[]a){
这个a=a;
对于(int i=0;i0)返回1;
如果(x==0)返回0;
返回-1;
}
公共双getLength(){
返回Math.sqrt(real*real+img*img);
}
}
当我试图将Java代码转换为C#时,由于abstract class Comparable<E> {
static bool operator ==( Comparable<E> e1, Comparable<E> e2 );
static bool operator !=( Comparable<E> e1, Comparable<E> e2 ) {
return !( e1 == e2 );
}
static bool operator >( Comparable<E> e1, Comparable<E> e2 );
static bool operator >=( Comparable<E> e1, Comparable<E> e2 ) {
if ( e1 > e2 ) return true;
if ( e1 == e2 ) return true;
return false;
}
static bool operator <=( Comparable<E> e1, Comparable<E> e2 ) {
return !( e1 > e2 );
}
static bool operator <( Comparable<E> e1, Comparable<E> e2 ) {
return !( e1 >= e2 );
}
}
public class BubbleSorter<E : Comparable<E>> {
E[] a;
void swap(int i, int j) {
E temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void bubbleSort(E[] a) {
this.a=a;
for (int i=0 ;i<a.length;i++) {
for (int j=0;j<a.length;j++) {
if ( a[i]>a[j] ) swap(i,j);
}
}
}
}
抽象类{
静态布尔运算符==(可比e1,可比e2);
静态布尔运算符!=(可比e1,可比e2){
返回!(e1==e2);
}
静态布尔运算符>(可比e1,可比e2);
静态布尔运算符>=(可比e1,可比e2){
如果(e1>e2)返回true;
如果(e1==e2)返回true;
返回false;
}
静态布尔算子e2);
}
静态布尔运算符=e2);
}
}
公共类气泡排序器{
E[]a;
无效交换(整数i,整数j){
E温度;
温度=a[i];
a[i]=a[j];
a[j]=温度;
}
void bubbleSort(E[]a){
这个a=a;
对于(inti=0;i您应该使用内置的IComparable
接口,然后将类声明为
public class BubbleSorter<T> where T : IComparable<T> { ... }
公共类BubbleSorter,其中T:IComparable{…}
where
关键字在泛型参数T
上定义了一个“约束”。编译器将通过确保对于泛型类的任何实例化,类型参数实现IComparable
接口来实施该约束。这是C语法:
公共类BubbleSorter,其中E:可比较
在C#中用于一般约束的关键字是
因此,首先声明泛型类型的签名:
public class BubbleSorter<E>
在冒号(:
)后面,可以指定以逗号分隔的接口列表,也可以指定类名。编译器知道哪个是哪个,因此不必显式指定是要实现(接口)还是要继承(从类中继承)
公共类BubbleSorter
{
静态无效交换(E[]a,int i,int j)
{
E温度;
温度=a[i];
a[i]=a[j];
a[j]=温度;
}
public void BubbleSort(E[]a,IComparer comparer)
{
对于(int i=0;我正在尝试学习。仅此而已。如果我希望能够继承呢?如何填写?@OfekRon静态方法(因此,运算符也会重载)不能是虚拟的,这意味着它们不能被重写或抽象。很好,谢谢。但是如果我想继承怎么办?我如何填写语法?@OfekRon我不确定我是否遵循。你想从BubbleSorter
?还是从IComparable
?如果可以修改E
的实现,这很好。否则,需要提供一个实现IComparer
的助手类。
public class BubbleSorter<E> where E : Comparable<E>
public class BubbleSorter<E>
where E : IComparable<E>
public class BubbleSorter<T>
where T : IComparable<T>
{
// ...
}
public class BubbleSorter<E>
{
static void Swap(E[] a, int i, int j)
{
E temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public void BubbleSort(E[] a, IComparer<E> comparer)
{
for (int i=0 ;i<a.length;i++) {
for (int j=0;j<a.length;j++) {
if ( comparer.Compare(a[i],a[j]) > 0 ) swap(a,i,j);
}
}
}
}