Using inherited overloaded methods

后端 未结 11 1753
北荒
北荒 2020-12-15 05:50

I have two classes:

public class ClassA {
    public void method(Number n) {
        System.out.println(\"ClassA: \" + n + \" \" + n.getClass());
    }
}
         


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 06:11

    because number 3 is automatically boxed to Integer.

    please see the link below: http://www.javabeat.net/articles/print.php?article_id=31

    General Rule: Arguments are implicitly widened to match method parameters. It's not legal to widen from one wrapper class to another.

提交回复
热议问题