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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值