类和对象(中)

简介: 类和对象(中)

8580f621118b49028cbb7df2ed059eb8.jpeg

🍏 2、类和对象的说明

🍌1. 类只是一个模型一样的东西,用来对一个实体进行描述,限定了类有哪些成员。

🍇2. 类是一种自定义的类型,可以用来定义变量。

🍒3. 一个类可以实例化出多个对象,实例化出的对象 占用实际的物理空间,存储类成员变量。

四、this引用

🍑定义:this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。

实例1:

   public class Classlook {

       public int year;

       public int month;

       public int day;

       public void Date(int year,int month,int day){

           year = year;

           month = month;

           day = day;

       }

       public void printDate(){

           System.out.println(year+"-"+month+"-"+day);

   

       }

       public static void main(String[] args) {

           Classlook s= new Classlook();

           s.printDate();

       }

   }

运行结果:

d6f9d7bbe4374048a7bea6da77179677.png

d6f9d7bbe4374048a7bea6da77179677.png

🍶🍶代码解读:为什么这里我们输出的结果都是0呢?我们看下面一段代码,这里是因为:局部变量优先,只是形参自己给自己赋值了。根本没有赋值到成员变量当中。

    public void Date(int year,int month,int day){

           year = year;

           month = month;

           day = day;

       }

我们来看this引用的用法:

   public class Date {

     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 printDate(){

       System.out.println(this.year + "/" + this.month + "/" + this.day);

    }

   }

🍥🍥注意:this引用的是调用成员方法的对象。

   public static void main(String[] args) {

     Date d = new Date();

     d.setDay(2022,8,4);

     d.printDate();

   }

运行结果:

cfdc308c1fed401c89dee2a3d4e4aaff.png

   🍛🍛结论:

   🌮1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型 。

   🌯2. this只能在"成员方法"中使用。

   🍖3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象。

   🍞4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法。

   🥛5.对象的引用传递给该成员方法,this负责来接收。

五、对象的构造及初始化  

🍌一、概念理解

我们知道,在Java方法内部定义一个局部变量时,必须要初始化,否则会编译失败。

92fb4102314347b7b02aeb109eb20497.png 🍼🍼如何解决呢?非常简单,我们只需要在使用a之前给a赋一个初始值便可。

🍠🍠如果是对象的话,则代码可以正常通过编译。

二、构造方法

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

实例:

   public class Classlook {

       public int year;

       public int month;

       public int day;

   

       public Classlook(int year,int month,int day){

           this.year = year;

           this.month = month;

          this.day = day;

           System.out.println("Classlook()方法被调用了");

       }

       public void printDate(){

           System.out.println(year+"-"+month+"-"+day);

       }

       public static void main(String[] args) {

   // 此处创建了一个Date类型的对象,并没有显式调用构造方法

           Classlook s= new Classlook(2022,8,04);

           s.printDate();

       }

   }

   🍶🍶解读:

   🍌一般情况下使用public修饰。

   🍐名字与类名相同,没有返回值类型,设置为void也不行。

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

   🍎构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。

运行结果:

8bf8844c09bd43c99305ae3fae27473e.png

8bf8844c09bd43c99305ae3fae27473e.png

🍋🍋构造方法可以重载。

   //无参构造方法

   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 class Date {

     public int year;

     public int month;

     public int day;

     public void printDate(){

       System.out.println(year + "-" + month + "-" + day);

    }

✈️✈️上述Date类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法。

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

   public Date(int year, int month, int day) {

       this.year = year;

       this.month = month;

       this.day = day;

    }

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

   public class Date {

     public int year;

     public int month;

     public int day;

   

     public Date(){

       this(2022, 8, 4);

       System.out.println(year);  

       this.year = 2022;

       this.month = 8;

       this.day = 4;

    }

🚅🚅注:this(2022,8,4);必须是构造方法中第一条语句,不然编译器会报错。

🚤🚤还有就是   " 不能形成环"。

   public Date(){

    this(1900,1,1);

   }

   public Date(int year, int month, int day) {

    this();

   }

🍎无参构造器调用三个参数的构造器,而三个参数构造器有调用无参的构造器,形成构造器的递归调用。

三、默认初始化

🍏上面提及,为什么局部变量在使用时必须要初始化,而成员变量可以不用呢?

   public class Date {

     public int year;

     public int month;

     public int day;

     public Date(int year, int month, int day) {

       // 成员变量在定义时,并没有给初始值, 为什么就可以使用呢?

       System.out.println(this.year);

       System.out.println(this.month);

       System.out.println(this.day);

    }

   

     public static void main(String[] args) {

       // 此处a没有初始化,编译时报错:

       // Error:(24, 28) java: 可能尚未初始化变量a

       // int a;

       // System.out.println(a);

       Date d = new Date(2022,8,4);

    }

   }

🍐这里博主不作过多的讲解,因为现在的知识点还不够,想了解的可以私下去查阅一些资料,这里就不在提了,只需知道成员变量不用初始化一样可以使用。

🍏对象空间被申请好之后,对象中包含的成员已经设置好了初始值:

数据类型 初始值

byte 0

char '\u0000'

short 0

int 0

long 0L

boolean false

float 0.0f

double 0.0

reference null

四、就地初始化

🍓定义:即在声明成员变量的同时就直接给出了初始值。

   public class Date {

     public int year = 2022;

     public int month = 8;

     public int day = 4;

     public Date(){

    }

     public Date(int year, int month, int day) {

    }

     public static void main(String[] args) {

       Date d1 = new Date(2022,8,5);

       Date d2 = new Date();

    }

   }

   🎉🎉后续关于封装,static成员,代码块,内部类,对象的打印知识点将在下篇博客更新,记得及时关注喔!⛵⛵


相关文章
|
1月前
|
编译器 C语言 C++
c++类和对象
c++类和对象
20 1
|
6月前
|
存储 程序员 数据安全/隐私保护
C++ 类和对象(一)
C++ 类和对象(一)
28 0
|
1月前
|
存储 编译器 C语言
【C++】类和对象(一)
【C++】类和对象(一)
|
1月前
|
存储 编译器 C语言
C++-类和对象(2)
C++-类和对象(2)
27 0
|
1月前
|
存储 编译器 C语言
【c++】类和对象1
【c++】类和对象1
22 1
|
1月前
|
编译器 C++
【c++】类和对象2
【c++】类和对象2
15 1
|
8月前
|
存储 编译器 程序员
类和对象的介绍一
类和对象的介绍一
57 0
|
9月前
|
C#
C#——类和对象
C#——类和对象
53 0
|
4月前
|
Java 编译器
类和对象!
类和对象!
29 1
|
4月前
|
Java 编译器
类和对象(一)
类和对象(一)
46 0