类和对象(中)

简介: 类和对象(中)

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成员,代码块,内部类,对象的打印知识点将在下篇博客更新,记得及时关注喔!⛵⛵


相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16882 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1234 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2327 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3798 22
|
19天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。