Play turn eclipse-automatically generate setter and Getter methods

Source: Internet
Author: User
Tags comments

In the process of program development, we often write such classes: some or all of the properties of a class do not want to be accessed directly by the outside world, without the public field being decorated. In this way, the method invocation becomes the only way to access these properties. JavaBean is a good example of strict adherence to object-oriented design logic and all properties are private. For any attribute xxx, there is a public getxxx () method to get the property and the public's Setxxx () method to modify the property. If there are only a few such attributes, you can manually add setter and getter methods to them. However, if you have a large number of such properties, manual additions can be time consuming.

Here's an example of how to automatically generate the required setter and getter methods through eclipse. The sample code is as follows:

/**
 * The Class boy.
 * * Public
class Boy {
    
    /** the name. */
    private String name;
    
    /** the age. */
    private int age;
    
    /** the smart. */
    private Boolean smart;
}
1. Basic Settings

Open the Boy.class file in the Code Editor, use the shortcut key ALT + Shift + S, press the R key (the shortcut key in your eclipse may be different), or right-click the source, Generate Getters and Setters ... The operation is shown in the following figure:


The Setup interface to enter the automatic setter and getter method is as follows:


Select

Select all: Choose to add setter and Getter methods for all properties

Deselect all: Cancels all the selected setter and Getter methods

Select Getters: Getter method for selecting all properties

Select Setters: Choose Setter method for all properties insertion point

You can choose either "first Member" for the file, "last Member", or after an element.

Sort by

Fields in Getter/setter pairs: getter and setter methods for each property are sorted in pairs

First getters, then setters: All getter methods before all setter methods Access modifier

You can choose access rights: Public,protected,default,private

You can also choose whether to be final or synchronized Comments

You can choose whether to generate comments for them while automatically generating setter and getter methods

In addition, in the code template you can set the format of the body and annotations of the automatically generated setter and Getter methods. 2. The parameter of setter method plus prefix

In general, the parameters in the automatically generated setter method are exactly the same as the attributes and need to be distinguished by this to distinguish between the same name property and parameters. Examples are as follows:

    /** * @param age-The age-to
     set
     *
    /public final void Setage (int.) {
        this.age = age;
    }

In a more rigorous code format check, this situation will prompt the ' xxx ' hides a field problem. In order to avoid this checkstyle problem, Add Org.eclipse.jdt.core.codecomplete.argumentprefixes=new to the end of the project by Org.eclipse.jdt.core.prefs file in the. Settings directory, You can precede the parameters of all setter methods that are automatically created with the new prefix. This configuration requires that you restart eclipse to take effect. This method of specific operation and analysis, you can refer to the "Play the Eclipse-Project. Settings directory decryption."

After the above configuration, the completed code after the auto-generated setter and getter method is as follows:

/**
 * The Class boy.
 * * Public
class Boy {
    
    /** the name. */
    private String name;
    
    /** the age. */
    private int age;
    
    /** the smart. */
    private Boolean smart;

    /**
     * @return the name
     *
    /public final String GetName () {
        return name;
    }

    /**
     * @param newName the name to set
     *
    /public final void SetName (String newName) {
        name = Newname;
  }

    /**
     * @return The Age
     *
    /public final int getage () {
        return age;
    }

    /**
     * @param newage The age to set
     *
    /public final void Setage (int. newage) {Age
        = newage;
    }

    /**
     * @return the Smart
     *
    /Public Final Boolean Issmart () {
        return smart;
    }

    /**
     * @param newsmart the smart to set
     *
    /public final void Setsmart (Boolean newsmart) {
        smart = NEWSM Art;
    }
}
Description

1) The parameters of the setter method automatically capitalize the first letter of the attribute and prefix it.

2) After the parameters of the setter method are prefixed to the attribute, it is not necessary to use this to differentiate between attributes and parameters.

3) For Boolean-type properties, the getter is no longer the get start, but rather begins with the IS.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.