Java类和对象[②认识this引用和构造方法]

一:this引用

(1)为什么要有this引用

当不同的对象都调用同一个方法时,如果没有任何有关对象的说明,就不知道这个方法具体是要使用哪个对象,从而就会造成程序混乱!

(2)this的作用

this引用指向当前对象(谁调用此方法this就指向谁)

通过this引用去访问在成员方法中所有成员变量的操作,这样就可以让方法知道具体使用哪个对象

(3)this的特性

①this是属于对应类类型引用,即哪个对象调用有this的方法就是哪个对象的引用类型


②this在成员方法中使用(💗成员方法=普通方法+构造方法)


③在成员方法中,哪个对象调用方法this就只能引用当前对象,不可引用其他对象


④this引用必须在构造方法中是第一条语句

(4)代码理解

public class Date {
     public int year;
     public int month;
     public int day;

     public void setDay(int year, int month, int day){     //谁调用setDay方法this就指向谁
            this.year = year;
            this.month = month;
            this.day = day;
            System.out.println(this);    //看看d与this是否相同
     }

     public void printDate(){
            System.out.println(this.year + "/" + this.month + "/" + this.day);   //谁调用printDate方法this就指向谁
     }

    public static void main(String[] args) {
        Date d = new Date();      //实例化Date,创建一个引用变量d,通过d调用方法

        d.setDay(2020,9,15);   //d调用setDay,this就指向d,this就代表d,此时setDay方法就知道是d对象调用了它

        d.printDate();

        System.out.println(d);          //看看d与this是否相同
    }
}

很明显,d与this的地址相同,也正是说明了this指向d!!!

二:构造方法

(1)概念

构造方法(也称为构造器)是一个特殊的成员方法

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

相当于人的出生,每个人只出生一次

(2)特点

①构造方法的名字必须与类名相同

(一般用public修饰即可)


②构造方法没有返回值(void也不行)


③构造方法的作用是为了在构造对象的同时给对象中的成员初始化,并不负责开辟空间

当有了构造方法,就可以直接new对象后在里面赋值

(一般来说我们new一个对象就开辟一块内存空间,但构造方法不是)


④构造方法可以重载,且可以有多个构造方法,参数可有可无

public class Date {
    public int year;
    public int month;
    public int day;

    public Date(){          // 无参构造方法
        this.year = 1900;
        this.month = 1;
        this.day = 1;
    }

    public Date(int year, int month, int day) {     // 带有三个参数的构造方法
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public void printDate(){
        System.out.println(year + "-" + month + "-" + day);
    }

    public static void main(String[] args) {
        Date d1 = new Date();               //d1调用无参的构造方法
        Date d2 = new Date(2003,10,11);     //d2调用三个参数的构造方法
        d1.printDate();
        d2.printDate();
    }
}


⑤当没写构造方法时,编译器会默认帮你写,里面什么都没有,也没有参数,一旦你写了任何一种构造方法,此时编译器就不会再帮你写了


⑥快速创建构造方法:右键➜generate➜constructor➜点击需要的变量

(3)代码理解

public class Date {
    public int year;
    public int month;
    public int day;

    public Date(int year, int month, int day){     //构造方法(名字与类名一样,都是Date)
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("Date(int,int,int)方法被调用了");
    }

    public void printDate(){
        System.out.println(this.year + "-" + this.month + "-" +this.day);
    }

    public static void main(String[] args) {
        Date d = new Date(2021,6,9);    //new对象后直接赋值
        d.printDate(); // 2021-6-9
    }
}

三:this与构造方法总结

(1)this的三种使用方式

①this.变量属性

例如:this.name=name;


②this.()

利用this调用构造方法


③this.普通方法()

利用this调用普通方法

(2)this调用构造方法

①特点

1.this只能在构造方法里调用其他构造方法

💙(在普通方法无法调用构造方法)


2.this必须是构造方法中的第一条语句


3.不能形成环,也不能自己调用自己

(例如先在无参构造方法调用有参构造方法,再在有参构造方法调用无参构造方法)

②写法

this();       💚//有参数在括号输入对应数量的参数;无参数则不写即可

③代码
public class Student {
    //成员变量
    public int id;
    public String name;

    public Student(int id, String name){   //带两个参数的构造方法
        this.id=id;
        this.name=name;
    }
    public Student(){                   //无参的Student里调用带两个参数的构造方法Student
        this(1,"hlizoo");
    }
    public void printf_student(){
        System.out.println("id为"+this.id+"的学生名字叫"+this.name);
    }

    public static void main(String[] args) {
        Student student = new Student();    //我实例化的是无参的构造方法,但我在无参构造方法里调用了有参的,因此结果一样可以打印出来1和hlizoo
        student.printf_student();
    }
}

(3)this调用普通方法

①特点

this在普通方法和构造方法都可以调用普通方法

🖤(在普通方法不能调用构造方法)

②写法

this.普通方法();

③代码
public class Student {
    //成员变量
    public int id;
    public String name;

    public Student(int id, String name) {   //构造方法,方便初始化
        this.id = id;
        this.name = name;
    }

    public void dosomething(){
        this.print();   //用this调用普通方法print
    }
    public void print(){
        System.out.println(id+" "+name);
    }

    public static void main(String[] args) {
        Student student = new Student(77,"hlizoo");   //直接调用两个参数的构造方法初始化
        student.dosomething();     //dosomething普通方法里本来是没打印功能的,但是调用了print普通方法,就可以进行打印了
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值