2012-07-02 72 views
2

这是针对通用bubbleSorter我的Java代码:转换Java泛型到C#泛型

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); 
} 

,这里是使用它的一个例子:

public class Test { 
    public static void main (String arg[]) { 
     Rational[] a = new Rational[3]; 
     a[0]=Rational.rationalFactory(9,2); 
     a[1]=Rational.rationalFactory(1,3); 
     a[2]=Rational.rationalFactory(10,11); 
     Complex[] b = new Complex[3]; 
     b[0]=new Complex(7,5); 
     b[1]=new Complex(3,4); 
     b[2]=new Complex(8,9); 
     BubbleSorter<Rational> br=new BubbleSorter<Rational>(); 
     BubbleSorter<Complex> bi=new BubbleSorter<Complex>(); 
     br.bubbleSort(a); 
     bi.bubbleSort(b); 
     for (int i=0 ; i < 3 ; i++) { 
      System.out.print(a[i] + " "); 
     } 
     System.out.println(); 
     for (int i=0 ; i < 3 ; i++) { 
      System.out.print(b[i] + " "); 
     } 
    } 
} 

public class Rational implements Comparable<Rational> { 
    int mone,mehane; 
    private Rational(int n,int m) { 
     mone=n; 
     mehane=m; 
    } 
    static public Rational rationalFactory(int n,int m) { 
     if (n==0) return null; 
     return new Rational(n,m); 
    } 
    public String toString() { 
     return mone + "/" + mehane; 
    } 
    public int compareTo(Rational r) { 
     return (r.mehane*mone - r.mone*mehane); 
    } 
} 
public class Complex implements Comparable<Complex> { 
     int real,img; 
     public Complex(int r,int i) { 
      real=r; 
      img=i; 
     } 
     public String toString() { 
      return real + "+" + img + "i"; 
     } 
     public int compareTo(Complex r) { 
      double x=(getLength() - r.getLength()); 
      if (x>0) return 1; 
      if (x==0) return 0; 
      return -1; 
     } 
     public double getLength() { 
      return 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); 
       } 
      } 
     } 

} 
+0

我在努力学习。就这样。 –

回答

7

您应该使用内置IComparable<T>界面,然后声明类为

public class BubbleSorter<T> where T : IComparable<T> { ... } 

where关键字定义了一个“约束”通用参数T。编译器将通过确保对泛型类的任何实例化来强制执行此约束,类型实参实现接口IComparable<T>

+0

不错,谢谢。但如果我想能够继承呢?我如何填充它的语法? –

+0

@OfekRon我不知道我关注。你想继承'BubbleSorter '吗?或从'IComparable '? – dlev

+0

nvm,我只是尝试了视觉和管理..非常感谢! –

1

这是C#语法:

public class BubbleSorter<E> where E : Comparable<E> 
+0

以及如果我想能够继承?我如何填写它? –

+0

@OfekRon静态方法(因此也是运算符重载)不能是虚拟的,这意味着它们不能被覆盖或抽象。 – Servy

2

在C#中使用泛型约束的关键词是where

因此,首先声明你的通用类型的签名:

public class BubbleSorter<E> 

然后定义通用约束:

where E : IComparable<E> 

编码约定的一个字:在C#,习惯上请拨打单个通用参数T(类似类型)而不是E(如元素)。这是在所有的框架集合类使用的模式,所以你可能要调整你的类型名称:

public class BubbleSorter<T> 
    where T : IComparable<T> 
{ 
    // ... 
} 

冒号(:),你可以指定一个逗号分隔的接口列表以及可能的类名称的背后。编译器知道哪个是哪个,所以你不必明确指定你想实现(接口)还是继承(从类)。

1

在C#中,实现类型可比性的一种常见惯用方法是使其来自IComparable<T>。如果你不能改变类型来实现IComparable,那么你可以实现一个辅助类来实现IComparer<E>

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); 
      } 
     } 
    } 
}