“构造方法”基础知识 (构造方法概念,构造方法的分类,构造方法的使用,构造方法的特性等等)

🚀文章导读

在本篇文章中,对构造方法进行了一些总结,可能也有不足的地方,如果有错误地方,还望在评论区指出,读完本篇文章,你需要理解以下几点:

1、什么是构造方法

2、构造方法分为哪几种?

3、this()语句的作用

4、掌握构造方法的特性

构造方法

构造方法概念:也称为构造器,名字必须与类名相同,在创建对象时,由编译器自动调用,并且在整个对象的生命周期内只调用一次

下面用代码为大家讲解:

构造方法的分类

非默认的静态方法

第一种:非默认的构造方法

public class Test {
    public int year;
    public int month;
    public int day;
    public Test() { //不带参数的构造方法
        System.out.println("不带参数的构造方法");
        System.out.println("year="+year+"\n"+"month="+month+"\n"+"day="+day);
    }
    public static void main(String[] args) {
        Test testPrint = new Test();//创建一个testPrint对象
    }
}

在这里插入图片描述

以上是我们自己所写出的构造方法,如果我们自己不写构造方法,java也会自己给我们设置默认的构造方法。

默认的构造方法

第二种:默认的构造方法

就算不写构造方法,代码也正常的运行了,所以证明了,java会自动创建默认的构造方法!
在这里插入图片描述

带参数的构造方法

第三种:带参数的构造方法

public class Test {
    public int year;
    public int month;
    public int day;
    //带有三个参数的构造方法
    public Test(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("year="+year+" "+"month="+month+" "+"day="+day);

    }
    public static void main(String[] args) {
        Test testPrint = new Test(2003,01,15);
    }
}

在这里插入图片描述

构造方法的特性:

1、名字必须与类名相同

2、在执行对象时,第一步会先执行构造方法

3、没有返回类型,也不能设置成void

4、创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次

在这里插入图片描述

5、构造方法可以重载

重载条件:

1、名字相同

2、参数列表不同

public class Test {
    public int year;
    public int month;
    public int day;
    //带有三个参数的构造方法
    public Test(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("year="+year+" "+"month="+month+" "+"day="+day);

    }
    //不带参数的构造方法
    public Test() {
        this.year = 1999;
        this.month = 5;
        this.day = 16;
    }
    public static void main(String[] args) {
        Test testPrint = new Test(2003,01,15);
    }
}

上述的两个构造方法,名字相同,参数列表不同,所以构成了重载,但是到底执行那个构造方法是根据在创建对象时有没有进行传参!传参了就执行有参数的构造方法,反之执行无参的构造方法!

6、如果用户没有明显的定义构造方法,编译器会默认生成一个不带参数的构造方法。但是一旦用户定义后,编译器不会再自动生成默认的构造方法。

7、通过this调用其他的构造方法简化代码

public class Test {
    public int year;
    public int month;
    public int day;
    //带有三个参数的构造方法
    public Test(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("year="+year+" "+"month="+month+" "+"day="+day);

    }
    //不带参数的构造方法
    public Test() {
        this(1999, 5, 15);
        System.out.println("不带参数的构造方法");
    }
    public static void main(String[] args) {
        Test testPrint = new Test();
    }
}

在这里插入图片描述

注意1、调用当前类当中的其他构造方法,只能在当前构造方法内部来使用并且this(……)语句只能是构造方法中的第一条语句

​ 2、不能形成环

public class Test {
    public int year;
    public int month;
    public int day;
    //带有三个参数的构造方法
    public Test(int year, int month, int day) {
        this();
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("year="+year+" "+"month="+month+" "+"day="+day);

    }
    //不带参数的构造方法
    public Test() {
        this(1999, 5, 15);
        System.out.println("不带参数的构造方法");
    }
    public static void main(String[] args) {
        Test testPrint = new Test();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值