輸出是Card Card。它被認爲是不爲人知的簡。我該如何解決?我試圖用Card.getClass()。getName()修復它,但那給了我另一個錯誤非靜態方法getClass()不能從靜態上下文中引用。Java getClass()。getName()它返回的類不是名稱
public class Card
{
private String name;
public Card()
{
name = "unknown";
}
public Card(String name1)
{
name = name1 ;
}
public String getName()
{
return name;
}
public String toString()
{
return getClass().getName();
}
}
public class CardTester
{
public static void main(String[] args)
{
Card card ;
card = new Card() ;
System.out.println(card) ;
System.out.println("unknown WAS EXPECTED") ;
card = new Card("Jane") ;
System.out.println(card) ;
System.out.println("Jane WAS EXPECTED") ;
}
}