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

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

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并显示nameaddressphone

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&#39;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() + &quot;\n&quot; + 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(&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;);
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:

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&#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,你应该添加一个健全性检查:

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+…);
}

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:

确定