简体   繁体   English

如何从 super 的关联 class 调用子类方法

[英]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方法:

  1. Iterate over the Building's Person-ListArray where the Doctor is (that's ok, there's a method for this to get them).遍历Doctor所在的建筑物的Person-ListArray(没关系,有一种方法可以获取它们)。
  2. For those objects on the ListArray who are instance of Parent (I can use instanceof Parent, so no point here), call the method setChildBorn() .对于 ListArray 上那些是 Parent 实例的对象(我可以使用 instanceof Parent,所以这里没有意义),调用方法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很少有好的应用程序设计实践

  1. higher and lower label modules should depend on only Abstractions.更高和更低的 label 模块应该只依赖于抽象。 Depending on concrete class and not on the abstractions injects high coupling.取决于具体的 class 而不是抽象注入高耦合。 Application should be designed to achieve low coupling and high cohesiveness between classes.应用程序应设计为实现类之间的低耦合和高内聚。
  2. Encapsulation (Building should not expose its state directly for access and modification, but should rather expose methods to handle its state)封装(建筑物不应直接公开其 state 以供访问和修改,而应公开处理其状态的方法)
  3. instanceof and casting is a code smell, avoid both of them. instanceof 和 cast 是一种代码味道,请避免使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM