能否在子类中对父类的方法和变量添加装饰器?

huangapple go评论69阅读模式
英文:

Can you add a decorator to parent methods and variables from a child class?

问题

可能在父类的字段或方法上添加一个 @ 装饰器而不隐藏它(不使用 super)吗?

> 下面的代码产生了如下结果:<br>
> colA = null,colA = Hello World,ttl = 360*<br><br>
请注意 toString 方法在父类中输出 null。

abstract class TableA {

	protected String colA;

	public String toString() {
		return "colA = " + this.colA;
	}
}

class TableAImpl extends TableA {

	//@Computed("ttl(value)")
	int myTtl;

	// *** 注意这个 ***
	//@PartitionKey
	protected String colA;

	public TableAImpl(int ttl, String colA) {
		this.myTtl = ttl;
		this.colA = colA;
	}

	// *** 注意这个 ***
	public String toString() {
		return super.toString() + ", colA = " + this.colA + ", ttl = " + this.myTtl;
	}
}

public class HelloWorld {

	public static void main(String[] args) {
		TableAImpl a = new TableAImpl(360, "Hello World");
		System.out.println(a.toString());
	}
}

背景:<br>
我需要支持多种数据库类型,因此我有一个通用的 Entity 类,它被特定于数据库的实现类重载。这些特定于数据库的类重载/添加了与该数据库交互的特定逻辑,应该对调用者不可见。

对于其中一个使用的数据库,Java 驱动程序的逻辑主要是通过向实体类的方法和属性添加 @ 装饰器来完成的,它们用于生成辅助代码。

当我尝试使用下面的代码为它们添加装饰器时,子类的变量会隐藏父类的变量。

英文:

Is it possible to add a @ decorator to a parent class's field or method without hiding it (without using super)?

>Code below produces:<br>
>colA = null, colA = Hello World, ttl = 360<br><br>
notice the toString has null for the parent

abstract class TableA {

	protected String colA;
	
	public String toString() {
		return &quot;colA = &quot; + this.colA;
	}
}

class TableAImpl extends TableA {
	
	//@Computed(&quot;ttl(value)&quot;)
	int myTtl;
	
    //*** NOTICE THIS  ***
	//@PartitionKey
	protected String colA;
	
	public TableAImpl(int ttl, String colA) {
		this.myTtl = ttl;
		this.colA = colA;
	}
	
    //*** NOTICE THIS  ***
	public String toString() {
		return super.toString() + &quot;, colA = &quot; + this.colA + &quot;, ttl = &quot; + this.myTtl;
	}
}

public class HelloWorld{

     public static void main(String []args){
        TableAImpl a = new TableAImpl(360, &quot;Hello World&quot;);
        System.out.println(a.toString());
     }
}

Context:<br>
I need to support multiple database types so I have a generic Entity class which are overloaded by DB specific Implementation classes. These DB specific classes overload/add the specific logic to interface with that DB, which should be invisible to the caller.

For one of the DBs used, the java driver's logic is primarily done by adding @Decorators to the Entity Class's methods and attributes which it uses to generate helper code.

When I try the below code to add decorators to them, the child class's variables hide the parent's

答案1

得分: 2

你从未设置父类的 `colA` 属性

public TableAImpl(int ttl, String colA) {
    this.myTtl = ttl;
    this.colA = colA;
    super.colA = colA;
}

为了保护父类的 `colA` 属性你应该将其设置为私有并使用 `private` 关键字以及像下面这样的 `getter` 方法

abstract class TableA {

    private String colA;

    public TableA() {}
    public TableA(String colA) {
        this.colA = colA;
    }

    public String toString() {
        return "colA = " + this.colA;
    }

    protected String getColA() {
        return colA;
    }
}

class TableAImpl extends TableA {

    //@Computed("ttl(value)")
    int myTtl;

    public TableAImpl(int ttl, String colA) {
        super(colA);
        this.myTtl = ttl;
    }

    public String toString() {
        return super.toString() + ", colA = " + this.getColA() + ", ttl = " + this.myTtl;
    }

    //*** 注意这里 ***
    //@PartitionKey
    @Override
    public String getColA() {
        return super.getColA();
    }
}

public class Main{
    public static void main(String []args){
        TableAImpl a = new TableAImpl(360, "Hello World");
        System.out.println(a.toString());
        // colA = Hello World, colA = Hello World, ttl = 360
    }
}
英文:

You've never set parent's colA attribute.

public TableAImpl(int ttl, String colA) {
    this.myTtl = ttl;
    this.colA = colA;
    super.colA = colA;
}

To protect parent's colA you should set it private with private and a getter like so :

abstract class TableA {

    private String colA;

    public TableA() {}
    public TableA(String colA) {
        this.colA = colA;
    }

    public String toString() {
        return &quot;colA = &quot; + this.colA;
    }

    protected String getColA() {
        return colA;
    }
}

class TableAImpl extends TableA {

    //@Computed(&quot;ttl(value)&quot;)
    int myTtl;

    public TableAImpl(int ttl, String colA) {
        super(colA);
        this.myTtl = ttl;
    }

    public String toString() {
        return super.toString() + &quot;, colA = &quot; + this.getColA() + &quot;, ttl = &quot; + this.myTtl;
    }

    //*** NOTICE THIS  ***
    //@PartitionKey
    @Override
    public String getColA() {
        return super.getColA();
    }
}

public class Main{
    public static void main(String []args){
        TableAImpl a = new TableAImpl(360, &quot;Hello World&quot;);
        System.out.println(a.toString());
        // colA = Hello World, colA = Hello World, ttl = 360
    }
}

huangapple
  • 本文由 发表于 2020年9月9日 00:22:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63797917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定