Introduction to Java, sorting out the knowledge points of architects | Java core classes | JavaBean s

In Java, many class definitions conform to such specifications:

  • Several private instance fields;
  • Use the public method to read and write instance fields.

For example:

public class Person {
    private String name;
    private int age;

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

    public int getAge() { return this.age; }
    public void setAge(int age) { this.age = age; }
}

If the read-write method complies with the following naming conventions:

// Read method:
public Type getXyz()
// Write method:
public void setXyz(Type value)

Then this class is called JavaBean:

If the above field is XYZ, the name of the read-write method starts with get and set respectively, followed by the field name XYZ starting with an uppercase letter. Therefore, the names of the two read-write methods are getXyz() and setXyz() respectively.

The boolean field is special. Its reading method is generally named isXyz():

// Read method:
public boolean isChild()
// Write method:
public void setChild(boolean value)

We usually call a set of corresponding read methods (getter s) and write methods (setter s) as properties. For example, the name attribute:

  • The corresponding read method is String getName()
  • The corresponding write method is setName(String)

Only the property of getter is called read-only. For example, define a read-only property:

  • The corresponding read method is int getAge()
  • No corresponding write method setAge(int)

Similarly, only the setter property is called write only.

Obviously, read-only attributes are common, and write only attributes are uncommon.

Property only needs to define getter and setter methods, not necessarily corresponding fields. For example, the child read-only attribute is defined as follows:

public class Person {
    private String name;
    private int age;

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

    public int getAge() { return this.age; }
    public void setAge(int age) { this.age = age; }

    public boolean isChild() {
        return age <= 6;
    }
}

It can be seen that getter and setter are also methods of data encapsulation.

The role of JavaBean s

JavaBeans are mainly used to transfer data, that is, to combine a group of data into a JavaBean for easy transmission. In addition, JavaBeans can be easily analyzed by IDE tools to generate code for reading and writing attributes, which is mainly used in the visual design of graphical interface.

Through IDE, getter s and setter s can be generated quickly. For example, in Eclipse, enter the following code first:

public class Person {
    private String name;
    private int age;
}

Then, right-click, select "Source", "Generate Getters and Setters" in the pop-up menu, select the fields to Generate Getters and Setters in the pop-up dialog box, and click OK to complete all method codes automatically by the IDE.

Enumerating JavaBean properties

To enumerate all the attributes of a JavaBean, you can directly use the Introspector provided by the Java core library:

import java.beans.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BeanInfo info = Introspector.getBeanInfo(Person.class);
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            System.out.println(pd.getName());
            System.out.println("  " + pd.getReadMethod());
            System.out.println("  " + pd.getWriteMethod());
        }
    }
}

class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Circle of friends input: huany6880 Add Guanyi teacher for learning Java And all related doubts and problems can be consulted by private letter at any time( Java,AI direction) and Java Architect roadmap, interview question data, etc

Run the above code to list all properties and corresponding read-write methods. Note that the class attribute is brought from the getClass() method inherited from Object.

Summary

JavaBean is a class that conforms to the naming convention. It defines attributes through getter s and setter s;

Attribute is a general name, not specified in Java syntax;

You can use IDE to quickly generate getter s and setter s;

Using introspector Getbeaninfo () can get the attribute list.

Tags: Java OOP javabean

Posted by webtechatlantic on Mon, 18 Apr 2022 15:15:55 +0930