• JAVASE语法零基础——this引用和构造方法


    Java系列文章目录


    在这里插入图片描述

    Write once,Runanywhere.🔥🔥🔥

    💥 💥 💥如果你觉得我的文章有帮助到你,还请【关注➕点赞➕收藏】,得到你们支持就是我最大的动力!!!
    💥 💥 💥

    版权声明:本文由【马上回来了】原创、在CSDN首发、需要转载请联系博主。
    版权声明:本文为CSDN博主「马上回来了」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

    在这里插入图片描述

    🚀🚀🚀 新的知识开始喽🚀🚀🚀
    在这里插入图片描述



    1.this引用

    概念:this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中成员变量都是同该引用去访问的。
    先看下面的代码,如果不用this引用会带来哪些问题:

    class Data{//一个时间类
        //成员变量
        public int year;
        public int month;
        public int day;
        //成员方法
        public void setDay(int year,int month,int day){
            year = year;
            month = month;
            day = day;
        }
        public void printDay(){
            System.out.println(year+"/"+month+"/"+day);
        }
    }
    public class Test {
        public static void main(String[] args) {
            Data data1 = new Data();
            data1.setDay(2022,6,22);
            data1.printDay();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述
    运行结果:发现并没有给成员变量赋值,而是打印出成员变量的初始值。
    在这里插入图片描述
    这是因为根据局部变量优先的原则,setDay方法里的左边的成员变量会被当做是局部变量,即:
    在这里插入图片描述
    this引用:

    class Data{//一个时间类
        //成员变量
        public int year;
        public int month;
        public int day;
        //成员方法
        public void setDay(int year,int month,int day){
            this.year = year;
            this.month = month;
            this.day = day;
        }
        public void printDay(){
            System.out.println(year+"/"+month+"/"+day);
        }
    }
    public class Test {
        public static void main(String[] args) {
            Data data1 = new Data();
            data1.setDay(2022,6,22);
            data1.printDay();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述
    运行结果:
    在这里插入图片描述

    2.构造方法

    构造方法是一种特殊的成员方法,与普通的成员方法相比较:

    1.方法名与类名相同
    2.没有返回值
    3.在对象创建时,编译器自动调用且在该对象的生命周期里只调用一次
    4.一般有public修饰

    上面那个Data类的代码,每次在创建对象后都需要我们去手动调用setDay方法才能设置出我们要的时间日期,如果我们要实例化出多个对象就用多次手动调用这个方法。比较繁琐,因此我们通过构造方法来实现简化操作。
    看代码:

    class Data{
        //成员变量
        public int year;
        public int month;
        public int day;
        //构造方法
        public Data(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    
        //成员方法
        public void printDay(){
            System.out.println(year+"/"+month+"/"+day);
        }
    }
    public class Test {
        public static void main(String[] args) {
            Data date1 = new Data(2022,6,22);
            date1.printDay();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述
    运行结果:
    在这里插入图片描述
    注意在实例化对象时,是必须得调用构造函数的,否者会编译失败的,上文讲解this引用那里Data没有写构造方法是因为,当程序员没有写构造方法时,编译器会自动提供一个没有参数的构造方法。
    但是如果程序员自己写了构造方法,那么编译器就不会自动提供构造方法了。
    再看一遍上面的代码:

    class Data{//一个时间类
        //成员变量
        public int year;
        public int month;
        public int day;
        //构造方法
        public Data() {
            System.out.println("该方法被调用了");
        }
    
        //成员方法
        public void setDay(int year,int month,int day){
            this.year = year;
            this.month = month;
            this.day = day;
        }
        public void printDay(){
            System.out.println(year+"/"+month+"/"+day);
        }
    }
    public class Test {
        public static void main(String[] args) {
            Data data1 = new Data();
            data1.setDay(2022,6,22);
            data1.printDay();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述
    运行结果:
    在这里插入图片描述

    3.方法重载

    方法重载三个条件:
    1.方法名相同
    2.参数值不同:指参数的类型、个数、顺序不同
    3.返回值不做要求
    构造方法可以重载

       //构造方法
        //带三个参数的构造方法
        public Data(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    //带两个参数的构造方法
        public Data(int year, int month) {
            this.year = year;
            this.month = month;
        }
    
        //带一个参数的构造方法
        public Data(int year) {
            this.year = year;
        }
        //不带参数的构造方法
        public Data() {
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    注意:编译器在重载构造方法时,只能识别参数的种类,不能识别参数的参数名
    在这里插入图片描述
    在这里插入图片描述

    虽然构造方法可以重载,但是如果在实例化对象时没有合适构造方法,编译也会出错的:

    class Data{//一个时间类
        //成员变量
        public int year;
        public int month;
        public int day;
        //构造方法
        public Data() {
            System.out.println("该方法被调用了");
        }
    
        //成员方法
        public void setDay(int year,int month,int day){
            this.year = year;
            this.month = month;
            this.day = day;
        }
        public void printDay(){
            System.out.println(year+"/"+month+"/"+day);
        }
    }
    public class Test {
        public static void main(String[] args) {
            Data data1 = new Data(2022,6,22);
            //data1.setDay(2022,6,22);
            data1.printDay();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    运行结果:
    在这里插入图片描述

    this(参数)调用别的合适的构造方法:
    1.this(参数)必须位于构造方法的第一行
    2.不能闭合成环
    看代码:

    class Data{//一个时间类
        //成员变量
        public int year;
        public int month;
        public int day;
        //构造方法
        public Data() {
            //System.out.println("该方法被调用了"); 这里代码不放放开注释,因为this(参数)必须在代码第一行
            this(2022,6,22);
        }
    
        public Data(int year, int month, int day) {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    
        //成员方法
        public void setDay(int year,int month,int day){
            this.year = year;
            this.month = month;
            this.day = day;
        }
        public void printDay(){
            System.out.println(year+"/"+month+"/"+day);
        }
    }
    public class Test {
        public static void main(String[] args) {
            Data data1 = new Data();
            //data1.setDay(2022,6,22);
            data1.printDay();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述
    运行结果:
    在这里插入图片描述
    在这里插入图片描述
    总结this引用的三个方法:
    1.this.成员变量:成员方法内访问成员变量
    2.this.成员方法:成员方法内访问其他的成员方法(也可以不用this,直接使用方法名)
    3.this(参数列表):构造方法内调用与参数列表相同的构造方法

    🌏🌏🌏今天的你看懂这里又学到了很多东西吧🌏🌏🌏

    在这里插入图片描述

    🌔 🌔 🌔下次见喽🌔 🌔 🌔
    在这里插入图片描述

  • 相关阅读:
    VsCode中搭建TypeScript调试环境
    【QT】回调函数的实现
    Nginx+Tomcat 搭建负载均衡、动静分离
    【AI应用】NVIDIA GeForce RTX 3060的详情参数
    阿里P8大能倾力编撰的“Java 进阶面试手册”,助力跳槽外包毕业生秋招收获大厂offer
    应用程序转换工具Unite mac中文版软件特点
    flink选择slot
    20231109-【尚硅谷】3小时Ajax入门到精通学习笔记
    网关信息查看
    【网络安全---XSS漏洞(1)】XSS漏洞原理,产生原因,以及XSS漏洞的分类。附带案例和payload让你快速学习XSS漏洞
  • 原文地址:https://blog.csdn.net/m0_62160964/article/details/125410585