自动装箱和原始类型以匹配方法签名 [英] Auto boxing and primitive types to match method signature

查看:127
本文介绍了自动装箱和原始类型以匹配方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在版本 1.5 中,Java引入了自动装箱的概念。

In version 1.5, Java have introduced the concept of auto-boxing.

public interface SomeInterface {
    public void test(Integer val);
}

public class Main implements SomeInterface {

    /*The method signature gets different and compiler is asking to override 
    un-implemented methods*/
    public void test(int t) {

    }
}

那么为什么我为了覆盖未实现的方法而得到编译时错误,为什么上面的测试方法的参数不是 auto-boxed 来匹配父测试方法签名?

Then why I am getting compile time error for overriding un-implemented methods, why above test method's arguments are not auto-boxed to match parent test method signature?

推荐答案

这是因为子类中的方法与超类中的一个不是覆盖等效的。超类方法可以将 null 作为参数,而子类方法不能(这里没有关于自动装箱的内容)。

It's because the method in subclass is not override-equivalent with the one in super class. The super class method can take null as argument, while the subclass method can't (There's nothing about auto-boxing here).

这篇关于自动装箱和原始类型以匹配方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆