[英]How to call a subclass method from super's associated class
Imagine I created two associated classes (Building and Person).想象一下,我创建了两个相关的类(Building 和 Person)。 A building can accommodate n people (persons) and a person can only be in a building at a time.
一栋楼可以容纳n个人(人),一个人一次只能在一栋楼里。
The code (just the relevant part of it) so far is:到目前为止的代码(只是它的相关部分)是:
public class Building {
//some attributes like name, location...
private List<Person> person;
//constructor
//some methods
}
public class Person {
//some attributes like name, age...
private Building building;
//constructor
//some methods
}
Ok, now I need to have more detail on Person so I extend this class to other two (Doctors and Parents), which have their own methods, some of them specifics for each Class.好的,现在我需要了解有关 Person 的更多详细信息,因此我将此 class 扩展到其他两个(医生和父母),它们有自己的方法,其中一些针对每个 Class 的细节。
The code:编码:
public class Parent extends Person {
// some specific attributs
public boolean born;
//constructor
//some methods
public void setChildBorn() {
this.born = true;
}
}
public class Doctor extends Person {
// some specific attributs
// constructor
// some methods
public void maternityWard() {
//HERE THE QUESTION
}
}
So, once arrived here, from maternityWard
method I would need to:所以,一旦到达这里,我需要通过
maternityWard
方法:
setChildBorn()
.setChildBorn()
。 A brief schema would be this:一个简短的架构是这样的:
Building < association > Person
/ extends \
Doctor Parent
And at last, the question: Is it possible to call an exclusive method in a subclass from another subclass?最后是一个问题:是否可以从另一个子类调用子类中的独占方法? If yes, how would be that code?
如果是,该代码将如何? In case it's possible, I believe that there's some casting here, but I'm not sure how to do this.
如果可能的话,我相信这里有一些演员,但我不知道该怎么做。
Thanks in advance for your help.在此先感谢您的帮助。
You can, but you always have the risk of getting a ClassCastException
if there was a Doctor
object in the person
list, and you forgot to use instanceof
.可以,但是如果
person
列表中有Doctor
object 并且您忘记使用instanceof
,您总是有获得ClassCastException
的风险。 If not, lets say you store the Person
in a variable called p
.如果没有,假设您将
Person
存储在一个名为p
的变量中。 Then, to access the method do:然后,要访问该方法,请执行以下操作:
((Parent) p).setChildBorn();
Is it possible to call an exclusive method in a subclass from another subclass?
是否可以从另一个子类调用子类中的独占方法?
An exclusive method's visibility in other subclasses (ie sibling classes) is the same as that in a non-sibling class and it follows the same access control as applicable for any non-sibling class.独占方法在其他子类(即同级类)中的可见性与在非同级 class 中的可见性相同,并且它遵循适用于任何非同级 class 的相同访问控制。
You can do this using instanceof
operator and casting您可以使用
instanceof
运算符和强制转换来做到这一点
public class Doctor extends Person {
//..some code
public void maternityWard() {
for(Person p : building.getPersonList()){
if(p instanceof Parent){
Parent parent = (Parent)p;
parent.setChildBorn();
}
}
}
PS Though this will help you invoking the method of Parent but this is a Bad Practice, though good to understand the language semantics and syntax. PS 虽然这将帮助您调用 Parent 的方法,但这是一种不好的做法,尽管有助于理解语言语义和语法。
Few good application design practices很少有好的应用程序设计实践
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.