如何返回仅属于一个子类的变量?

huangapple go评论105阅读模式
英文:

How to return variable that is exclusive to one subclass?

问题

  1. 我有一个名为`Person`的类它有多个子类所有的子类都有相同的变量`name``address``phone`),但其中一个子类有一个额外的变量`rank`我知道如何显示`name``address``phone`但我不知道如何显示`rank`
  2. 定义类
  3. ```java
  4. public class Person {
  5. public String name;
  6. public String address;
  7. public String phone;
  8. public Person(String name, String address, String phone) {
  9. this.name = name;
  10. this.address = address;
  11. this.phone = phone;
  12. }
  13. public String toString() {
  14. return this.getClass().getName() + "\n" + name;
  15. }
  16. }

定义带有rank的子类:

  1. public class Pupil extends Person {
  2. public String rank;
  3. public Pupil(String name, String address, String phone, String rank) {
  4. super(name, address, phone);
  5. this.rank = rank;
  6. }
  7. }

创建pupil并显示nameaddressphone

  1. public class Test {
  2. public static void main(String[] args) {
  3. Person person = new Person("John", "1 John Deer Rd", "1112223333");
  4. Pupil pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
  5. System.out.println(pupil.name + "'s phone is " + pupil.phone + " and their address is " + pupil.address + ".\n");
  6. System.out.println(pupil.name + "'s rank is " + pupil.rank + ".\n");
  7. }
  8. }

如何显示rank

  1. <details>
  2. <summary>英文:</summary>
  3. 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&#39;t know how to display `rank`.
  4. Define class:

public class Person {
public String name;
public String address;
public String phone;

  1. public Person(String name, String address, String phone) {
  2. this.name = name;
  3. this.address = address;
  4. this.phone = phone;
  5. }
  6. public String toString() {
  7. return this.getClass().getName() + &quot;\n&quot; + name;
  8. }

}

  1. 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);
}

}

  1. 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");

}
}

  1. How do I also display `rank`?
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. 你可以使用 Pupil 的构造函数代替 Person 的构造函数
  6. ```java
  7. public class Pupil extends Person {
  8. public String rank;
  9. public Pupil(String name, String address, String phone, String rank) {
  10. super(name, social, phone);
  11. }
  12. }
  13. Pupil pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
  14. pupil = new Pupil("Jane", "2 John Deer Rd", "1112223333", "junior");
  15. System.out.println(pupil.name + "'s phone is " + pupil.phone + " and their address is " + pupil.address + "." + "\n" + pupil.rank);

我建议使用 getter 和 setter 方法,将字段设为私有。您可以阅读有关 getter 和 setter 方法的更多信息。以下是一个快速示例:

  1. public class Person {
  2. private String name;
  3. public String getName() {
  4. return name;
  5. }
  6. public void setName(String name) {
  7. this.name = name;
  8. // 获取名字参数并将其设置为该类的 name 参数
  9. }

最后但同样重要的是,我建议您阅读有关 this 关键字 的内容。祝您开心!

英文:

You can use Pupil 's constructor instead of Person's constructor

  1. public class Pupil extends Person {
  2. public String rank;
  3. public Pupil (String name, String address, String phone, String rank) {
  4. super(name, social, phone);
  5. }
  6. }
  7. Pupil pupil = new Pupil(&quot;Jane&quot;, &quot;2 John Deer Rd&quot;, &quot;1112223333&quot;, &quot;junior&quot;);pupil = new Pupil(&quot;Jane&quot;, &quot;2 John Deer Rd&quot;, &quot;1112223333&quot;, &quot;junior&quot;);
  8. System.out.println(pupil.name + &quot;&#39;s phone is &quot; + pupil.phone + &quot; and their address is &quot; + pupil.address + &quot;.&quot; + &quot;\n&quot;+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:

  1. public class Person {
  2. private String name;
  3. public String getName() {
  4. return name;
  5. }
  6. public void setName(String name) {
  7. this.name =name;
  8. //get name parameter and set it to this class&#39;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,你应该添加一个健全性检查:

  1. if (pupil instanceof Pupil) {
  2. System.out.println( + ((Pupil)pupil).rank + );
  3. }
英文:

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:

  1. if(pupil instanceof Pupil) {
  2. System.out.println(…+((Pupil)pupil).rank+…);
  3. }

huangapple
  • 本文由 发表于 2020年4月8日 22:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61102962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定