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

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

问题描述

最近我开始听说POJO(Plain Old Java Objects)。我用谷歌搜索,但仍然不理解这个概念。任何人都可以给我一个明确的POJO描述吗?



考虑一个带有变量id,name,address,salary的类Person - 我将如何创建一个这个场景的POJO?代码是否低于POJO?

 公共类人员{
//变量
人民=新人();
private int id;
private String name;
私有字符串地址;
私人工资;


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;
}
}


解决方案

A POJO只是一个简单的旧Java Bean,删除了限制。 Java Bean必须满足以下要求:


  1. 默认无参数构造函数

  2. 遵循Bean约定getFoo(或booans的isFoo)和setFoo方法,用于名为foo的可变属性;如果foo是不可变的,请不要使用setFoo。

  3. 必须实现java.io.Serializable

POJO没有强制要求任何这些。这正是名称所说的:在JDK下编译的对象可以被视为普通的旧Java对象。没有应用服务器,没有基类,不需要使用接口。



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



Martin Fowler创造了一个新的Spring写道,没有EJB的J2EE,写得很好,影响了EJB,所以版本3.1看起来很像Spring和Hibernate,并得到了一个甜蜜的IPO VMWare就此了。



以下是一个可以解决问题的例子:

  public class MyFirstPojo 
{
private String name;

public static void main(String [] args)
{
for(String arg:args)
{
MyFirstPojo pojo = new MyFirstPojo(arg) ); //这是你如何创建一个POJO
System.out.println(pojo);
}
}

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

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

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


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?

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;
    }
}

解决方案

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

  1. Default no-arg constructor
  2. Follow the Bean convention of getFoo (or isFoo for booleans) and setFoo methods for a mutable attribute named foo; leave off the setFoo if foo is immutable.
  3. Must implement java.io.Serializable

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.

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 coined a new acronym.

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