Tag: inheritance

如何在运行时获取DiscriminatorValue

我们有以下课程 @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) // optional annotation as this is default @DiscriminatorColumn(name = “apType”, discriminatorType = DiscriminatorType.STRING, length = 255) @DiscriminatorValue(“AP”) public class ApplicationProcess { } 和这个 @Entity @DiscriminatorValue(“APS”) public class ApplicationProcessScheme extends ApplicationProcess { } 现在我需要在运行时知道ApplicationProcess是否是DiscriminatorValue AP or APS 。 由于这是由jpa自动处理的,我无法获得此值。 我们正在调用一个以ApplicationProcess作为参数的方法,我想避免使用instanceof来检查它是什么类型。 如果我可以做类似的事情会更酷 applicationProcess.getApType().equals(“AP”);

如何设计扩展

有一个Checkstyle规则DesignForExtension 。 它说:如果你有一个公共/受保护的方法,它不是抽象的,也不是最终的也不是空的,它不是“为扩展而设计的”。 请在Checkstyle页面上阅读此规则的说明以获取基本原理。 想象一下这个案子。 我有一个抽象类,它定义了一些字段和这些字段的validation方法: public abstract class Plant { private String roots; private String trunk; // setters go here protected void validate() { if (roots == null) throw new IllegalArgumentException(“No roots!”); if (trunk == null) throw new IllegalArgumentException(“No trunk!”); } public abstract void grow(); } 我还有一个植物的子类: public class Tree extends Plant { private […]

使用JAX-RSinheritance

我正在使用JAX-RS作为我的Web服务。 我有共同的function,并希望使用inheritance。 我提供简单的CRUD操作。 我已经定义了这样的接口: public interface ICRUD { @POST @Consumes(“application/json”) @Produces(“application/json”) @Path(“create”) public String createREST(String transferObject); @GET @Consumes(“application/json”) @Produces(“application/json”) @Path(“retrieve/{id}”) public String retrieveREST(@PathParam(“id”) String id); @POST @Consumes(“application/json”) @Produces(“application/json”) @Path(“update”) public void updateREST(@Suspended final AsyncResponse asyncResponse, final String transferObject) ; @DELETE @Consumes(“application/json”) @Produces(“application/json”) @Path(“delete/{id}”) public String deleteREST(@PathParam(“id”) String id); } 我有一个实现这个接口的抽象类: public abstract class BaseREST implements […]

Java中的implements和extends关键字有什么区别

Java中的以下关键字有什么区别: implements , extends ?

从子类访问父类的私有实例变量?

假设我们有一个类foo ,它有一个私有实例变量bar 。 现在让我们有另一个类, baz ,它extends foo 。 如果foo中没有定义访问器方法, baz非静态方法可以访问foo的变量bar吗? 顺便说一下,我在Java工作。

如何从子类调用重写的父类方法?

如果我有一个子类,它有从父类重写的方法,并且在非常特殊的情况下我想使用原始方法,我该如何调用这些方法?

为什么我无法使用父类型访问子对象方法

对象a2属于A类但引用了类C的对象。因此,a2应该能够访问m3()。 但是,为什么不发生呢? 如果在类A中定义了m3()方法,则代码可以正常运行 class A { int var = 7; void m1() { System.out.println(“A’s m1 ,”); } void m2() { System.out.println(“A’s m2 ,”); } } class B extends A { void m1() { System.out.println(“B’s m1 ,”); } } class C extends B { void m3() { System.out.println(“c’s m3 ,” + (var + 6)); } } […]

从Java中的基类访问子类字段

我有一个名为Geometry的基类,其中存在一个子类Sphere : public class Geometry { String shape_name; String material; public Geometry() { System.out.println(“New geometric object created.”); } } 和一个子类: public class Sphere extends Geometry { Vector3d center; double radius; public Sphere(Vector3d coords, double radius, String sphere_name, String material) { this.center = coords; this.radius = radius; super.shape_name = sphere_name; super.material = material; } } 我有一个包含所有Geometry对象的ArrayList,我想迭代它以检查是否正确读取了文本文件中的数据。 […]

如何inheritance静态字段并改变它的值?

我正在开发程序/游戏,我有静态实用程序类和params。 class ParamsGeneral { public static final int H_FACTOR = 100; public static int MAX_SCORE = 1000; … } 然后我需要在某些特定情况下覆盖这些值,例如在分数有限的地图上播放。 所以我做了以下事情: class ParamsLimited extends ParamsGeneral { public static int MAX_SCORE = 500; // other params stay same } 预期用途如下: class Player { ParamsGeneral par; public Player() { if(onLimitedMap()){ par = new ParamLimited(); } } public […]

Base b2 = new Child(); 表示?

class Base { public void add() { System.out.println(“Base ADD”); } void subtract() { throw new UnsupportedOperationException(“Not yet implemented”); } } class Child extends Base { public void add(){ System.out.println(“Child ADD”); } public void subtract() { System.out.println(“Child Subtract”); } } class MainClass { public static void main(String args[]) { Base b1 = new Base(); Base b2 […]