我正在尝试使用多态并使用类。我写了一个超类Card。然后,我编写了3个子类:IDCardCallingCardDriverLicense。然后,我编写了另一个名为Billfold的类,该类应该包含其中两张卡的插槽。
我应该编写一个BillfoldTester程序,该程序将两个不同子类的对象添加到Billfold对象。

BillfoldTester中,将实例化DriverLicense对象和CallingCard对象并将其添加到Billfold中,该Card使用Card引用引用这些对象。

我真的不明白该怎么做。我创建了两个Billfold对象,但是我试图将其添加到我的Billfold a = new Card (x);中,它将无法正常工作。我尝试了,但不正确。非常感谢您的帮助。

public class BillfoldTester
{
    public static void main (String[]args)
    {
        Card x= new IDCard("Julie", 1995);
        Card j= new DriverLicense("Jess", 1997);
  //Having trouble trying to put the objects into my Billfold and print it.
    }
}

public class Billfold extends Card
{
    private String card1;
    private String card2;

    void addCard(String Card)//Not sure if this should be String
    {
        card1=Card;
    }
}

public class Card
{

   private String name;

   public Card()
   //This is my superclass
   {
      name = "";
   }

   public Card(String n)
   {
      name = n;
   }

   public String getName()
   {
      return name;
   }

   public boolean isExpired()
   {
      return false;
   }

   public String format()
   {
      return "Card holder: " + name;
   }
}
  public class IDCard extends Card
{
    //This is one of my subclasses
    private int IDNumber;
    public IDCard (String n, int id)

    {
        super(n);
        this.IDNumber=id;
    }
    public String format()
    {
        return super.format() + IDNumber;
    }
}

最佳答案

多态性示例。不确定功能是否正是您所需要的,但是您可以看到整个想法(我希望如此)。请参见Billfold类的showAllFormat()方法。

重点在于DriverLicense和IDCard的不同format()方法内。根据“真实”(或最初分配的)对象,即使您仅引用“ Card”类,也会调用不同的方法。

注意:
您没有提供DriverLicense实现,而我只是从头开始。我有一些不同的构造函数,以显示此子类可能完全不同。

import java.util.ArrayList;
import java.util.List;


class Billfold {
    List<Card> list = new ArrayList<Card>(10);

    void addCard(Card card) // Q: Not sure if this should be String
                            // A: You would like to add a Card
    {
        list.add(card);
    }

    void showAllFormat() {
        // go polymorphism !...
        // when you call this general 'format()' you see the subclasses
        // 'format()' is executed, not from 'Card' class
        for(Card x: list) {
            System.out.println(x.format());
        }
    }
}

class Card {
    private String name; /* owner */

    public Card() //This is my superclass
    {
        name = "";
    }

    public Card(String n) {
        name = n;
    }

    public String getName() {
        return name;
    }

    public boolean isExpired() {
        return false;
    }

    public String format() {
        return "Card holder: " + name;
    }
}

class IDCard extends Card {
    //This is one of my subclasses
    private int IDNumber;

    public IDCard(String n, int id) {
        super(n);
        this.IDNumber = id;
    }

    public String format() {
        return "(ID)" + super.format() + " " + IDNumber;
    }
}

class DriverLicense extends Card {
    private String type;

    public DriverLicense(String n, String type) {
        super(n);
        this.type = type;
    }

    public String format() {
        return "(DL)" + super.format() + " TYPE: " + type;
    }
}

public class BillfoldTester {
    public static void main(String[] args) {

        Card x = new IDCard("Julie", 1995);
        Card j = new DriverLicense("Jess", "AB");

        Billfold bf = new Billfold();
        bf.addCard(x);
        bf.addCard(j);

        bf.showAllFormat();
    }
}

10-07 14:12