Frage

please help me to sort out the issues.because my answer getting different from Kathie siera book answer.for the below code i get compile error because of redeclaration to sifter(BB . But in the book answer is "-434"

 class AA{}
 class BB extends AA{

 }

    public class ComingThru {
    static String s="-";

    public static void main(String args[])
    {
    AA aa []=new AA[2];
    BB bb []=new BB[2];
    sifter(aa);
    System.out.println(s);
    sifter(bb);
    sifter(7);
    System.out.println(s);
    }

    static void sifter(AA... a2)
    {
    s+="1";


    }

     static void sifter(BB... a2)
    {
    s+="2";


    }
       static void sifter(BB[] a3)
    {
    s+="2";


    }
    static void sifter(Object o){
       s+="4"; 
    }

}
War es hilfreich?

Lösung

I've checked the book, the arguments for the first two functions is not AA or BB, is an array of AA or BB: It should be like this:

static void sifter(AA[] ... a2) {s += "1"}
static void sifter(BB[] ... b1) {s += "2"}

Attention that AA[] means that it accepts an array of array(AA[]) but not an array of AA. So the answer in the book is correct.

PS: In your code, it's normal that there is a compile error. Since you define both sifter(BB ... b1) and sifter(BB[] b2), the compiler cannot tell which one to choose if you pass an array of BB as argument.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top