Java bean,注释:他们做什么?他们如何帮助我? [英] Java beans, annotations: What do they do? How do they help me?

查看:208
本文介绍了Java bean,注释:他们做什么?他们如何帮助我?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所理解的:

Here is what what I understand so far:

Java bean只是为了帮助其他事情(可视化的东西?)与你的代码交互。我认为这主要是为了UI的东西,因为它更容易设计这个视觉。使用Java bean进行非UI操作是不好的做法吗?

Java beans are just to help other things (visual things?) interact with your code. I think that this is mostly for UI stuff because it is easier to design this visually. Is it bad practice to use Java beans for non-UI things?

Java bean具有getter和setter方法(糟糕的OOP练习),并且是可序列化的。

Java beans have getter and setter methods (bad OOP practice) and are serializable.

在注解方面,我认为用户定义的注释不提供任何功能。一些注释,如@depretiated引发编译器警告。用户定义的注释可以这样做吗?用户定义的注释对文档以外的其他任何东西都有好处吗?我该怎么用? eclipse还是intellij有一些涉及注释的功能?

In terms of annotations, I think that user defined annotations do not provide any functionality. Some annotations like @depretiated raise compiler warnings. Can user defined annotations do this somehow? Are user defined annotations good for anything other than documentation? How can I use them? Does eclipse or intellij have some feature that involves annotations?

有一个很好的周末。

Jake

更新:开始更有意义。有人可以参考一下使用java bean格式何时适合的例子吗?

Update: that is starting to make more sense. Could someone refer me to an example of when it would be appropriate to use the java bean format and when it would not?

另外,我读到几个类可以是一个bean,它是一种打包类的方式。

Also I read somewhere that several classes can be a bean and it is a way of packaging classes.

只是为了澄清一件事情。我有95%的确定,作为一个java bean有点像一个单例(或其他模式)。它不影响编译器的作用。

Just to clarify one more thing. I am 95% sure that being a java bean is somewhat like being a singleton (or other pattern). It does not affect what the compiler does.

推荐答案

注释是一种形式的声明编程。首先,在注释的实用程序变得清楚之前,您必须了解声明式编程的好处。实质上,您可以通过声明它具有一定的特征,将代码块的功能或行为添加到代码块中。这实际上是写出一系列语句来应用或设置相同的行为。

Annotations are a form of declaritive programming. First, you have to grok the benefits of declaritive programming before the utility of annotations becomes clear. In essence, you can add functionality or behavior to a block of code simply by "declaring" that it has a certain characteristic. This is in contrast to actually writing out a series of statements to apply or setup the same behavior.

JPA注释是添加注释功能的示例。我不知道我的头顶上的用户创建示例,但JPA注释的实现方式与您或我将会完全相同。

The JPA annotations are an example of adding functionality with annotations. I don't know of "user created" example off the top of my head, but the JPA annotations are implemented exactly the same way that you or I would do it.

就Java Bean而言,它们的原始用途是用于GUI编程。使用JavaBeans的简单方法是使用命名约定来定义bean的属性 - 因此getter和setter。据我所知,JavaBeans最初是基于GUI编辑窗体和UI的实现。因此,getter和setter可以方便UI软件发现用户可浏览或可编辑的属性。使用Bean描述符,您可以改变描述符的工作方式。

As far as Java Beans, the original use of them was for GUI-programming. The "easy" way of using JavaBeans was to use naming conventions to define the "attributes" of a bean--hence getters and setters. As far as I know, JavaBeans were originally an implementation for GUI-based editing of forms and UI's. So the getters and setters made it easy for the UI software to discover user-viewable or editable attributes. With a Bean Descriptor you can change the way descriptors work a bit...

他们坚持到今天的原因是它们提供了一个事实上的方式来检查对象的公开暴露的属性。在GUI之外使用JavaBeans也是不错的形式。 Java中的偏好似乎是使用no arg构造函数,然后注入您的依赖关系,而不是使用RAII风格的编程(不是它是严格可用的)...

The reason they persist to this day is they provide a de facto way to inspect objects for publicly exposed properties. It is not bad form to use JavaBeans outside of a GUI. The preference in Java seems to be to use the no arg constructor and then inject your dependencies rather than using RAII style of programming (not that it's strictly available)...

实际上很常见,特别是如果对象将被不知道它将被操纵的对象的代码操纵(看看 Hibernate 为一个很好的例子)。

It's actually quite common, particuarly if the object will be manipulated by code that does not know a priori the object it will be manipulating (take a look at Hibernate for a good example).

这篇关于Java bean,注释:他们做什么?他们如何帮助我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆