[英]How to annotate an instance variable derived from an abstract Class?
I have the following abstract Java class: 我有以下抽象Java类:
abstract class AbstractDto {
String id;
public void setId(String id) { this.id = id; }
public String getId() {return id; }
}
I also have class which extends the abstract class: 我也有扩展抽象类的类:
class SomeDto extends AbstractDto {
@SomeAnnotation
String id;
}
I want to annotate an instance variable derived from an abstract class. 我想注释一个从抽象类派生的实例变量。 I am not sure if this is the way to go as I did it. 我不确定这是否是我的理想之选。 I know that Java does not provide variable overloading so this is shadowing. 我知道Java不提供变量重载,所以这是阴影。
So what happens if I do: 那么,如果我这样做会发生什么:
public void go(AbstractDto dto) {
println("dto.id: "+dto.id);
}
AbstractDto dto = new SomeDto();
dto.setId("1234");
go(dto);
Since I do shadowing when I set the id of SomeDto then there is an id
variable inherited from AbstractDto which is still not set. 由于我在设置SomeDto的ID时会进行阴影处理,因此存在一个从AbstractDto继承的id
变量,该变量仍未设置。
How can I annotate an instance variable defined in an abstract super class? 如何注释抽象超类中定义的实例变量?
Edit: When I do: 编辑:当我这样做时:
SomeDto dto = new SomeDto();
dto.setId("123");
Which id was set the one in AbstractDto or the one in SomeDto? 在AbstractDto或SomeDto中设置了哪个ID? What happens I a method in the Abstract class reads from id which id is then used? 我在Abstract类中的方法从id读取然后使用哪个id会发生什么情况?
The example you provided is called shadowing , there is no overriding for fields in Java. 您提供的示例称为shadowing ,在Java中没有覆盖字段。
More about shadowing could be found in Wikipedia : 可以在Wikipedia中找到有关阴影的更多信息:
In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. 在计算机编程中,当在某个范围(决策块,方法或内部类)中声明的变量与在外部范围中声明的变量具有相同的名称时,就会发生变量阴影。
So, to use your annotation, you can either annotate your id
variable in the abstract class (all inherited classes could use that annotated variable), or just annotate the id
variable of SomeDto
but you have to be aware that it's a new variable (no relation with the id's super class variable) 因此,要使用注释,您可以在抽象类中注释您的id
变量(所有继承的类都可以使用该注释的变量),也可以仅注释SomeDto
的id
变量,但您必须知道它是一个新变量(否与id的超类变量的关系)
Regarding your second question: 关于第二个问题:
Which id was set the one in AbstractDto or the one in SomeDto? 在AbstractDto或SomeDto中设置了哪个ID?
The id of SomeDto will be set because the reference variable type of dto
is SomeDto
. 将设置SomeDto的ID,因为dto
的引用变量类型为SomeDto
。 To explain more, in the inherited class SomeDto
you defined another id
variable which hides the first one, so each call using a reference to that class or any inherited class of SomeDto
will call the new id
variable defined in the SomeDto
class. 为了进一步解释,在继承的类SomeDto
定义了另一个隐藏第一个id
变量,因此,每次使用对该类或SomeDto
任何继承类的SomeDto
进行的调用SomeDto
将调用SomeDto
类中定义的新id
变量。
What happens I a method in the Abstract class reads from id which id is then used? 我在Abstract类中的方法从id读取然后使用哪个id会发生什么情况?
Each call to id
in the abstract class will use the id
in the abstract class, so a method implemented in AbstractDto
and using the variable id
will use the AbstractDto
one. 抽象类中对id
每次调用都将使用抽象类中的id
,因此在AbstractDto
实现并使用变量id
将使用AbstractDto
。
Annotating instance variables defined in abstract super class: 注释在抽象超类中定义的实例变量:
This is some real life examples where instance variables of abstract classes are annotated: 这是一些现实生活中的示例,其中注释了抽象类的实例变量:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.