构造方法
什么是构造方法?
从字面上理解就是 创建对象 时要 执行 的方法。那么只要在 new 对象时,知道其执行的构造方法是什么,就可以在执行这个方法的时候给对象进行 属性赋值 。
构造方法也叫构造器,主要作用是创建对象,给对象中的成员进行初始化
构造方法格式
修饰符 构造方法名(参数列表){
}
构造方法特点
1.构造方法 没有返回值类型 以及 返回值 (特殊的方法,连void都没有)。
2.不需要写返回值。因为它是为构建对象的,对象创建完,方法就执行结束(只用这一次)
3.构造方法名称必须和 类名保持一致。
IDEA可以用 Alt + insert 选择构造函数生成方法
public class Test {
public static void main(String[] args) {
//使用方法
//可以直接赋值,省去调用set方法
Fu fu=new Fu("吃饭","呼吸");
System.out.println(fu.getBreathe()+" "+fu.getEat());
}
}
class Fu {
String eat;
String breathe;
public void play() {
System.out.println("打篮球");
}
//定义无参方法(默认构造)
public Fu() {
}
//定义带参方法
//通过构造方法可以为成员变量赋值,也就是对成员变量进行了初始化
public Fu(String eat, String breathe) {
this.eat = eat;
this.breathe = breathe;
}
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
public String getBreathe() {
return breathe;
}
public void setBreathe(String breathe) {
this.breathe = breathe;
}
}
默认构造
注意:上述代码为何要有个无参方法:
当在编译Java文件时,编译器会自动给class文件中添加 默认的构造方法。如果在描述类时,我们显式指定了构造方法,那么,当在编译Java源文件时,编译器就不会再给 class 文件中添加 默认构造 方法。
class Fu {
//如果没有显式指定构造方法,编译会在编译时自动添加默认的构造方法
//public Fu(){} //空参数的默认构造方法
}
当在描述事物时,要根据描述事物的特点来确定是否需要构造方法:
当描述的事物在 创建 其对象时就要 明确属性 的值,这时就需要在定义类的时候书写带参数的构造方法。
若创建对象时不需要明确具体的数据,这时可以不用书写构造方法(不书写也有默认的构造方法)。
建议无论如何都把无参方法写上
构造方法和一般方法区别
到目前为止,学习两种方法,分别为 构造方法 和 普通方法 ,那么他们之间有什么异同呢?
构造方法在对象创建时就执行了,而且只执行一次。
一般方法是在对象创建后,需要使用时才被对象调用,并可以被多次调用。
有了构造方法之后可以对对象的属性进行初始化,那么还需要对应的set和get方法吗?
需要相应的set和get方法,因为对象在创建之后需要修改和访问相应的属性值时,在这时只能通过set或者get方法来操作。
public class Test {
public static void main(String[] args) {
//使用无参构造
Student s = new Student();
System.out.println(s.getName());
System.out.println("---------使用带参构造方法------------------------");
//使用带参构造方法
Student s2 = new Student("学员一");
System.out.println(s2.getName());
System.out.println("---------使用带两个参构造方法------------------------");
//使用两个带参构造
Student s3 = new Student("学员二", 18);
System.out.println(s3.getName() + "---" + s3.getAge());
//s3 = new Student("学员三",19);
s3.setAge(19);
System.out.println(s3.getName() + "---" + s3.getAge());
//我们之前通过set方法进行赋值,现在可以通过构造器进行赋值
}
}
class Student {
private String name;
private int age;
//构造方法的重载
public Student() {
System.out.println("我是无参构造");
//this.name = "学员一";
//通过构造方法可以为成员变量赋值,也就是对成员变量进行了初始化
}
//这是一个带一个参数的构造
public Student(String name) {
this.name = name;
}
//这是带两个参数的构造
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//对外提供公共的
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
结果:
我是无参构造
null
---------使用带参构造方法------------------------
学员一
---------使用带两个参构造方法------------------------
学员二---18
学员二---19
方法重载
当我们只想用一个属性时就会报错
Fu fu=new Fu(“吃饭”);
public class Test {
public static void main(String[] args) {
//此时括号报错,因为缺少了"呼吸"
Fu fu=new Fu("吃饭");
System.out.println(fu.getEat());
}
}
因此可以使用方法的重载
public class Test {
public static void main(String[] args) {
Fu fu=new Fu("吃饭","呼吸");
System.out.println(fu.getBreathe()+" "+fu.getEat());
Fu fu1=new Fu("吃饭");
System.out.println(fu1.getEat());
}
}
class Fu {
String eat;
String breathe;
public void play() {
System.out.println("打篮球");
}
//定义无参方法
public Fu() {
}
//定义带参方法
public Fu(String eat, String breathe) {
this.eat = eat;
this.breathe = breathe;
}
//定义带一个参的方法
public Fu(String eat) {
this.eat = eat;
}
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
public String getBreathe() {
return breathe;
}
public void setBreathe(String breathe) {
this.breathe = breathe;
}
}
此时就可以用单个的eat属性了
重载注意事项
如果我们没有给出构造方法,系统将自动提供一个无参构造方法。
如果我们给出了构造方法,系统将不再提供默认的无参构造方法。
注意:这个时候,如果我们还想使用无参构造方法,就必须自己给出。
建议永远自己给出无参构造方法