如何创建一个POJO? [英] How to create a POJO?

查看:26
本文介绍了如何创建一个POJO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始听说POJO"(Plain Old Java Objects).我用谷歌搜索了它,但仍然没有很好地理解这个概念.谁能给我一个 POJO 的清晰描述?

Recently I've started hearing about "POJOs" (Plain Old Java Objects). I googled it, but still don't understand the concept well. Can anyone give me a clear description of a POJO?

考虑一个带有变量id、name、address、salary"的类Person"——我将如何为这种情况创建一个 POJO?下面的代码是POJO吗?

Consider a class "Person" with variables "id, name, address, salary" -- how would I create a POJO for this scenario? Is the code below a POJO?

public class Person {
    //variables
    People people = new People();
    private int id;
    private String name;
    private String address;
    private int salary;


    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public int getSalary() {
        return salary;
    }

    public void setId() {
        this.id = id;
    }

    public void setName() {
        this.name = name;
    }

    public void setAddress() {
        this.address = address;
    }

    public void setSalary() {
        this.salary = salary;
    }
}

推荐答案

POJO 只是删除了限制的普通旧 Java Bean.Java Bean 必须满足以下要求:

A POJO is just a plain, old Java Bean with the restrictions removed. Java Beans must meet the following requirements:

  1. 默认无参数构造函数
  2. 对于名为 foo 的可变属性,遵循 getFoo(或布尔值的 isFoo)和 setFoo 方法的 Bean 约定;如果 foo 是不可变的,请不要使用 setFoo.
  3. 必须实现 java.io.Serializable

POJO 不强制要求任何这些.顾名思义:在JDK下编译的对象可以被认为是Plain Old Java Object.无需应用服务器、无需基类、无需使用接口.

POJO does not mandate any of these. It's just what the name says: an object that compiles under JDK can be considered a Plain Old Java Object. No app server, no base classes, no interfaces required to use.

首字母缩略词 POJO 是对 EJB 2.0 的一种反应,EJB 2.0 需要几个接口、扩展基类和许多方法来做简单的事情.有些人(其中包括 Rod Johnson 和 Martin Fowler)反对复杂性,并寻求一种无需编写 EJB 即可实现企业级解决方案的方法.

The acronym POJO was a reaction against EJB 2.0, which required several interfaces, extended base classes, and lots of methods just to do simple things. Some people, Rod Johnson and Martin Fowler among them, rebelled against the complexity and sought a way to implement enterprise scale solutions without having to write EJBs.

Martin Fowler 创造了一个新的缩写词.

Martin Fowler coined a new acronym.

Rod Johnson 写了没有 EJB 的 J2EE",写了 Spring,对 EJB 产生了足够的影响,所以 3.1 版看起来很像 Spring 和 Hibernate,并且从 VMWare 那里获得了甜蜜的 IPO.

Rod Johnson wrote "J2EE Without EJBs", wrote Spring, influenced EJB enough so version 3.1 looks a great deal like Spring and Hibernate, and got a sweet IPO from VMWare out of it.

这里有一个例子,你可以把头围起来:

Here's an example that you can wrap your head around:

public class MyFirstPojo
{
    private String name;

    public static void main(String [] args)
    {
       for (String arg : args)
       {
          MyFirstPojo pojo = new MyFirstPojo(arg);  // Here's how you create a POJO
          System.out.println(pojo); 
       }
    }

    public MyFirstPojo(String name)
    {    
        this.name = name;
    }

    public String getName() { return this.name; } 

    public String toString() { return this.name; } 
}

这篇关于如何创建一个POJO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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