1. 程式人生 > >java:Eclipse使用快捷鍵,快速建立構造方法和get/set方法

java:Eclipse使用快捷鍵,快速建立構造方法和get/set方法

`public class person {
private String name;
private int age;
public person() {//alt+shift+s 在加c,建立父類空參建構函式
super();
}
public person(String name, int age) {//alt+shift+s 在加o,根據成員變數,建立父類有參建構函式
super();
this.name = name;
this.age = age;
}
public String getName() {//alt+shift+s 在加r,建立get和set方法
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;
}


作者:銘記不如銘心
來源:CSDN
原文:https://blog.csdn.net/qq_24644517/article/details/82886727
版權宣告:本文為博主原創文章,轉載請附上博文連結!`

方法一:

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

我建立了一個類,裡面有兩個私有屬性age和name。現在構造兩個建構函式,一個不帶引數,一個帶引數。右鍵—>source—>Generate Constructors from Superclass,建立一個空參的建構函式;右鍵—>source—>Generate Constructors using Fields,建立一個帶引數的建構函式。

public class Person2 {
    private int age;
    private String name;
    public Person2() {
        super();
    }
    public Person2(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
}

方法二:
直接上shift+alt+s,然後選擇Generate Constructors from Superclass或者Generate Constructors using Fields,分別構造空參和帶引數的建構函式

作者:芳草碧連天lc
來源:CSDN
原文:https://blog.csdn.net/leichaoaizhaojie/article/details/52675734
版權宣告:本文為博主原創文章,轉載請附上博文連結!