简体   繁体   English

当只有一个实现时,为什么反射返回两个方法?

[英]Why does reflection return two methods, when there is only one implementation?

Suppose I have this code: 假设我有这个代码:

public interface Address {
    public int getNo();
}

public interface User<T extends Address> {
    public String getUsername();
    public T getAddress();    
}

public class AddressImpl implements Address {
    private int no;
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
}

public class UserImpl implements User<AddressImpl> {
    private String username;
    private AddressImpl addressImpl;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public AddressImpl getAddress() {
        return addressImpl;
    }

    public void setAddress(AddressImpl addressImpl) {
        this.addressImpl = addressImpl;
    }
}

Running the code: 运行代码:

int getAddressMethodCount = 0;
for (Method method : UserImpl.class.getMethods()) {
    if (method.getName().startsWith("getAddress")) {
        getAddressMethodCount++;
    }
}

would yield 2 for getAddressMethodCount variable; getAddressMethodCount变量将产生2; why is this so? 为什么会这样?

It's the way covariant return types are implemented. 这是协变返回类型的实现方式。 javap -private will show you more conveniently than reflection. javap -private会比反射更方便地向您展示。

The subclass with have a synthetic bridge method that handles forwarding to the more specific method. 具有合成桥接方法的子类处理转发到更具体的方法。 As far the JVM is concerned methods have a name, a sequence of raw typed parameters and a raw type return. 就JVM而言,方法有一个名称,一系列原始类型参数和一个原始类型返回。 You can overload on return type in bytecode. 您可以在字节码中重载返回类型。

A System.err.println(mehtod.getReturnType()); System.err.println(mehtod.getReturnType()); should give you different results for the two methods. 应该给你两种方法不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Java 在通过反射调用不装箱的方法时不支持访问原始返回值? - Why does Java not support accessing primitive return values when invoking methods without boxing via reflection? 为什么这只返回一个7? - Why does this only return one 7? 当只有一个实现类时,为什么要使用接口? - Why should I use an interface when there is only one implementation class? 实现只包含一个接口的两种方法 - Two methods when implementing interface containing only one 当两个接口具有冲突的返回类型时,为什么一个方法成为默认值? - When two interfaces have conflicting return types, why does one method become default? Scala:反射 API 调用具有相同名称的两个方法之一 - Scala: Reflection APIs to call one of the two methods with the same name 为什么并发散列映射在被两个线程访问时正常工作,一个使用 clear() ,另一个使用 putifAbsent() 方法? - Why does a concurrent hash map work properly when accessed by two thread, one using the clear() and other using the putifAbsent() methods? Android的EventBus实现,为什么要反思 - EventBus android implementation, why reflection 递归与堆栈实现。 为什么当Stack没有时递归会返回StackOverflow? - Recursion vs. Stack implementation. Why does recursion return StackOverflow when Stack does not? 比较器两个比较方法与反射 - Comparator two compare Methods with Reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM