目录一⾯向对象的初步认知1.什么是⾯向对象二类定义和使用1.简单认识类2.类的定义格式3.课堂练习三类的实例化1.什么是实例化2.如何访问对象当中的成员3.类和对象的再次理解四this关键字1.为什么要有this引⽤2.this是什么3.this引⽤的特性五对象的构造及初始化1.如何初始化对象中的成员变量2.默认初始化3.就地初始化4.构造⽅法初始化六对象的打印内容大纲本文介绍了Java面向对象编程的基础知识。1.首先解释了面向对象的概念强调Java作为纯面向对象语言的特点。2.然后详细讲解了类的定义和使用方法包括成员变量和成员方法的声明并通过洗衣机、宠物狗和学生等实例进行说明。3.接着阐述了类的实例化过程使用new关键字创建对象及访问对象成员的方式。重点讲解了this关键字的作用和使用场景解决了成员变量与形参命名冲突的问题。4.最后介绍了对象的初始化方法包括默认初始化、就地初始化和构造方法初始化并说明了如何通过重写toString方法实现对象属性的打印。全文系统性地介绍了Java面向对象编程的核心概念和基本操作。一⾯向对象的初步认知1.什么是⾯向对象Java是⼀⻔纯⾯向对象的语⾔(ObjectOrientedProgram简称OOP)在⾯向对象的世界⾥⼀切皆 为对象。⾯向对象是解决问题的⼀种思想主要依靠对象之间的交互完成⼀件事情。⽤⾯向对象的思 想来设计程序更符合⼈们对事物的认知对于⼤型程序的设计、扩展以及维护都⾮常友好。二类定义和使用1.简单认识类类是⽤来对⼀个实体(对象)来进⾏描述的主要描述该实体(对象)具有哪些属性(外观尺⼨等)哪些功 能(⽤来⼲啥)描述完成后计算机就可以识别了。⽐如洗⾐机它是⼀个品牌在Java中可以将其看成是⼀个类别。属性产品品牌型号产品重量外观尺⼨颜⾊...功能洗⾐烘⼲、定时....在Java语⾔中如何对上述的洗⾐机类来进⾏定义呢2.类的定义格式在Java中定义类时需要⽤到class关键字具体语法如下创建类 class ClassName { field; //字段(属性)或者成员变量method; method; //⾏为 或者成员⽅法 }class为定义类的关键字ClassName为类的名字{ }中为类的主体。类中包含的内容称为类的成员。属性主要是⽤来描述类的称之为类的成员属性或者类成员变量。⽅ 法主要说明类具有哪些功能称为类的成员⽅法。Class WashMechin(){ //成员变量 public String brand; //品牌 public String type; //型号 public double length; //⻓ public double width; //宽 public double height; // ⾼ public String color; //颜⾊ //成员方法 public void washClothes(){ System.out.println(洗⾐功能 ); } public void dryClothes(){ System.out.println(脱水功能); } public void setTime(){ System.out.println(定时功能); } }采⽤Java语⾔将洗⾐机类在计算机中定义完成经过javac编译之后形成.class⽂件在JVM的基础上 计算机就可以识别了。注意事项类名建议采⽤⼤驼峰定义成员前写法统⼀为public后⾯会详细解释此处写的⽅法不带static关键字.后⾯会详细解释3.课堂练习定义⼀个Dog类⼤家可以思考⼀下哪些是Dog的属性哪些是Dog的⾏为参考代码如下class PetDog{ //成员变量 public String name; //名字 public String color; //颜色 //狗的属性成员方法 public void barks(){ System.out.println(name汪汪往...); } //狗的行为成员方法 public void wag(){ System.out.println(name摇尾巴...); } }定义⼀个学⽣类Student⼤家可以思考⼀下哪些是Student的属性哪些是Student的⾏为参考代码如下//定义了一个学生 class Student{ //成员变量 public String name; public String sex; public double high; public double weight; public int score; //成员方法 public void DoClass(){ } public void DoHomework(){ } public void Exam(){ } }注意事项⼀般⼀个⽂件当中只定义⼀个类public修饰的类必须要和⽂件名相同不要轻易去修改public修饰的类的名称如果要修改通过开发⼯具修改三类的实例化1.什么是实例化定义了⼀个类就相当于在计算机中定义了⼀种新的类型与intdouble类似只不过int和double 是java语⾔⾃带的内置类型⽽类是⽤⼾⾃定义了⼀个新的类型⽐如上述的PetDog类和Student 类。它们都是类(⼀种新定义的类型)有了这些⾃定义的类型之后就可以使⽤这些类来定义实例(或者 称为对象)。⽤类类型创建对象的过程称为类的实例化在java中采⽤new关键字配合类名来实例化对象。public class Test { //类的实例化 public static void main(String[] args) { PetDog dog new PetDog();//通过new实例化对象 PetDog dogs new PetDog(); } }2.如何访问对象当中的成员public static void main(String[] args) { PetDog dog1 new PetDog();//通过new实例化对象 //成员的访问--》访问成员变量 dog1.name大黄; dog1.color黄色; //访问成员方法 dog1.wag(); dog1.barks(); //通过new关键字实例化对象 PetDog dog2 new PetDog(); //访问成员变量 dog2.color 黑色; dog2.name 小黑; //访问成员方法 dog2.barks(); dog2.wag(); }输出注意事项new关键字⽤于创建⼀个对象的实例使⽤.点 号来访问对象中的属性和⽅法同⼀个类可以创建多个实例3.类和对象的再次理解1. 类只是⼀个模型⼀样的东西⽤来对⼀个实体进⾏描述限定了类有哪些成员.2. 类是⼀种⾃定义的类型可以⽤来定义变量.3.⼀个类可以实例化出多个对象实例化出的对象 占⽤实际的物理空间存储类成员变量4. 做个⽐⽅。类实例化出对象就像现实中使⽤建筑设计图建造出房⼦类就像是设计图只设计出需 要什么东西但是并没有实体的建筑存在同样类也只是⼀个设计实例化出的对象才能实际存储 数据占⽤物理空间四this关键字1.为什么要有this引⽤如下代码定义了⼀个Date类Date类中包含3个属性分别是yearmonthday。分别使⽤setDay和 printDate对进⾏设置和打印⽇期。/定义一个Data打印和涉设置日期 class Data{ //成员变量 public int year; public int month; public 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 static void main(String[] args) { //构造三个⽇期类型的对象 d1 d2 d3 Data d1 new Data(); Data d2 new Data(); Data d3 new Data(); //对 d1d2d3 的⽇期设置 d1.SetDay(2026,5,20); d2.SetDay(2026,5,21); d3.SetDay(2026,5,22); //打印日期内容 d1.PrintDay(); d2.PrintDay(); d3.PrintDay(); } }输出提出2个疑问1.形参名不⼩⼼与成员变量名相同会发⽣什么答案会出问题.....public void setDay(int year, int month, int day){ year year; month month; day day; }输出2. 三个对象都在调⽤setDate和printDate函数但是这两个函数中没有任何有关对象的说明 setDate和printDate函数如何知道打印的是那个对象的数据呢2.this是什么this引⽤指向当前对象(成员⽅法运⾏时调⽤该成员⽅法的对象)在成员⽅法中所有成员变量的操作都是通过该引⽤去访问。只不过所有的操作对⽤⼾是透明的即⽤⼾不需要来传递编译器⾃动完成。class Data{ //成员变量 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 PrintDay(){ System.out.println(year/month/day); } public static void main(String[] args) { //构造三个⽇期类型的对象 d1 d2 d3 Data d1 new Data(); //对 d1d2d3 的⽇期设置 d1.SetDay(2026,5,20); //打印日期内容 d1.PrintDay(); } }输出此时通过指定this就能正确解决上述的2个问题了。这⾥之所以可以起作⽤关键点在于this代表的是当前对象的引⽤当前对象就是指哪个对象调⽤setDay⽅法谁就是this引⽤3.this引⽤的特性1.this的类型对应类类型引⽤即哪个对象调⽤就是哪个对象的引⽤类型2.this只能在成员⽅法中使⽤3.在成员⽅法中this只能引⽤当前对象不能再引⽤其他对象4.this是“成员⽅法”第⼀个隐藏的参数编译器会⾃动传递在成员⽅法执⾏时编译器会负责将 调⽤成员⽅法对象的引⽤传递给该成员⽅法this负责来接收5.this的使⽤总结this. 成员变量 // 访问对象的成员变量this. 成员⽅法 // 访问对象的成员⽅法事实上我们也可以通过this来访问构造⽅法那么什么是构造⽅法呢五对象的构造及初始化1.如何初始化对象中的成员变量通过前⾯知识点的学习知道在Java⽅法内部定义⼀个局部变量时必须要初始化否则会编译失 败。public static void main(String[] args) {int a;System.out.println(a);}要让上述代码通过编译⾮常简单只需在正式使⽤a之前给a设置⼀个初始值即可。如果是对象public static void main(String[] args) {Date d new Date();d.printDate();d.setDate(2021,6,9);d.printDate();}// 代码可以正常通过编译需要调⽤之前写的SetDate⽅法才可以将具体的⽇期设置到对象中。通过上述例⼦发现两个问题每次对象创建好后调⽤SetDate⽅法设置具体⽇期⽐较⿇烦那对象该如何初始化局部变量必须要初始化才能使⽤为什么字段声明之后没有给值依然可以使⽤2.默认初始化public class Date { public int year; public int month; public int day; public static void main(String[] args) { Date d new Date(); System.out.println(d.year); System.out.println(d.month); System.out.println(d.day); } }输出对于成员变量来说如果没有进⾏初始化会有⼀个对应的默认值默认值遵循如下规则3.就地初始化在声明成员变量时就直接给出了初始值。class Date { public int year 1900; public int month 1; public int day 1; public void printDate(){ System.out.println(year/month/day); } public static void main(String[] args) { Date d1 new Date(); d1.printDate(); } }输出4.构造⽅法初始化构造⽅法概念构造⽅法(也称为构造器)是⼀个特殊的成员⽅法名字必须与类名相同在创建对象时由编译器⾃动调⽤并且在整个对象的⽣命周期内只调⽤⼀次。//定义一个Data打印和涉设置日期 class Data{ //成员变量 public int year; public int month; public int day; //构造方法 public Data(int year,int month,int day){ this.year year; this.month month; this.day day; System.out.println(Data(int,int,int)⽅法被调⽤了 ); } //打印日期 public void PrintData(){ System.out.println(year/month/day); } public static void main(String[] args) { Data d1 new Data(1998,5,20); d1.PrintData(); } }输出构造⽅法注意事项1.名字必须与类名相同2.没有返回值类型设置为void也不⾏3.创建对象时由编译器⾃动调⽤并且在对象的⽣命周期内只调⽤⼀次(相当于⼈的出⽣每个⼈只能 出⽣⼀次)4.构造⽅法可以重载(⽤⼾根据⾃⼰的需求提供不同参数的构造⽅法)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 d new Date(); d.printDate(); }输出;上述两个构造⽅法名字相同参数列表不同因此构成了⽅法重载。5.如果⽤⼾没有显式定义编译器会⽣成⼀份默认的构造⽅法⽣成的默认构造⽅法⼀定是⽆参的。public class Date { public int year; public int month; public int day; public void printDate(){ System.out.println(year - month - day); } public static void main(String[] args) { Date d new Date(); d.printDate(); } }上述Date类中没有定义任何构造⽅法编译器会默认⽣成⼀个不带参数的构造⽅法。6. ⼀旦⽤⼾定义了其他的构造⽅法编译器则不再⽣成。//成员变量 public class Date { public int year; public int month; public int day; //构造方法 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 d new Date(); d.printDate(); } }输出上述代码运⾏之后会显⽰没有⽆参构造⽅法。7.构造⽅法中可以通过this调⽤其他构造⽅法来简化代码public class Date { public int year; public int month; public int day; //构造方法 public Date(){ this(1900, 1, 1); } public void printDate(){ System.out.println(year - month - day); } // 带有三个参数的构造⽅法 public Date(int year, int month, int day) { this.year year; this.month month; this.day day; } }输出1.this(...)必须是构造⽅法中第⼀条语句2.不能形成环的调⽤public Date(){ this(1900,1,1); } public Date(int year, int month, int day) { this(); }8.绝⼤多数情况下使⽤public来修饰特殊场景下会被private修饰(后序讲单例模式时会遇到)六对象的打印如果我们直接打印对象的引⽤此时输出的结果为类路径名对象的hashcode值public class Person { String name; String gender; int age; public Person(String name, String gender, int age) { this.name name; this.gender gender; this.age age; } public static void main(String[] args) { Person person new Person(Jim,男, 18); System.out.println(person); } }输出如果想要默认打印对象中的属性该如何处理呢答案重写toString⽅法即可。class Person { String name; String gender; int age; //构造方法 public Person(String name, String gender, int age) { this.name name; this.gender gender; this.age age; } public String toString() { return [ name , gender , age ]; } public static void main(String[] args) { Person person new Person(Jim,男, 18); System.out.println(person); } }输出关于重写的理解我们会在继承多态章节讲到。以上就是我们的全部内容了