我不确定我应该在3-5做什么。有人可以向我解释一下吗?
/ ** CreditCardTester - 测试您必须编写的CreditCard类 默认构造函数,带参数的构造函数和toString方法。 这里没有改变。 * /
public class CreditCardTester
{
public static void main(String[] args)
{
IDCard card ;
card = new CreditCard() ;
System.out.println(card) ;
System.out.println("CreditCard[name = unknown][id = 000000, expires 0] WAS EXPECTED.") ;
System.out.println(card.isExpired()) ;
System.out.println("true WAS EXPECTED.") ;
card = new CreditCard("Jane Doe", "123456", 1996) ;
System.out.println(card) ;
System.out.println("CreditCard[name = Jane Doe][id = 123456, expires 1996] WAS EXPECTED.") ;
System.out.println(card.isExpired()) ;
System.out.println("true WAS EXPECTED.") ;
card = new CreditCard("Justin Time", "11223344", 2016) ;
System.out.println(card.isExpired()) ;
System.out.println("false WAS EXPECTED.") ;
}
}
/**
IDCard.java
A IDCard object has a name and a few methods to get the name
and format the card. It never expires.
There are two todo regions ... one for the default constructor and
one for the toString method.
*/
class IDCard
{
private String name;
/**
Constructs a IDCard object with name "unknown"
*/
public IDCard()
{
name = "unknown";
}
/**
Constructs a IDCard object with given name
@param name1 the given name
*/
public IDCard(String name1)
{
name = name1 ;
}
/**
Accessor method for the name
@return the name
*/
public String getName()
{
return name;
}
/**
Tests whether the card is expired.
@return false, since this type of card is never expired
*/
public boolean isExpired()
{
return false;
}
/**
Produces a string representation of the object
Note: use getClass().getName() for "IDCard" so that the subclass name
will be correct.
@return a string representation
*/
public String toString()
{
//-----------Start below here. To do: approximate lines of code = 1
// 1. something like: "IDCard[name = Joe Turner]"
return getClass().getName() + "[name = " + name + "]" ;
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
/**
CreditCard.java
A CreditCard object is a specialized IDCard that has an ID number
and an expiration year.
This contains three todo regions: one for each constructor and one for
the toString method.
*/
//-----------Start below here. To do: approximate lines of code = 1
// 2. class ... so that CreditCard is a subclass of IDCard
class CreditCard extends IDCard
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
{
private String id ;
private int expirationYear ;
/**
Constructs an CreditCard object with an id of "000000" and expiration year 0
*/
public CreditCard()
{
super() ;
id = "000000" ;
expirationYear = 0 ;
}
/**
Constructs a CreditCard object with given name and year
@param name1 the given name
@param id1 the given id
@param expiryYear the year the card expires
*/
public CreditCard(String name1, String id1, int expiryYear)
{
//-----------Start below here. To do: approximate lines of code = 3
// 3-5. fill in this constructor and use the super call
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
/**
Tests whether the card is expired.
@return true if the current year is after the expiration year
*/
public boolean isExpired()
{
java.util.Calendar calendar = java.util.Calendar.getInstance() ;
int currentYear = calendar.get(java.util.Calendar.YEAR) ;
return expirationYear < currentYear ;
}
/**
Gives a String representation for the card and instance variables
@return the String representing this card
*/
public String toString()
{
//-----------Start below here. To do: approximate lines of code = 1
// 6. something like: "CreditCard[name = Joe Turner][id = 1342343, expires 1999]" and use the super call
return super.toString()+"[id = "+id+", expires "+expirationYear+" ]";
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
答案 0 :(得分:1)
中的行
super();
public CreditCard()
{
super() ;
明确表示您正在调用父
的构造函数public IDCard()
{
name = "unknown";
}
如果你没有打电话给super();
,那么无论如何都会默认这样做,但你必须知道这就是它的作用。