What is a JavaBean exactly?

后端 未结 19 1650
清酒与你
清酒与你 2020-11-22 01:55

I understood, I think, that a \"Bean\" is a Java class with properties and getters/setters. As much as I understand, it is the equivalent of a C struct. Is that true?

<
19条回答
  •  忘了有多久
    2020-11-22 02:27

    Properties of JavaBeans

    A JavaBean is a Java object that satisfies certain programming conventions:

    1. The JavaBean class must implement either Serializable or Externalizable

    2. The JavaBean class must have a no-arg constructor

    3. All JavaBean properties must have public setter and getter methods

    4. All JavaBean instance variables should be private

    Example of JavaBeans

    @Entity
    public class Employee implements Serializable{
    
       @Id
       private int id;
       private String name;   
       private int salary;  
    
       public Employee() {}
    
       public Employee(String name, int salary) {
          this.name = name;
          this.salary = salary;
       }
       public int getId() {
          return id;
       }
       public void setId( int id ) {
          this.id = id;
       }
       public String getName() {
          return name;
       }
       public void setName( String name ) {
          this.name = name;
       }
       public int getSalary() {
          return salary;
       }
       public void setSalary( int salary ) {
          this.salary = salary;
       }
    }
    

提交回复
热议问题