YQC asked in 電腦與網際網路程式設計 · 1 decade ago

問一個java繼承的問題(可以快點給我解答嗎??)

public class test0509

{

public static void main(String[] args)

{

Dog Amanda = new RaceDog(15, 3, 6, 20000);

Dog Bob = new RaceDog(18, 5, 7, 18000);

Dog Carol = new RaceDog(12, 4, 3, 12000);

System.out.println(Amanda.returnIndex());

>>> System.out.println(Amanda.salary_to_HI_ratio());

}

}

class Dog

{

int length, weight, age;

public Dog (){}

public Dog (int len, int wei, int a)

{

length = len;

weight = wei;

age = a;

}

public int returnIndex()

{

return length * weight / age;

}

}

class RaceDog extends Dog

{

private int salary ;

public RaceDog(int len,int wei, int a,int sa)

{

super(len,wei,a);

salary = sa;

}

public double salary_to_HI_ratio()

{

return this.returnIndex()/salary;

}

}

以上是我的程式碼...(我知道我很嫩)

我是要顯示出他們的salary可是不知道為什麼打System.out.println(Amanda.salary_to_HI_ratio());(前面有\">>>\"的那行)

它跟我說cannot find symbol

location:class Dog

為什麼不能執行子類別的方法阿???

還有要怎麼比較他們三個salary的大小~我不太知道要怎麼寫

請會寫java的大大可以給我指點一下嘛?? 謝謝 p.s 我是用gel

Update:

-------------------------------------------------------

痾...真是不好意思...不過改了以後還是有錯ˊˋ

它說:duplicate class:Race Dog

這是什麼意思阿@@

Update 2:

---------------------------------

噢....compile過了 = =

不好意思

Update 3:

要怎麼把記憶體位置轉換過來??(我忘了) 因為我要的是一個值

我說的是這裡System.out.println(Amanda.salary_to_HI_ratio()); 執行出來是3.5E-4

1 Answer

Rating
  • Dark
    Lv 4
    1 decade ago
    Favorite Answer

    public class test0509

    {

    public static void main(String[] args)

    {

    RaceDog Amanda = new RaceDog(15, 3, 6, 20000);

    RaceDog Bob = new RaceDog(18, 5, 7, 18000);

    RaceDog Carol = new RaceDog(12, 4, 3, 12000);

    System.out.println(Amanda.returnIndex());

    System.out.println(Amanda.salary_to_HI_ratio());

    int[] a=new int[3];

    a[0]=Amanda.returnsalary();

    a[1]=Bob.returnsalary();

    a[2]=Carol.returnsalary();

    for(int i=0;i<2;i++)

    {

    for(int j=i+1;j<=2;j++)

    {

    if(a[i]>a[j])

    {

    int b;

    b=a[i];

    a[i]=a[j];

    a[j]=b;

    }

    }

    }

    System.out.print("由小到大:");

    for(int i=0;i<=2;i++)

    {

    System.out.print(a[i]+" ");

    }

    }

    }

    class Dog

    {

    int length, weight, age;

    public Dog (){}

    public Dog (int len, int wei, int a)

    {

    length = len;

    weight = wei;

    age = a;

    }

    public int returnIndex()

    {

    return length * weight / age;

    }

    }

    class RaceDog extends Dog

    {

    private double salary ;

    public RaceDog(int len,int wei, int a,int sa)

    {

    super(len,wei,a);

    salary = sa;

    }

    public double salary_to_HI_ratio()

    {

    return super.returnIndex()/salary;

    }

    public int returnsalary()

    {

    return (int)salary;

    }

    }

    你宣告錯了  不是Dog  而是RaceDog

    2006-05-11 10:08:58 補充:

    這是1 個值啊 3.5E-4是因為你的 salary太大了而以

    Source(s): ME
Still have questions? Get your answers by asking now.