货币币继承

MonetaryCoin inheritance

我的作业是这样的(必须要用到继承):

设计并实现一个名为 MonetaryCoin 的 class,它派生自 Coin class。在代表其价值的货币硬币中存储一个值,并添加一个 returns 其价值的方法。创建 ClientTester class实例化并计算几个不同MonetaryCoinobjects的总和。例如,Dime、Quarter 和 HalfDollar 的总值为 85 美分。 硬币继承了它 parent 的翻转能力。

我的硬币 class 是

import java.util.Random;

public class Coin
{
    private final int HEADS = 0;
    private final int TAILS = 1;
    private int face;
    // Constructor... sets up the coin by flipping it initially
    public Coin()
    {
        flip();
    }

    // flips the coin by randomly choosing a face value
    public void flip()
    {
        face = (int)(Math.random()*2);  //random numbers 0 or 1
    }

    // returns true if the current face of the coin is head

    public boolean isHeads()
    {
        return (face == HEADS);
    }

    // returns the current face of the coin as a string

    public String toString()
    {
        String faceName;
        if(face==HEADS)
        { faceName = "Heads"; }
        else
        { faceName = "Tails"; }
        return faceName;
    }
}

我的 MonetaryCoinClass

public class MonetaryCoin extends Coin
{

    private int value;

    public MonetaryCoin( int value )
    {
        this.value = value;
    }

    public int getValue()
    {
        return this.value;
    }

    public void setValue( int value )
    {
        this.value = value;
    }

    public int add( MonetaryCoin [] mc )
    {
        if ( mc.length >= 0 )
            return -1;
        int total = this.value;
        for ( int i = 0; i < mc.length; i++ )
        {
            total += mc[i].getValue();
        }
        return total;
    }
}

最后我的客户是

public class Client
{
    public static void main()
    {
        MonetaryCoin mc1 = new MonetaryCoin( 25 );            
        MonetaryCoin mc2 = new MonetaryCoin( 13 );       
        MonetaryCoin mc3 = new MonetaryCoin( 33 );           

        int total = mc1.add( mc2, mc3 );                               
        int value = mc2.getValue();                                     
    }
}

我的Client是唯一不能编译的。我不知道我在为客户做什么。我必须使用我之前制作的翻转命令。

请帮帮我!

更新:我的客户现在是

 public class Client
 {
 public static void main()
 {
    MonetaryCoin mc1 = new MonetaryCoin( 25 );            
    MonetaryCoin mc2 = new MonetaryCoin( 13 );       
    MonetaryCoin mc3 = new MonetaryCoin( 33 );       
    MonetaryCoin[] test = new MonetaryCoin[2];
    test[0] = mc2;
    test[1] = mc3;
    int total = mc1.add(test);                               
    int value = mc2.getValue();    
    System.out.println("total: " +total+ " values: " +value);
}
}

并编译。但是,如何才能让 Coin 继承其 parent 的翻转能力?

你应该使用 MonetaryCoin... mc 而不是 MonetaryCoin[] mc,像这样:

public class MonetaryCoin extends Coin{

    // All your other methods
    // ...

    public int add(MonetaryCoin... mc)
    {
        if ( mc.length >= 0 )
            return -1;
        int total = this.value;
        for ( int i = 0; i < mc.length; i++ )
        {
            total += mc[i].getValue();
        }
    return total;
    }

}
  1. MonetaryCoin[] mc 表示你将传入一个数组,如 { m1, m2, m3 }.
  2. MonetaryCoin... mc表示您将传入未知数量的货币。