すべての出現箇所を、ユーザーが新しい名前で指定した名前に置き換える必要があります。ここ は私のコードです:javaメソッドの変数を置き換える方法
public class Person
{
public Person() {}
public Person(String name, String address, String email, String phone)
{
this.name = name;
this.address = address;
this.email = email;
this.phone = phone;
}
String name = "";
String address = "";
String email = "";
String phone = "";
public String toString()
{
return "Name: " + name + " Address: " + address + " Email: " + email + " Phone: " + phone;
}
}
class Student extends Person
{
public Student() {}
public Student(String studentName, String studentAddress, String studentEmail, String studentPhone)
{
this.studentName = studentName;
this.studentAddress = studentAddress;
this.studentEmail = studentEmail;
this.studentPhone = studentPhone;
}
public String toString()
{
String studentInfo = super.toString();
return studentInfo.replaceAll(name, studentName);
}
//int grade = 0; COME BACK TO THIS
String studentName = "";
String studentAddress = "";
String studentEmail = "";
String studentPhone = "";
public static final int freshman = 0;
public static final int sophomore = 1;
public static final int junior = 2;
public static final int senior = 3;
}
今、それはユーザーが通過する名前を持つすべての文字に置き換えられます。実際の名前を置き換えるにはどうすればいいですか?
'Student'クラスは等「studentName」の変数の第2のセットを必要としない - それは単に使用することができますそれは(Student)がPersonのサブクラスなので、Personクラスの "name"変数です。 'Student'コンストラクタは単に' this.name = studentName'を行うことができます。そして、Person.toStringはすでに正しいことをしているので、Studentは 'toString'をオーバーライドする必要もありません。 –