日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java的编译和连接方法_Java:编译时解析和“最具体的方法”

發(fā)布時(shí)間:2024/9/19 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的编译和连接方法_Java:编译时解析和“最具体的方法” 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

如果您嘗試在下面使用它們,重載函數(shù)compute1(),compute2()和compute5()會(huì)導(dǎo)致編譯錯(cuò)誤:

package com.example.test.reflect;

class JLS15Test2

{

int compute1(Object o1, Integer i, Integer j) { return 1; }

int compute1(String s1, Integer i, int j) { return 2; }

int compute2(Object o1, Integer i, int j) { return 3; }

int compute2(String s1, Integer i, Integer j) { return 4; }

int compute3(Object o1, Integer i, int j) { return 5; }

int compute3(String s1, Integer i, int j) { return 6; }

int compute4(Object o1, Integer i, Integer j) { return 7; }

int compute4(String s1, Integer i, Integer j) { return 8; }

int compute5(Object o1, Integer i, Object j) { return 9; }

int compute5(String s1, Integer i, int j) { return 10; }

public static void main(String[] args)

{

JLS15Test2 y = new JLS15Test2();

// won't compile:

// The method compute1(Object, Integer, Integer) is ambiguous

// for the type JLS15Test2

// System.out.println(y.compute1("hi", 1, 1));

// Neither will this (same reason)

// System.out.println(y.compute2("hi", 1, 1));

System.out.println(y.compute3("hi", 1, 1));

System.out.println(y.compute4("hi", 1, 1));

// neither will this (same reason)

// System.out.println(y.compute5("hi", 1, 1));

}

}

在閱讀JLS第15.12節(jié)之后,我想我理解……在第2階段(裝箱/取消裝箱允許,沒(méi)有varargs)匹配重載方法,在確定“最具體的方法”時(shí),JLS說(shuō)(實(shí)際上)最多特定方法是其形式參數(shù)是其他適用方法的子類(lèi)型的方法,而原語(yǔ)和對(duì)象(例如int和Integer)從不是彼此的子類(lèi)型.因此Integer是Integer的子類(lèi)型,int是int的子類(lèi)型,但是Integer和int是不兼容的w / r / t子類(lèi)型比較,因此compute1()/ compute2()對(duì)都沒(méi)有最具體的方法.

(在compute3()和compute4()中,使用String參數(shù)的方法比使用Object參數(shù)的方法更具體,因此程序打印6和8.)

我的推理是否正確?

總結(jié)

以上是生活随笔為你收集整理的java的编译和连接方法_Java:编译时解析和“最具体的方法”的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。