英文:
How to return variable that is exclusive to one subclass?
问题
我有一个名为`Person`的类,它有多个子类。所有的子类都有相同的变量(`name`,`address`,`phone`),但其中一个子类有一个额外的变量,`rank`。我知道如何显示`name`,`address`,`phone`,但我不知道如何显示`rank`。
定义类:
```java
public class Person {
public String name;
public String address;
public String phone;
public Person(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public String toString() {
return this.getClass().getName() + "\n" + name;
}
}
定义带有rank
的子类:
public class Pupil extends Person {
public String rank;
public Pupil(String name, String address, String phone, String rank) {
super(name, address, phone);
this.rank = rank;
}
}
创建pupil
并显示name
,address
和phone
。
public class Test {
public static void main(String[] args) {
Person person = new Person("John", "1 John Deer Rd", "1112223333");
Pupil pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
System.out.println(pupil.name + "'s phone is " + pupil.phone + " and their address is " + pupil.address + ".\n");
System.out.println(pupil.name + "'s rank is " + pupil.rank + ".\n");
}
}
如何显示rank
?
<details>
<summary>英文:</summary>
I have class `Person` which has multiple subclasses. All subclasses have the same variables (`name`, `address`, `phone`), but one has an extra variable, `rank`. I know how to display `name`, `address`, `phone` but I don't know how to display `rank`.
Define class:
public class Person {
public String name;
public String address;
public String phone;
public Person(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public String toString() {
return this.getClass().getName() + "\n" + name;
}
}
Define the subclass with `rank`:
public class Pupil extends Person {
public String rank;
public Student(String name, String address, String phone, String rank) {
super(name, social, phone);
}
}
Create `pupil` and display `name`, `address`, and `phone`.
public class Test {
public static void main(String[] args) {
Person person = new Person("John", "1 John Deer Rd", "1112223333");
Person pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
System.out.println(pupil.name + "'s phone is " + pupil.phone + " and their address is " + pupil.address + "." + "\n");
}
}
How do I also display `rank`?
</details>
# 答案1
**得分**: 1
你可以使用 Pupil 的构造函数代替 Person 的构造函数
```java
public class Pupil extends Person {
public String rank;
public Pupil(String name, String address, String phone, String rank) {
super(name, social, phone);
}
}
Pupil pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
System.out.println(pupil.name + "'s phone is " + pupil.phone + " and their address is " + pupil.address + "." + "\n" + pupil.rank);
我建议使用 getter 和 setter 方法,将字段设为私有。您可以阅读有关 getter 和 setter 方法的更多信息。以下是一个快速示例:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
// 获取名字参数并将其设置为该类的 name 参数
}
最后但同样重要的是,我建议您阅读有关 this 关键字 的内容。祝您开心!
英文:
You can use Pupil 's constructor instead of Person's constructor
public class Pupil extends Person {
public String rank;
public Pupil (String name, String address, String phone, String rank) {
super(name, social, phone);
}
}
Pupil pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
System.out.println(pupil.name + "'s phone is " + pupil.phone + " and their address is " + pupil.address + "." + "\n"+pupil .rank);
I suggest using getter and setter methods and use them and make your fields private.
You can read more about setter and getter methods
a quick example:
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name =name;
//get name parameter and set it to this class'sname parameter }
Last but not least, I recommend that you read about this keyword
be happy.
答案2
得分: 1
你必须将对象从Person
转换回Pupil
:
((Pupil)pupil).rank
如果你不是100%确定你的对象实际上是一个Pupil
,你应该添加一个健全性检查:
if (pupil instanceof Pupil) {
System.out.println(… + ((Pupil)pupil).rank + …);
}
英文:
You have to cast the object from Person
back to Pupil
:
((Pupil)pupil).rank
And if you are not 100% sure that your object is actually a Pupil you should add a sanity-check:
if(pupil instanceof Pupil) {
System.out.println(…+((Pupil)pupil).rank+…);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论