子类toString打印null? (对不同的toStrings使用相同的参数)

时间:2017-05-14 16:09:13

标签: java inheritance null tostring

我试图创建一个疯狂的libs游戏,用户可以在其中输入5个变量(名词,形容词,动词,副词,第二名词),然后在我的子类中用于不同的" libs&# 34 ;.问题是,当我输入我的参数时,它将它们打印为null。我确定有一种简单的方法可以为不同的toStrings使用相同的输入,但我不熟悉继承,并且不确定它的部分是如何工作的。

(我查看了其他toString null问题,但是没有一个对我的情况很有用,或者他们的构造者有问题。我相当确定我的没有错。)< / p>

这是我的Phrase超类:

public class Phrase 
{
    private String noun;
    private String adjective;
    private String verb;
    private String adverb;
    private String noun2;

    public Phrase (String n, String a, String v, String ad, String n2)
    {
        n=noun;
        a=adjective;
        v=verb;
        ad=adverb;
        n2=noun2;
    }

    public String getNoun()
    {return noun;}

    public String getAdj ()
    {return adjective;}

    public String getVerb ()
    {return verb;}

    public String getAdverb ()
    {return adverb;}

    public String get2Noun ()
    {return noun2;}

    //i'll need the get methods in the libs classes and the set methods in the 
    while loop, if user wants to change parameters

    public void setNoun (String Newnoun)
    {noun=Newnoun;}

    public void setAdj (String newAdj)
    {adjective=newAdj;}

    public void setVerb (String newVrb)
    {verb=newVrb;}

    public void setAdverb (String newAdv)
    {adverb=newAdv;}

    public void set2Noun (String newNoun2)
    {noun2=newNoun2;}

}

这是我的子类:

public class Obama extends Phrase
{
    public Obama(String noun, String adjective, String verb, String adverb, 
            String noun2)
    {super (noun, adjective, verb, adverb, noun2);}

    public String getNoun()
    {return super.getNoun();}

    public String getAdj ()
    {return super.getAdj();}

    public String getVerb ()
    {return super.getVerb();}

    public String getAdverb ()
    {return super.getAdverb();}

    public String get2Noun (String n)
    {return super.get2Noun();}


    public String toString()
    {
        return ("there is a " + super.getAdj() + " " + super.getNoun() +" on the 
                floor! It is " + super.getVerb() +"ing " + super.getAdverb() +". Next to it 
                is a " + super.get2Noun()); 
    }


}

这是我的司机:

public class Madlibsdriver 
{
    static Scanner scan= new Scanner (System.in);
    public static void main(String[] args) 
    {

        System.out.print ("Welcome to Mad libs!");
        System.out.println();
        System.out.println("-------------------------------------------- ");
        System.out.println("Enter a noun:");
        String ip1= scan.nextLine();
        System.out.println("Enter an adjective:");
        String ip2= scan.nextLine();
        System.out.println ("Enter a verb:");
        String ip3= scan.nextLine();
        System.out.println ("Enter an adverb (ex: angrily) :");
        String ip4= scan.nextLine();
        System.out.println ("Enter another noun:");
        String ip5= scan.nextLine();

        Phrase obama= new Obama(ip1, ip2, ip3, ip4, ip5);

        System.out.println();
        System.out.print (obama.toString());

    }

}

1 个答案:

答案 0 :(得分:2)

您已在构造函数中交换参数,您可以设置传入的值(而不是使用它们来设置本地字段)。使用anchor apple关键字来捕获此类错误,例如

this