java 調用方法引起歧義:The method XXX is ambiguous for the type XX

來源:互聯網
上載者:User

標籤:

The method XXX is ambiguous for the type XX

eclipse編譯報錯。

分析原因:方法歧義,是編譯器無法確定,代碼中使用哪一個方法。

1、調用的是自己編寫的方法

public class Ambiguous {
    public static void main(String[] args) {
        Ambiguous ambiguous = new Ambiguous();
        ambiguous.Para(null);
    }

    public void Para(String a) {
        System.out.println("String類型a" + a);
    }

    public void Para(StringBuffer a) {
        System.out.println("StringBuffer" + a.toString());
    }
}

顯然null作為參數,會導致編譯器無法識別調用哪一個方法。

這種情況,有一種特例是,如果方法的參數有繼承關係,那麼編譯器可以確定調用哪個方法,確定的原則是”我要的,你都有“。

2、調用庫方法。

有些庫靜態方法很多靜態欄位很多,為了方便調用調用,常常這樣寫:

import static org.csdn.linghushaoxia.Demo.*;
<pre name="code" class="java">import static org.csdn.linghushaoxia.Demo1.*;



如果,碰巧在別的包裡面,存在相同的方法,可就麻煩了,不容易尋找。

比如,調用了Demo.init()靜態方法,代碼裡直接就寫init();

在Demo1裡面有Demo1.init(),代碼裡再直接寫init();必然引起歧義了。

在查出歧義之後,就好解決了,加上包就可以了。

這樣子:

org.csdn.linghushaoxia.Demo.init();


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java 調用方法引起歧義:The method XXX is ambiguous for the type XX

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.