简体   繁体   English

开始Java多态性子类超类

[英]Beginning Java Polymorphism Subclass Superclass

I am trying to practice with Polymorphism and using classes. 我正在尝试使用多态和使用类。 I wrote a superclass called Card . 我写了一个名为Card的超类。 I then wrote 3 subclasses called: IDCard , CallingCard , and DriverLicense . 然后我写了3个子类,分别叫做: IDCardCallingCardDriverLicense I then wrote another class called Billfold which is supposed to contain slots for two of the cards. 然后我写了另一个名为Billfold类,它应该包含两张卡的插槽。
I am supposed to write a BillfoldTester program which adds two objects of different subclasses to a Billfold object. 我应该编写一个BillfoldTester程序,它将两个不同子类的对象添加到Billfold对象中。

In BillfoldTester , a DriverLicense object and a CallingCard object are instantiated and added to a Billfold , which refers to these objects with Card references. BillfoldTester ,实例化DriverLicense对象和CallingCard对象并将其添加到Billfold ,它使用Card引用引用这些对象。

I don't really understand how to do this. 我真的不明白怎么做。 I created two Card objects but I am trying to add it to my Billfold and it wont work. 我创建了两个Card对象,但我试图将它添加到我的Billfold ,它不会工作。 I tried Billfold a = new Card (x); 我试过Billfold a = new Card (x); but it's not right... Any help is much appreciated. 但它不对......任何帮助都非常感谢。

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;
    }
}

This is wrong. 这是错的。 A Billfold is not a Card; Billfold不是卡片; it HAS Cards. 它有卡片。

public class Billfold
{
    List<Card> cards = new ArrayList<Card>();

    void addCard(Card card) {
        if (card != null) {
            this.cards.add(card);
        }
    }
}

Prefer composition over inheritance. 更喜欢继承的组合。

The polymorphism example. 多态性的例子。 Not sure if the functionally is exactly what you need, but you can see the whole idea (I hope). 不确定功能是否正是您所需要的,但您可以看到整个想法(我希望)。 See the showAllFormat() method of Billfold class. 请参阅Billfold类的showAllFormat()方法。

The whole point is inside different format() methods of the DriverLicense and IDCard. 重点在于DriverLicense和IDCard的不同format()方法。 Depending on the 'real' (or initially assigned) object the different method will be called even if you just only refer to 'Card' class. 根据“真实”(或最初分配的)对象,即使您只是引用“卡片”类,也会调用不同的方法。

NOTE: You didn't provide your DriverLicense implementation, and my is just from head. 注意:您没有提供DriverLicense实现,而我只是从头开始。 I have a bit different constructor to show this sub-classes may be totally different. 我有一些不同的构造函数来表明这个子类可能完全不同。

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();
    }
}

Ok, you're largely on the right track, just a couple of things: 好吧,你在很大程度上走上正轨,只是做了几件事:

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

You're right, this should be: 你是对的,这应该是:

void addCard(Card card)
{
    card1=card;
}

then to add them: 然后添加它们:

public class BillfoldTester
{
    public static void main (String[]args)
    {
        Card x= new IDCard("Julie", 1995);
        Card j= new DriverLicense("Jess", 1997);
        Billfold bf = new Billfold();
        Billfold.addCard(x);
        Billfold.addCard(j);
    }
}

Then add a method to Billfold to print the cards in it. 然后向Billfold添加一个方法以在其中打印卡片。

Edit: Oh yeah, and duffymo is totally right, you don't need to extends Card on Billfold 编辑:哦,是的duffymo是完全正确的,你不需要extends CardBillfold

You should have Billfold class have two Card objects, not two Strings : 你应该让Billfold类有两个Card对象,而不是两个Strings

public class Billfold
{
    Card card1;
    Card card2;

    void addCard(Card card) {
        if (card != null) {
            if (card1 != null) {
                this.card1 = card;
            } else {
                this.card2 = card;
            }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM