简体   繁体   English

Java从父类获取子方法

[英]Java get child method from parent class

I have a few java classes that extend a parent class. 我有几个扩展父类的java类。 From the parent class, I'm looking to call a couple methods from the child class. 从父类,我想从子类中调用几个方法。 Does anybody know how to do this? 有人知道怎么做这个吗? Thanks 谢谢

Example. 例。

@MappedSuperclass
public class LookupBaseEntity {

    private List<VehicleConfiguration> vehicleConfigurations;

    public VehicleConfiguration getVehicleConfiguration() {
        return vehicleConfiguration;
    }

    public void setVehicleConfigurations(List<VehicleConfiguration> vehicleConfigurations) {
        this.vehicleConfigurations = vehicleConfigurations;
    }

}

child class 儿童班

@Entity
public class VehicleYear extends LookupBaseEntity {

    @OneToMany(mappedBy = "vehicleYear")
    private List<VehicleConfiguration> vehicleConfigurations;

    public VehicleYear() {
    }

    public List<VehicleConfiguration> getVehicleConfigurations() {
        return vehicleConfigurations;
    } 

    public void setVehicleConfigurations(List<VehicleConfiguration> vehicleConfigurations) {
        this.vehicleConfigurations = vehicleConfigurations;
    }   

}

You would never have a parent directly call a child's methods as the parent should have no knowledge or dependence on the child. 你永远不会让父母直接调用孩子的方法,因为父母应该不知道或依赖孩子。 The parent can indirectly call a child's methods through Java's OOPS ability to override methods of course. 父级可以通过Java的OOPS能力间接调用子方法,当然也可以覆盖方法。 In other words, if a parent calls one of its own non-final non-static methods, it will call the method of the current child type via the magic of look-up tables that allow for virtual method calls in Java. 换句话说,如果父调用其自己的非最终非静态方法之一,它将通过允许Java中的虚方法调用的查找表来调用当前子类型的方法。

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

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