gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:50:15 27 4
gpt4 key购买 nike

我正在尝试制作一个疯狂的库游戏,用户将输入 5 个变量(名词、形容词、动词、副词、第二个名词),然后这些变量将在我的子类中用于不同的“库”。问题是,当我输入参数时,它会将它们打印为空。我确信有一种简单的方法可以对不同的 toString 使用相同的输入,但我对继承很陌生,并且不确定这部分是如何工作的。

(我查看了其他 toString null 问题,但没有一个完全适合我的情况,或者它们的构造函数有问题。我相当确定我的问题没有问题。)

这是我的短语父类(super class):

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

}

}

最佳答案

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

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

关于java - 子类 toString 打印 null? (对不同的toStrings使用相同的参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43966093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com