代码之家  ›  专栏  ›  技术社区  ›  Pierre-olivier Gendraud

泛型类型,实现具有多个类型的泛型接口

  •  -1
  • Pierre-olivier Gendraud  · 技术社区  · 7 年前

    interface a<A, B>
    {
        A retA();
        B retB();
    }
    

    泛型方法

    private void fa<T>() where T : a<A, B>, new(){ code}
    

    这条线不行( ):

    2 回复  |  直到 7 年前
        1
  •  2
  •   Dmitrii Bychenko    7 年前

    你有 泛型参数( A B , T )内 fa 方法;每个方法都应该是 宣布 断然的 ;有几种方法可以做到这一点,例如。

      public class Sample1<A, B> {
        // Class declares A and B; method declares T
        private void fa<T>() where T : a<A, B>, new() {
          // Code
        }
      }
    
      public class Sample2 {
        // Method declares all three generic types: A, B and T
        private void fa<T, A, B>() where T : a<A, B>, new() {
          // Code 
        }
      }
    
      public class Sample3 {
        // Method declares T; A and B are resolved (explict types: string and int)
        private void fa<T>() where T : a<string, int>, new() {
          // Code
        }
      }
    
      public class Sample4<A> {
        // Class declares A; method declares T; B is resolved (explicit type int)
        private void fa<T>() where T : a<A, int>, new() {
          // Code
        }
      }
    

    等。

        2
  •  0
  •   Pierre-olivier Gendraud    7 年前

    fa<T, A, B>() where T : a<A, B>, new(){ code}