货币硬币继承

时间:2015-12-17 01:43:41

标签: java inheritance superclass

我的任务是这个(我必须使用继承):

设计并实现一个名为MonetaryCoin的类,该类派生自Coin类。将值存储在代表其值的货币硬币中,并添加一个返回其值的方法。创建ClientTester 用于实例化和计算几个不同MonetaryCoin个对象的总和的类。例如,Dime,Quarter和HalfDollar的总价值为85美分。 硬币继承了其父母被翻转的能力。

我的'硬币类是

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继承其父母的翻转能力呢?

1 个答案:

答案 0 :(得分:3)

您应该使用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表示您将传递未知数量的MonetaryCoins。
相关问题