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

查看:32
本文介绍了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?

周末愉快.

杰克

更新: 这开始变得更有意义了.有人可以给我举个例子,说明什么时候适合使用 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 Beans 而言,它们最初的用途是用于 GUI 编程.使用 JavaBeans 的简单"方法是使用命名约定来定义 bean 的属性"——因此是 getter 和 setter.据我所知,JavaBeans 最初是一种基于 GUI 的表单和 UI 编辑实现.因此 getter 和 setter 使 UI 软件可以轻松地发现用户可查看或可编辑的属性.使用 Bean Descriptor,您可以稍微改变描述符的工作方式...

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天全站免登陆