java基础学习记录:7.4 构造方法及重载

mac2022-06-30  24

1、作用

完成对对象中的属性赋值

2、构造方法

构造方法是一种特殊的方法,构造方法名字必须与类名一致,构造方法必须没有返回类型,不编写返回类型

语法格式:

public 方法名称([参数列表]){

 

}

public class Dog { //无参构造方法 public Dog() { //完成对品种、颜色、姓名、年龄、性别 strain = "哈士奇";//品种 color = "黑白";//颜色 name = "二哈";//姓名 age = 5;//年龄 sex = "公";//性别 } //成员变量 String strain;//品种 String color;//颜色 String name;//姓名 int age;//年龄 String sex;//性别 //输出方法 public void print() { System.out.println("品种:" + strain); System.out.println("颜色:" + color); System.out.println("名字:" + name); System.out.println("年龄:" + age); System.out.println("性别:" + sex); } }

3、构造方法如何执行

当创建对象时自动执行相匹配的构造方法

public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.print(); } }

结果为: 

4、创建带参构造方法

public class Dog { //无参构造方法 public Dog() { //完成对品种、颜色、姓名、年龄、性别 strain = "哈士奇";//品种 color = "黑白";//颜色 name = "二哈";//姓名 age = 5;//年龄 sex = "公";//性别 } //带参构造方法 public Dog(String strain,String color,String name) { //完成对品种、颜色、姓名的赋值 //局部变量的值赋值给成员变量 this.strain = strain;//品种 this.color = color;//颜色 this.name = name;//姓名 } //成员变量 String strain;//品种 String color;//颜色 String name;//姓名 int age;//年龄 String sex;//性别 //输出方法 public void print() { System.out.println("品种:" + strain); System.out.println("颜色:" + color); System.out.println("名字:" + name); System.out.println("年龄:" + age); System.out.println("性别:" + sex); } } public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.print(); System.out.println("分界线-------------------------------"); //创建Dog对象,带三个参数的构造方法 Dog dog02 = new Dog("中华田园犬","黄","阿黄"); dog02.print(); } }

输出结果:

5、创建对象的同时完成所有属性的赋值

public class Dog { //无参构造方法 public Dog() { //完成对品种、颜色、姓名、年龄、性别 strain = "哈士奇";//品种 color = "黑白";//颜色 name = "二哈";//姓名 age = 5;//年龄 sex = "公";//性别 } //带参构造方法 public Dog(String strain,String color,String name) { //完成对品种、颜色、姓名、年龄、性别的赋值 //局部变量的值赋值给成员变量 this.strain = strain;//品种 this.color = color;//颜色 this.name = name;//姓名 } //创建对所有属性赋值的构造方法 public Dog(String strain,String color,String name,int age,String sex) { //局部变量的值赋值给成员变量 this.strain = strain;//品种 this.color = color;//颜色 this.name = name;//姓名 this.age = age;//年龄 this.sex = sex;//性别 } //成员变量 String strain;//品种 String color;//颜色 String name;//姓名 int age;//年龄 String sex;//性别 //输出方法 public void print() { System.out.println("品种:" + strain); System.out.println("颜色:" + color); System.out.println("名字:" + name); System.out.println("年龄:" + age); System.out.println("性别:" + sex); } } public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.print(); System.out.println("分界线-------------------------------"); //创建Dog对象,带三个参数的构造方法 Dog dog02 = new Dog("中华田园犬","黄","阿黄"); dog02.print(); System.out.println("分界线-------------------------------"); //创建Dog对象,对所有属性赋值的构造方法 Dog dog03 = new Dog("泰迪","棕色","小迪",5,"公"); dog03.print(); } }

输出结果为:


6、构造方法的重载

构造方法分类:

       隐式构造方法:当一个类中,没有手动编写的构造方法,则系统会提供一个默认的无参的构造方法

       显示构造方法:当在一个类中,手动编写构造方法,则系统回话提供默认的无参的构造方法

                                当手动编写构造方法时,建议先编写无参构造方法,然后再编写需要的构造方法

构造方法重载:

       在同一个类中,构造方法的名字必须相同,参数列表不同(个数不同、类型不同、顺序不同)

//无参构造方法 public Dog() { //完成对品种、颜色、姓名、年龄、性别 strain = "哈士奇";//品种 color = "黑白";//颜色 name = "二哈";//姓名 age = 5;//年龄 sex = "公";//性别 } //带参构造方法 public Dog(String strain,String color,String name) { //完成对品种、颜色、姓名、年龄、性别的赋值 //局部变量的值赋值给成员变量 this.strain = strain;//品种 this.color = color;//颜色 this.name = name;//姓名 } //创建对所有属性赋值的构造方法 public Dog(String strain,String color,String name,int age,String sex) { //局部变量的值赋值给成员变量 this.strain = strain;//品种 this.color = color;//颜色 this.name = name;//姓名 this.age = age;//年龄 this.sex = sex;//性别 }

 

 

 

 

 

 

最新回复(0)