我有几个扩展父类的java类。从父类,我想从子类中调用几个方法。有人知道怎么做这个吗?感谢
实施例。
@MappedSuperclass
public class LookupBaseEntity {
private List<VehicleConfiguration> vehicleConfigurations;
public VehicleConfiguration getVehicleConfiguration() {
return vehicleConfiguration;
}
public void setVehicleConfigurations(List<VehicleConfiguration> vehicleConfigurations) {
this.vehicleConfigurations = vehicleConfigurations;
}
}
儿童班
@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;
}
}
答案 0 :(得分:4)
您永远不会让父母直接调用孩子的方法,因为父母应该不了解或依赖孩子。父级可以通过Java的OOPS能力间接调用子方法,当然也可以覆盖方法。换句话说,如果父级调用其自己的非最终非静态方法之一,它将通过允许在Java中进行虚拟方法调用的查找表来调用当前子类型的方法。