简体   繁体   English

了解Java Setter和Getters

[英]Understanding Java Setters And Getters

I'm working on a Java project I didn't code, written with Tapestry 4. Here's an example of some code: 我正在研究一个我没有编写的Java项目,用Tapestry 4编写。这是一些代码的例子:

@Persist
public abstract String getParentClientId();
public abstract void setParentClientId(String str);

My knowledge of Java is limited. 我对Java的了解有限。 I'm accustomed to seeing getters return something and setters, well, set something. 我习惯于看到吸气剂返回东西和制定者,好吧,设置一些东西。 There's no {} used to define these methods and no code between them like I expect. 没有{}用于定义这些方法,并且它们之间没有像我期望的那样的代码。 Is there some convention I'm missing? 我缺少一些约定吗? Perhaps getParentClientId() implicitly returns the value of ParentClientId, or something of the like? 也许getParentClientId()隐式返回ParentClientId的值,或类似的东西? Any info you have would be great. 你有任何信息都会很棒。 Thanks! 谢谢!

those are abstract methods that's why they dont have any implementation. 那些是abstract methods ,这就是为什么他们没有任何实现。 To know more about the abstract class and methods see Oracle's documentation on Abstract Methods : 要了解有关抽象类和方法的更多信息,请参阅Oracle关于抽象方法的文档

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: 抽象方法是在没有实现的情况下声明的方法(没有大括号,后跟分号),如下所示:

 abstract void moveTo(double deltaX, double deltaY); 

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. 当抽象类被子类化时,子类通常为其父类中的所有抽象方法提供实现。 However, if it does not, the subclass must also be declared abstract. 但是,如果没有,则子类也必须声明为abstract。

typical implementation of setter and getter is: setter和getter的典型实现是:

public String getParentClientId() { return parentClientId;}

public void setParentClientId(String str){ parentClientId = str;}

If method have abstract modifier means that it should be implmeneted in subclass 如果方法有abstract修饰符意味着它应该在子类中被赋予

你的方法是抽象的,你必须实现tham)

The methods are defined as abstract hence there is no code with it. 这些方法被定义为抽象,因此没有代码。

Maybe some tapestry API will implement these abstract method by creating dynamic classes as you have used @Persist for getParentClientId(). 也许一些tapestry API将通过创建动态类来实现这些抽象方法,就像你使用@Persist for getParentClientId()一样。 If these methods are working in your code then this must be the case. 如果这些方法在您的代码中正常工作,则必须如此。

Those are abstract classes. 那些是抽象类。 They are used to define methods that have to be present on any instance of that class, but are different for each subclass. 它们用于定义必须存在于该类的任何实例上的方法,但每个子类的方法都不同。 They are actually defined in subclasses. 它们实际上是在子类中定义的。
Of course no objects of the class that defines the abstract methods can be created, as it wouldn't have an implementation of that methods. 当然,不能创建定义抽象方法的类的对象,因为它不具有该方法的实现。 Therefore, only abstract classes can include abstract methods - and one can never create instances of abstract classes. 因此,只有抽象类可以包含抽象方法 - 而且永远不能创建抽象类的实例。

I 'm not a Tapestry 4 user but, as far as I know Tapestry 4 used a lot of abstract method declarations to hook functionality you don't have to code (as getters/setters for example). 我不是Tapestry 4的用户,但据我所知,Tapestry 4使用了很多抽象方法声明来挂钩您不必编写代码的功能(例如getter / setter)。

Afterwards Tapestry 4 would create a new class in runtime that inherits your class and implements some of the abstract methods you declared (based in certain Tapestry 4 convention). 然后Tapestry 4将在运行时创建一个新类,它继承您的类并实现您声明的一些抽象方法(基于某些Tapestry 4约定)。

I 'ma Tapestry 5 user, and in this new version abstract methods are not required by the framework they 've changed the way the runtime created classes are created (they 're now using the Plastic framework, and previously javassist) in order to avoid those rare abstract declarations in favor of use of brand new Annotations. 我是Tapestry 5用户,在这个新版本中,框架不需要抽象方法,他们改变了创建运行时创建类的方式(他们现在使用的是Plastic框架,以及之前的javassist)以避免那些罕见的抽象声明支持使用全新的注释。

If you have the time to migrate to T5 don't doubt it! 如果你有时间迁移到T5,不要怀疑它!

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

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