对象是一种特殊的数据结构可以用来记住一个事物的数据从而代表该事务第一章 面向对象编程快速入门① 先设计对象的模板也就是对象的设计图类。② 通过new关键字每new一次类就得到一个新的对象packagecom.kgl.object;publicclassStar{Stringname;intage;doubleheight;doubleweight;}packagecom.kgl.object;publicclassTest{publicstaticvoidmain(String[]args){StarstarnewStar();star.name张三;star.age18;star.weight50.0;star.height178.0;Starstar1newStar();star1.name李四;star1.age18;star1.weight50.0;star1.height168.0;}}1.1 对象对象本质上是一种特殊的数据结构(可以理解为一张表)对象是用类new出来的有了类就可以创建出对象。publicclass类名{// 1、变量用来说明对象可以处理什么数据// 2、方法描述对象有什么功能也就是可以对数据进行什么样的处理...}语法格式类名 对象名 new 类名();万物皆对象谁的数据谁存储。第二章 构造器publicclassStudent{/** 构造器 */publicStudent(){...}}2.1 构造器构造器是一种特殊方法不能写返回值类型名称必须是类名。创建对象时对象会立即自动调用构造器执行StudentsnewStudent();2.2构造器的常见应用场景构造器常用于完成对象初始化创建对象时同时完成对对象成员变量(属性)的初始化赋值。2.3构造器的注意事项类默认就自带了一个无参构造器如果为类定义了有参数构造器类默认的无参数构造器就没有了此时如果还想用无参数构造器就必须自己手写一个无参数构造器出来。第三章 this 关键字是一个变量可以用在方法中来拿到当前对象。哪个对象调用这个方法this就拿到哪个对象。主要用来解决对象的成员变量与方法内部变量的名称一样时导致访问冲突问题publicclassStudent{Stringname;doublescore;publicvoidprint(){System.out.println(name);System.out.println(this);}publicvoidprintHobby(Stringname){System.out.println(this.name喜欢name);}publicvoidcheckPass(doublescore){if(this.scorescore){System.out.println(恭喜您考上哈佛走向巅峰~~~);}else{System.out.println(不好意思您没有考上~~~);}}}publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();s1.name张三;s1.print();System.out.println(s1);System.out.println(-----------------------------------);Students2newStudent();s2.print();System.out.println(s2);System.out.println(-----------------------------------);Students3newStudent();s3.name李四;s3.printHobby(读书);System.out.println(-----------------------------------);s3.score80;s3.checkPass(90);}}输出张三com.kgl.thisdemo.Student3b07d329com.kgl.thisdemo.Student3b07d329-----------------------------------nullcom.kgl.thisdemo.Student41629346com.kgl.thisdemo.Student41629346-----------------------------------李四喜欢读书-----------------------------------不好意思您没有考上~~~第四章 封装面向对象的三大特征:封装、继承、多态。定义:就是用类设计对象处理某一个事物的数据时,应该把要处理的数据,以及处理这些数据的方法,设计到一个对象中去。4.1 封装的设计规范合理隐藏:使用private关键字(私有,隐藏)修饰成员变量,就只能在本类中被直接访问,其他任何地方不能直接访问。合理暴露:使用public修饰(公开)的get和set方法合理暴露,成员变量的取值和赋值。4.2 代码层面控对象的成员公开或隐藏公开成员:可以使用public(公开)进行修饰。隐藏成员:使用private(私有,隐藏)进行修饰。publicclassStudent{Stringname;privateintage;privatedoublechinese;privatedoublemath;publicvoidsetAge(intage){if(age0||age100){System.out.println(输入的年龄有误);return;}this.ageage;}publicintgetAge(){returnage;}}publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();s1.setAge(-15);System.out.println(s1.getAge());}}第五章 实体类 Javabean5.1 定义是一种特殊类,类中要满足如下需求:要求 1:类中的成员变量全部私有,并提供public修饰的getter/setter方法。在IDEA中可以通过右键菜单-Generate-Getter and Setter快速生成。要求 2:提供get、set 方法,类中需要提供一个无参数构造器,有参数构造器可选。 在IDEA中可以通过右键菜单-Generate-constructor-select None/ok快速生成。5.2 实体类应用场景核实体类的对象只负责数据存取,而对数据的业务处理交给其他类的对象来完成,以实现数据和数据业务处理相分离。用来保存数据的java类,可以用它创建对象,保存某个事物的数据。// 实体类:Student (只负责存数据)publicclassStudent{privateStringname;privatedoublescore;// getter setter 方法...// 生成Getter/Setter:在IDEA中可以通过右键菜单-Generate-Getter and Setter快速生成。}// 业务类:StudentOperator (负责处理逻辑)publicclassStudentOperator{privateStudents;publicStudentOperator(Students){this.Students;}// 打印总成绩// 打印平均成绩// ...}5.3 实操5.3.1 定义实体类packagecom.kgl.Javabean;publicclassStudent{privateStringname;privatedoublechinese;privatedoublemath;publicvoidsetName(Stringname){this.namename;}publicvoidsetChinese(doublechinese){this.chinesechinese;}publicvoidsetMath(doublemath){this.mathmath;}publicStringgetName(){returnname;}publicdoublegetChinese(){returnchinese;}publicdoublegetMath(){returnmath;}publicStudent(){}publicStudent(Stringname,doublechinese,doublemath){this.namename;this.chinesechinese;this.mathmath;}}5.3.2 业务类packagecom.kgl.Javabean;publicclassStudebtOperator{privateStudents;publicStudebtOperator(Students){this.ss;}publicvoidprinttotalScore(){System.out.println(s.getName()的语文成绩是s.getChinese(),数学成绩是s.getMath());}publicvoidprintavgScore(){System.out.println(s.getName()的总成绩是(s.getChinese()s.getMath()),平均成绩是(s.getChinese()s.getMath())/2);}}5.3.3 调用packagecom.kgl.Javabean;publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();s1.setName(播妞);s1.setChinese(80.0);s1.setMath(90.0);System.out.println(s1.getName()的语文成绩是s1.getChinese(),数学成绩是s1.getMath());System.out.println(-------------------------------------------);Students2newStudent(播仔,90.0,80.0);StudebtOperatoroperatornewStudebtOperator(s2);operator.printtotalScore();operator.printavgScore();}}第六章 static关键字6.1 定义叫静态可以修饰成员变量、成员方法。成员变量按照有无static修饰分为两种:6.1.1 静态变量(类变量):有static修饰属于类在计算机里只有一份会被类的全部对象共享。6.1.2 实例变量(对象的变量):无static修饰属于每个对象的。publicclassStudent{// 静态变量staticStringname;// 实例变量(对象的变量)intage;}Student学生表中包含static name和age字段。s1对象和s2对象各自拥有独立的age值(均为0)但共享同一个name字段。推荐访问方式:类名.静态变量不推荐:对象.静态变量。实例变量只能通过 对象.实例变量 访问。6.2 static修饰成员变量 — 成员变量的执行原理publicclassStudent{// 类变量 静态变量:有static修饰属于类只加载一份可以被类和类的全部对象共享staticStringname;// 实例变量(对象变量) 没有static修饰属于对象每个对象都有一份intage;}publicclassTest{publicstaticvoidmain(String[]args){// 1、类变量的用法// 类名.静态变量(推荐)Student.name袁华;// 对象.静态变量(不推荐)Students1newStudent();s1.name马冬梅;Students2newStudent();s2.name秋雅;System.out.println(s1.name);// 秋雅System.out.println(Student.name);// 秋雅// 2、实例变量的用法// 对象.实例变量s1.age23;s2.age18;System.out.println(s1.age);// 23// System.out.println(Student.age); // 报错}}第七章 静态方法如果某个数据只需要一份且希望能够被共享访问、修改则该数据可以定义成静态变量来记住。同一个类中访问静态成员可以省略类名不写,在某个类中访问其他类里的类变量必须带类名访问7.1 static修饰方法成员方法的分类7.1.1 静态方法有 static 修饰的成员方法属于类。可以直接用类名访问也可以用对象访问publicstaticvoidprintHelloWorld(){System.out.println(Hello World!);System.out.println(Hello World!);}7.1.1.1 调用方式类名.静态方法 (推荐)对象名.静态方法 (不推荐)7.1.1.2 案例系统启动后要求用户类可以记住自己创建了多少个用户对象了。publicclassUser{// 静态变量publicstaticintnumber;// 构造器publicUser(){User.number;//注意同一个类中访问静态成员可以省略类名不写number;System.out.println(创建了一个对象当前对象数量为number);}}publicclassTest2{publicstaticvoidmain(String[]args){// 目标了解静态变量的应用。newUser();newUser();newUser();System.out.println(User.number);}}7.1.2 实例方法无 static 修饰的成员方法属于对象。只能用对象访问publicvoidprintPass(){...}调用方式对象.实例方法如果方法只是为了做一个功能且不需要直接访问对象的数据这个方法直接定义成静态方法如果这个方法是对象的行为需要访问对象的数据这个方法必须定义成实例方法publicclassStudent{privatedoublescore;publicvoidprintpass(){System.out.println(score60?通过:未通过);}publicstaticvoidprint(){System.out.println(hello world);}publicvoidsetScore(doublescore){this.scorescore;}}publicclassTest{publicstaticvoidmain(String[]args){Student.print();//hello world// Student.printpass();Students1newStudent();s1.print();//hello worlds1.setScore(80.0);s1.printpass();//通过printhello();//hello world1Test.printhello();//hello world1}publicstaticvoidprinthello(){System.out.println(hello world1);}}7.2 main方法publicclassTest{publicstaticvoidmain(String[]args){...}}属于类方法实际执行是 java Test ---- Test.main(…)调用了方法。见上代码Test.printhello();//hello world1
java知识四(面向对象编程)
发布时间:2026/6/8 8:14:08
对象是一种特殊的数据结构可以用来记住一个事物的数据从而代表该事务第一章 面向对象编程快速入门① 先设计对象的模板也就是对象的设计图类。② 通过new关键字每new一次类就得到一个新的对象packagecom.kgl.object;publicclassStar{Stringname;intage;doubleheight;doubleweight;}packagecom.kgl.object;publicclassTest{publicstaticvoidmain(String[]args){StarstarnewStar();star.name张三;star.age18;star.weight50.0;star.height178.0;Starstar1newStar();star1.name李四;star1.age18;star1.weight50.0;star1.height168.0;}}1.1 对象对象本质上是一种特殊的数据结构(可以理解为一张表)对象是用类new出来的有了类就可以创建出对象。publicclass类名{// 1、变量用来说明对象可以处理什么数据// 2、方法描述对象有什么功能也就是可以对数据进行什么样的处理...}语法格式类名 对象名 new 类名();万物皆对象谁的数据谁存储。第二章 构造器publicclassStudent{/** 构造器 */publicStudent(){...}}2.1 构造器构造器是一种特殊方法不能写返回值类型名称必须是类名。创建对象时对象会立即自动调用构造器执行StudentsnewStudent();2.2构造器的常见应用场景构造器常用于完成对象初始化创建对象时同时完成对对象成员变量(属性)的初始化赋值。2.3构造器的注意事项类默认就自带了一个无参构造器如果为类定义了有参数构造器类默认的无参数构造器就没有了此时如果还想用无参数构造器就必须自己手写一个无参数构造器出来。第三章 this 关键字是一个变量可以用在方法中来拿到当前对象。哪个对象调用这个方法this就拿到哪个对象。主要用来解决对象的成员变量与方法内部变量的名称一样时导致访问冲突问题publicclassStudent{Stringname;doublescore;publicvoidprint(){System.out.println(name);System.out.println(this);}publicvoidprintHobby(Stringname){System.out.println(this.name喜欢name);}publicvoidcheckPass(doublescore){if(this.scorescore){System.out.println(恭喜您考上哈佛走向巅峰~~~);}else{System.out.println(不好意思您没有考上~~~);}}}publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();s1.name张三;s1.print();System.out.println(s1);System.out.println(-----------------------------------);Students2newStudent();s2.print();System.out.println(s2);System.out.println(-----------------------------------);Students3newStudent();s3.name李四;s3.printHobby(读书);System.out.println(-----------------------------------);s3.score80;s3.checkPass(90);}}输出张三com.kgl.thisdemo.Student3b07d329com.kgl.thisdemo.Student3b07d329-----------------------------------nullcom.kgl.thisdemo.Student41629346com.kgl.thisdemo.Student41629346-----------------------------------李四喜欢读书-----------------------------------不好意思您没有考上~~~第四章 封装面向对象的三大特征:封装、继承、多态。定义:就是用类设计对象处理某一个事物的数据时,应该把要处理的数据,以及处理这些数据的方法,设计到一个对象中去。4.1 封装的设计规范合理隐藏:使用private关键字(私有,隐藏)修饰成员变量,就只能在本类中被直接访问,其他任何地方不能直接访问。合理暴露:使用public修饰(公开)的get和set方法合理暴露,成员变量的取值和赋值。4.2 代码层面控对象的成员公开或隐藏公开成员:可以使用public(公开)进行修饰。隐藏成员:使用private(私有,隐藏)进行修饰。publicclassStudent{Stringname;privateintage;privatedoublechinese;privatedoublemath;publicvoidsetAge(intage){if(age0||age100){System.out.println(输入的年龄有误);return;}this.ageage;}publicintgetAge(){returnage;}}publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();s1.setAge(-15);System.out.println(s1.getAge());}}第五章 实体类 Javabean5.1 定义是一种特殊类,类中要满足如下需求:要求 1:类中的成员变量全部私有,并提供public修饰的getter/setter方法。在IDEA中可以通过右键菜单-Generate-Getter and Setter快速生成。要求 2:提供get、set 方法,类中需要提供一个无参数构造器,有参数构造器可选。 在IDEA中可以通过右键菜单-Generate-constructor-select None/ok快速生成。5.2 实体类应用场景核实体类的对象只负责数据存取,而对数据的业务处理交给其他类的对象来完成,以实现数据和数据业务处理相分离。用来保存数据的java类,可以用它创建对象,保存某个事物的数据。// 实体类:Student (只负责存数据)publicclassStudent{privateStringname;privatedoublescore;// getter setter 方法...// 生成Getter/Setter:在IDEA中可以通过右键菜单-Generate-Getter and Setter快速生成。}// 业务类:StudentOperator (负责处理逻辑)publicclassStudentOperator{privateStudents;publicStudentOperator(Students){this.Students;}// 打印总成绩// 打印平均成绩// ...}5.3 实操5.3.1 定义实体类packagecom.kgl.Javabean;publicclassStudent{privateStringname;privatedoublechinese;privatedoublemath;publicvoidsetName(Stringname){this.namename;}publicvoidsetChinese(doublechinese){this.chinesechinese;}publicvoidsetMath(doublemath){this.mathmath;}publicStringgetName(){returnname;}publicdoublegetChinese(){returnchinese;}publicdoublegetMath(){returnmath;}publicStudent(){}publicStudent(Stringname,doublechinese,doublemath){this.namename;this.chinesechinese;this.mathmath;}}5.3.2 业务类packagecom.kgl.Javabean;publicclassStudebtOperator{privateStudents;publicStudebtOperator(Students){this.ss;}publicvoidprinttotalScore(){System.out.println(s.getName()的语文成绩是s.getChinese(),数学成绩是s.getMath());}publicvoidprintavgScore(){System.out.println(s.getName()的总成绩是(s.getChinese()s.getMath()),平均成绩是(s.getChinese()s.getMath())/2);}}5.3.3 调用packagecom.kgl.Javabean;publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();s1.setName(播妞);s1.setChinese(80.0);s1.setMath(90.0);System.out.println(s1.getName()的语文成绩是s1.getChinese(),数学成绩是s1.getMath());System.out.println(-------------------------------------------);Students2newStudent(播仔,90.0,80.0);StudebtOperatoroperatornewStudebtOperator(s2);operator.printtotalScore();operator.printavgScore();}}第六章 static关键字6.1 定义叫静态可以修饰成员变量、成员方法。成员变量按照有无static修饰分为两种:6.1.1 静态变量(类变量):有static修饰属于类在计算机里只有一份会被类的全部对象共享。6.1.2 实例变量(对象的变量):无static修饰属于每个对象的。publicclassStudent{// 静态变量staticStringname;// 实例变量(对象的变量)intage;}Student学生表中包含static name和age字段。s1对象和s2对象各自拥有独立的age值(均为0)但共享同一个name字段。推荐访问方式:类名.静态变量不推荐:对象.静态变量。实例变量只能通过 对象.实例变量 访问。6.2 static修饰成员变量 — 成员变量的执行原理publicclassStudent{// 类变量 静态变量:有static修饰属于类只加载一份可以被类和类的全部对象共享staticStringname;// 实例变量(对象变量) 没有static修饰属于对象每个对象都有一份intage;}publicclassTest{publicstaticvoidmain(String[]args){// 1、类变量的用法// 类名.静态变量(推荐)Student.name袁华;// 对象.静态变量(不推荐)Students1newStudent();s1.name马冬梅;Students2newStudent();s2.name秋雅;System.out.println(s1.name);// 秋雅System.out.println(Student.name);// 秋雅// 2、实例变量的用法// 对象.实例变量s1.age23;s2.age18;System.out.println(s1.age);// 23// System.out.println(Student.age); // 报错}}第七章 静态方法如果某个数据只需要一份且希望能够被共享访问、修改则该数据可以定义成静态变量来记住。同一个类中访问静态成员可以省略类名不写,在某个类中访问其他类里的类变量必须带类名访问7.1 static修饰方法成员方法的分类7.1.1 静态方法有 static 修饰的成员方法属于类。可以直接用类名访问也可以用对象访问publicstaticvoidprintHelloWorld(){System.out.println(Hello World!);System.out.println(Hello World!);}7.1.1.1 调用方式类名.静态方法 (推荐)对象名.静态方法 (不推荐)7.1.1.2 案例系统启动后要求用户类可以记住自己创建了多少个用户对象了。publicclassUser{// 静态变量publicstaticintnumber;// 构造器publicUser(){User.number;//注意同一个类中访问静态成员可以省略类名不写number;System.out.println(创建了一个对象当前对象数量为number);}}publicclassTest2{publicstaticvoidmain(String[]args){// 目标了解静态变量的应用。newUser();newUser();newUser();System.out.println(User.number);}}7.1.2 实例方法无 static 修饰的成员方法属于对象。只能用对象访问publicvoidprintPass(){...}调用方式对象.实例方法如果方法只是为了做一个功能且不需要直接访问对象的数据这个方法直接定义成静态方法如果这个方法是对象的行为需要访问对象的数据这个方法必须定义成实例方法publicclassStudent{privatedoublescore;publicvoidprintpass(){System.out.println(score60?通过:未通过);}publicstaticvoidprint(){System.out.println(hello world);}publicvoidsetScore(doublescore){this.scorescore;}}publicclassTest{publicstaticvoidmain(String[]args){Student.print();//hello world// Student.printpass();Students1newStudent();s1.print();//hello worlds1.setScore(80.0);s1.printpass();//通过printhello();//hello world1Test.printhello();//hello world1}publicstaticvoidprinthello(){System.out.println(hello world1);}}7.2 main方法publicclassTest{publicstaticvoidmain(String[]args){...}}属于类方法实际执行是 java Test ---- Test.main(…)调用了方法。见上代码Test.printhello();//hello world1