✨hello,进来的小伙伴们呢,你们好呐!✨
🍊系列专栏:【JavaSE】☕
🍊本篇内容:JavaSE部分的类和对象的知识点上篇,包括面向对象思想的概念到类的构造及初始化等部分的内容。
🍎作者简介:一名大二即将升大三的科班编程小白,我很平凡,学会努力!
🍓码云存放仓库gitee:https://gitee.com/king-zhou-of-java/java-se.git
目录
⛴️三、类的实例化
🛳️四、this引用
🚈概念:Java是一门纯面向对象的语言(Object Oriented Program,继承OOP),在面向对象的世界里,一切皆为对象。面向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。
🍏1、面向过程
举个简单的例子:传统的手洗衣服的过程:拿盆子->放水->放衣服->放洗衣粉->手搓->换水->手搓->拧干。
🍏传统的方式:注重的是洗衣服的过程,少了一个环节可能都不行,按照该种方式来写代码,将来扩展或者维护起来会比较麻烦。
🍇2.面向对象
🍋实例:
假设这里总共对象有四个:人 洗衣粉 洗衣机 衣服。人只需要把衣服放进洗衣机里面,其他的至于洗衣机是如何把衣服洗干净以及如何拧干的,这都不是我们需要关心的问题,重点是整个过程是由这个4个对象的交互完成的!
🍔定义:类是用来对一个实体(对象)来进行描述的,主要描述该实体(对象)具有哪些属性(外观尺寸等),哪些功能(用来干啥)。
🍋定义格式:
- class ClassName{
- field; // 字段(属性) 或者 成员变量
- method; // 行为 或者 成员方法
- }
🍎练习:定义一个学生类
我们定义一个学生类的属性和方法:
- public class Student{
- public String name;
- public int age;
- public int num;
- public double score;
-
- public void DoClass(){
- System.out.println("做作业");
- }
- public void Exam(){
- System.out.println("考试");
- }
🍋定义:用类类型创建对象的过程,称为类的实例化,在java中采用new关键字,配合类名来实例化对象。
- public static void main(String[] args) {
- stu s= new stu();
- s.age =18;
- s.name = "张三";
- s.score = 99.8;
- s.num = 12345678;
- s.DoClass();
- s.Exam();
- System.out.println(s.age+" "+s.name+" "+ s.score+" "+s.num);
- }
🍈s就是我们实例化出来的对象,通过s.的方式可以给我们的学生类中的属性实例化,同样可以通过s.的方式调用我们的函数。
🍬🍬注意事项:
同一个类可以创建多个实例。
new 关键字用于创建一个对象的实例。
使用 . 来访问对象中的属性和方法。
🍏 2、类和对象的说明
🍌1. 类只是一个模型一样的东西,用来对一个实体进行描述,限定了类有哪些成员。
🍇2. 类是一种自定义的类型,可以用来定义变量。
🍒3. 一个类可以实例化出多个对象,实例化出的对象 占用实际的物理空间,存储类成员变量。
🍑定义: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();
- }
- }
运行结果:
🍶🍶代码解读:为什么这里我们输出的结果都是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();
- }
运行结果:
🍛🍛结论:
🌮1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型 。
🌯2. this只能在"成员方法"中使用。
🍖3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象。
🍞4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法。
🥛5.对象的引用传递给该成员方法,this负责来接收。
我们知道,在Java方法内部定义一个局部变量时,必须要初始化,否则会编译失败。
🍼🍼如何解决呢?非常简单,我们只需要在使用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也不行。
🍊在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次。
🍎构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。
运行结果:
🍋🍋构造方法可以重载。
- //无参构造方法
- 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成员,代码块,内部类,对象的打印知识点将在下篇博客更新,记得及时关注喔!⛵⛵