JAVA系列 小白入门参考资料 类和对象(2)

目录

小引 

1. this

this引用

this的特性

this的使用 

2. 对象的构造及初始化

如何初始化对象

构造方法

利用构造方法初始化对象的三种方式

方式一

方式二

方式三

直接初始化 


小引 

上一篇文章我们讨论到 如果创立类和对象,并且如何实例化对象和怎样去调用成员方法和成员变量。JAVA系列 小白入门参考资料 类和对象(1)

下面我们再写一些代码来回顾一下

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

    public void setDay(int y,int m,int d){
        year = y;
        month = m;
        day = d;
    }

    public  void printDay(){
        System.out.println(year + "年" + month + "月" + day + "日");
    }
}


public class blog {
    public static void main(String[] args) {
        Date date = new Date();

        //调用方法 且传参进行初始化
        date.setDay(2024,4,29);
        date.printDay();
        System.out.println("===========================");

        //也可以使用成员变量直接赋值
//        date.year = 2024;
//        date.month = 4;
//        date.day = 29;
//        date.printDay();

        
        Date date1 = new Date();
        date1.setDay(2023,3,30);
        date1.printDay();

    }
}

到这里我们就不禁有些疑问,我们有两个对象,但只有一个方法。

我们继续向下看 

class Date{
    int year;
    int month;
    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 blog {
    public static void main(String[] args) {
        //调用方法 且传参进行初始化
        Date date = new Date();
        date.setDay(2024,4,29);
        date.printDay();
        System.out.println("===========================");

        //也可以使用成员变量直接赋值
//        date.year = 2024;
//        date.month = 4;
//        date.day = 29;
//        date.printDay();


        Date date1 = new Date();
        date1.setDay(2023,3,30);
        date1.printDay();
    }
}

当我把代码变成这种形式时

public void setDay(int year,int month,int day){
    year = year;
    month = month;
    day = day;
}

好像计算机也“蒙圈”了

这时我们就需要其他方法来帮助我们,那就是this关键字 


1. this

this引用

this 引用指向当前对象 ( 成员方法运行时调用该成员方法的对象 ) ,在成员方法中所有成员变量的操作,都是通过该 引用去访问
 public void setDay(int year,int month,int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }

 当我们把关键代码修改成上面时,结果是什么样呢?

 我们可以看到代码又可以正常运行了

通俗的来说,哪个 对象引用  调用this,这个this就是其对象引用(简称为“替身”)。

this的特性

1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
2. this只能在"成员方法"中使用
3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象
4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法 对象的引用传递给该成员方法,this负责来接收 

this的使用 

this. 成员变量 

this. 成员方法  

this() 

this()调用的是类中的构造方法,这里先提一下,下面讲构造方法时会解释 


2. 对象的构造及初始化

如何初始化对象

在之前的学习中我们知道了,在Java方法内部中定义一个局部变量的时候,一定要初始化。否则会报错 

public class NewTest {
    public static void main(String[] args) {
        int a;
        System.out.println(a);
    }
}

我们只要把关键代码改为  int a = 10; 就可以正常运行了!

学到这里我们可能就有些疑问?

1. 在之前的代码中成员变量也没有初始化,但为什么没有报错?

class Date{
    //成员变量
    int year;
    int month;
    int day;
}

因为程序会自动默认给成员变量初始化,引用数据类型的初始值 是 NULL,基本数据类型的初始值是0 

class Dog{
    String name;
    int age;
    String color;

    public Dog(){
    }

    public void print(){
        System.out.println(this.name + "  " + this.age + "  " + this.color);
    }
}

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

2.  无论我们是调用方法还是给成员变量赋值,都要去自己重新传一次参或按相应的成员变量一个一个去赋值,好像有点麻烦,有没有什么办法可以直接一次就把成员变量给赋值?

public class blog {
    public static void main(String[] args) {
        //调用方法 且传参进行初始化
        Date date = new Date();
        date.setDay(2024,4,29);
        date.printDay();
        System.out.println("===========================");

        //也可以使用成员变量直接赋值
//        date.year = 2024;
//        date.month = 4;
//        date.day = 29;
//        date.printDay();


        Date date1 = new Date();
        date1.setDay(2023,3,30);
        date1.printDay();
    }
}

答案是有的,Ta就是构造方法 

构造方法

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

语法结构:

访问修饰符  类名 (形参列表){

}

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

    //构造方法
    public Date(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 blog {
    public static void main(String[] args) {
        //调用方法 且传参进行初始化
        Date date = new Date(2024,4,29);
        date.printDay();

        System.out.println("===========================");

        Date date1 = new Date(2023,3,30);
        date1.printDay();
    }
}

这里我们利用的是构造方法来初始化,而前面的代码我们是利用 setDay方法 来初始化的 

构造方法特性:

1. 名字必须与类名相同
2. 没有返回值类型,设置为void也不行
3. 创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次(相当于人的出生,每个人只能出生一次)
4. 构造方法可以重载(用户根据自己的需求提供不同参数的构造方法) 

下面的代码中 构造方法就实现了方法的重载 

class Date{
    //成员属性
    int year;
    int month;
    int day;
    
    //构造方法
    public Date(int year) {
        this.year = year;
    }

    //构造方法的重载
    public Date(int year, int month) {
        this.year = year;
        this.month = month;
    }

    //构造方法的重载
    public Date(int yeat, int month, int day){
        this.year = year;
        this.month = month;
        this.day = day;
    }

}

                  上述三个构造方法:名字相同,参数列表不同,因此构成了方法重载。 

5. 如果用户没有显式定义,编译器会生成一份默认的构造方法,生成的默认构造方法一定是无参的。

注意:一旦用户定义,编译器则不再生成。 

 6. 构造方法中,可以通过this调用其他构造方法来简化代码 

利用构造方法初始化对象的三种方式

方式一

使用无参构造方法,利用this去访问成员变量,并进行赋值 

class Dog{
    String name;
    int age;
    String color;

   //方式一
    public Dog(){
        this.name = "小飞";
        this.age = 10;
        this.color = "粉色";
    }

    public void print(){
        System.out.println(this.name + "  " + this.age + "  " + this.color);
    }
}

public class NewTest {
    public static void main(String[] args) {
        //方式一
        Dog dog = new Dog();
        dog.print();
    }
}

方式二

使用含参数的构造方法,并在创建对象时进行传参,初始化

class Dog{
    String name;
    int age;
    String color;

    //方式二
    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public void print(){
        System.out.println(this.name + "  " + this.age + "  " + this.color);
    }
}

public class NewTest {
    public static void main(String[] args) {
       //方式二
        Dog dog1 = new Dog("小飞飞",10,"浅粉色");
        dog1.print();
    }
}

方式三

先通过 new对象() 调用无参构造方法,再通过  无参构造方法  中的  this(...)语句把其中的参数传给  带参数的构造方法 ,再进行初始化 

这里就是前面提到 this() 的知识

this() 就是调用无参的构造方法

this(...) 就是调用对应参数的构造方法

class Dog{
    String name;
    int age;
    String color;

    //方式三
    public Dog(){
        this("小飞飞飞",10,"浅浅粉色");
    }


    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }
    

    public void print(){
        System.out.println(this.name + "  " + this.age + "  " + this.color);
    }
}

public class NewTest {
    public static void main(String[] args) {
        
        //方式三
        Dog dog = new Dog();
        dog.print();

    }
}

直接初始化

在声明成员变量时,就直接给出了初始值。
class Dog{
    String name = "小狗狗";
    int age = 10;
    String color = "米白色";
}
  • 42
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值