构造方法
Constructors
什么是
一个方法, 用于创建对象
创建对象时会被调用
new 类名();
格式
public class 类名 {
public 类名() {
...
}
}
方法名必须跟类名一样
没有返回值类型
作用
因为创建时会被调用, 所以可以在此机会做初始化的操作
使用前
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
// Getters & Setters
...
}
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player();
player1.setAtk(100);
player1.setDef(100);
player1.setHp(100);
player2.setAtk(100);
player2.setDef(100);
player2.setHp(100);
int damage = 200;
player1.setHp(player1.getHp() - damage);
player2.setHp(player2.getHp() - damage);
}
}
使用后
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public Player(){
atk = 100;
def = 100;
hp = 100;
}
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
// Getters & Setters
...
}
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player();
int damage = 200;
player1.setHp(player1.getHp() - damage);
player2.setHp(player2.getHp() - damage);
}
}
有参初始化
利用方法重载, 构造方法也可以方法重载
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public Player(){
atk = 100;
def = 100;
hp = 100;
}
public Player(int atk, int def, int hp) {
this.atk = atk;
this.def = def;
this.hp = hp;
}
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
// Getters & Setters
...
}
Game.java
public class Game {
public static void main(String[] args) {
new Game().run();
}
public void run(){
Player player1 = new Player();
Player player2 = new Player(200, 200, 200);
int damage = 200;
player1.setHp(player1.getHp() - damage);
player2.setHp(player2.getHp() - damage);
}
}
构造方法转接调用
使用前
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public Player(){
atk = 100;
def = 100;
hp = 100;
}
public Player(int atk, int def, int hp) {
this.atk = atk;
this.def = def;
this.hp = hp;
}
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
// Getters & Setters
...
}
转接调用构造方法
使用 this()
在构造方法内调用其它构造方法
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public Player(){
this(100, 100, 100);
}
public Player(int atk, int def, int hp) {
this.atk = atk;
this.def = def;
this.hp = hp;
}
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
// Getters & Setters
...
}
转接调用其它方法
调用其它方法协助初始化
Player.java
public class Player {
private int atk;
private int def;
private int hp;
public Player(){
initialize(100, 100, 100);
}
public Player(int atk, int def, int hp) {
initialize(atk, def, hp);
}
public void initialize(int atk, int def, int hp) {
this.atk = atk;
this.def = def;
this.hp = hp;
}
public void trace(){
System.out.println("atk:" + atk + ", def:" + def);
}
// Getters & Setters
...
}
规则
构造方法可以调用其它构造方法进行初始化
也可以调用其它方法协助进行初始化
但一般方法不能使用 this()
转接调用构造方法
自动生成
无参自动生成
如果不写任何构造方法
系统自动生成一个无参构造
写了就不生成
如果写了任何构造方法
系统就不自动生成无参构造
这时, 一般会手动补一个无参构造
谁该带入构造方法
出生时必须
出生时必须的需要带入构造方法
为省略后期对象构建的
如果对象创建后, 很多属性都要赋值, 可以考虑使用有参构造快速赋值
特殊情况
其它类型转型
拷贝构造