Java 通用抽象方法&;功能界面及;lambda表达式

Java 通用抽象方法&;功能界面及;lambda表达式,java,generics,lambda,interface,abstract-methods,Java,Generics,Lambda,Interface,Abstract Methods,问:如果我在函数接口中使用lambda表达式,那么函数接口中的抽象方法本身就不能有“独立”的泛型参数类型?有关编译错误,请参见示例3 例1: 我的概念是,当我有一个声明泛型参数类型的抽象方法时,该类型独立于接口泛型。 test()方法中的T独立于接口MyInterface中的T。 请参阅下面的代码 //Example 1 interface MyInterface<T> { <T> T test(T t); } // Example 2 public

问:如果我在函数接口中使用lambda表达式,那么函数接口中的抽象方法本身就不能有“独立”的泛型参数类型?有关编译错误,请参见示例3

例1: 我的概念是,当我有一个声明泛型参数类型的抽象方法时,该类型独立于接口泛型。 test()方法中的T独立于接口MyInterface中的T。 请参阅下面的代码

//Example 1
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 2
public class Test{
     
    public  static void main(String... args){
        MyInterface m = (x) -> x;        
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 3
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
  
Test.java:12: error: incompatible types: invalid functional descriptor for lambda expression
                MyInterface<Integer> m = (x) -> x;
                                         ^
    method <T>(T)T in interface MyInterface is generic
  where T is a type-variable:
    T extends Object declared in method <T>test(T)
1 error
// Example 4
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    T test(T t);  
}
//示例1
接口MyInterface{
T检验(T);
}
例2: 如果我将lambda表达式应用于函数接口,它的行为会很有趣。见下文

编译并运行成功。 这是我的理解。变量m是原始类型。因此,Java不会检查test()方法的一般要求。Java不关心test()抽象方法的泛型类型是独立于MyInterface还是依赖于MyInterface。因此,在这种情况下,将返回对象类型。 请参阅下面的代码

//Example 1
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 2
public class Test{
     
    public  static void main(String... args){
        MyInterface m = (x) -> x;        
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 3
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
  
Test.java:12: error: incompatible types: invalid functional descriptor for lambda expression
                MyInterface<Integer> m = (x) -> x;
                                         ^
    method <T>(T)T in interface MyInterface is generic
  where T is a type-variable:
    T extends Object declared in method <T>test(T)
1 error
// Example 4
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    T test(T t);  
}
//示例2
公开课考试{
公共静态void main(字符串…参数){
MyInterface m=(x)->x;
}
}
接口MyInterface{
T检验(T);
}
例3: 现在,我将变量m声明为MyInterface类型的参数化类型。 编译器生成错误。 请参阅下面的代码

//Example 1
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 2
public class Test{
     
    public  static void main(String... args){
        MyInterface m = (x) -> x;        
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 3
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
  
Test.java:12: error: incompatible types: invalid functional descriptor for lambda expression
                MyInterface<Integer> m = (x) -> x;
                                         ^
    method <T>(T)T in interface MyInterface is generic
  where T is a type-variable:
    T extends Object declared in method <T>test(T)
1 error
// Example 4
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    T test(T t);  
}
//示例3
公开课考试{
公共静态void main(字符串…参数){
MyInterface m=(x)->x;
}
}
接口MyInterface{
T检验(T);
}
Test.java:12:错误:不兼容类型:lambda表达式的函数描述符无效
MyInterface m=(x)->x;
^
接口MyInterface中的方法(T)T是泛型的
其中T是一个类型变量:
T扩展方法测试中声明的对象(T)
1错误
例4: 最后,我从test()抽象方法中取出泛型参数类型。它很好用。程序编译。 抽象方法“T测试(T T)”现在依赖于接口MyInterface的通用T。 请参阅下面的代码

//Example 1
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 2
public class Test{
     
    public  static void main(String... args){
        MyInterface m = (x) -> x;        
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
// Example 3
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    <T> T test(T t);    
}
  
Test.java:12: error: incompatible types: invalid functional descriptor for lambda expression
                MyInterface<Integer> m = (x) -> x;
                                         ^
    method <T>(T)T in interface MyInterface is generic
  where T is a type-variable:
    T extends Object declared in method <T>test(T)
1 error
// Example 4
public class Test{
     
    public  static void main(String... args){
        MyInterface<Integer> m = (x) -> x;     
    }
 
}
 
interface MyInterface<T> {
 
    T test(T t);  
}
//示例4
公开课考试{
公共静态void main(字符串…参数){
MyInterface m=(x)->x;
}
}
接口MyInterface{
T检验(T);
}
如果将lambda表达式与函数接口一起使用,函数接口中的抽象方法本身就不能有“独立”的泛型参数类型

对。您的示例3演示了这一点。(这里更简单的术语是“抽象方法不能是泛型的”:泛型方法是定义自己类型参数的方法)

不能使用lambda,可以使用方法引用

interface MyInterface<T> {
    <T> T test(T t);    
}

static <T> T foo(T t) { return t; }

public static void main (String[] args) throws java.lang.Exception
{
    MyInterface<?> x = Ideone::foo;  // Compiles OK
    MyInterface<?> y = a -> a;       // Error.
}
接口MyInterface{
T检验(T);
}
静态T foo(T){return T;}
公共静态void main(字符串[]args)引发java.lang.Exception
{
MyInterface x=Ideone::foo;//编译正常
MyInterface y=a->a;//错误。
}

你已经找到了关于lambdas的事了。不知道方法引用是否对您有用

如果将lambda表达式与函数接口一起使用,函数接口中的抽象方法本身就不能有“独立”的泛型参数类型

对。您的示例3演示了这一点。(这里更简单的术语是“抽象方法不能是泛型的”:泛型方法是定义自己类型参数的方法)

不能使用lambda,可以使用方法引用

interface MyInterface<T> {
    <T> T test(T t);    
}

static <T> T foo(T t) { return t; }

public static void main (String[] args) throws java.lang.Exception
{
    MyInterface<?> x = Ideone::foo;  // Compiles OK
    MyInterface<?> y = a -> a;       // Error.
}
接口MyInterface{
T检验(T);
}
静态T foo(T){return T;}
公共静态void main(字符串[]args)引发java.lang.Exception
{
MyInterface x=Ideone::foo;//编译正常
MyInterface y=a->a;//错误。
}


你已经找到了关于lambdas的事了。不知道方法引用是否对您有用。

这是否回答了您的问题?这回答了你的问题吗?知道了!非常感谢。他们都帮了大忙!知道了!非常感谢。他们都帮了大忙!