三、this关键字什么是this关键字this就是一个变量可以用在方法中来拿到当前对象,代码示例publicclassStudent{publicvoidprintThis(){System.out.println(this);}}publicclassTest{publicstaticvoidmain(String[]args){Students1newStudent();System.out.println(s1);s1.printThis();Students2newStudent();System.out.println(s2);s2.printThis();}}输出//输出s1的地址com.zzh.thisdemo.Student23fc625ecom.zzh.thisdemo.Student23fc625e//输出s2的地址com.zzh.thisdemo.Student3f99bd52com.zzh.thisdemo.Student3f99bd52那个对象调用这个方法this就拿到哪个对象this关键字的作用方便访问对象数据代码示例//Student类文件packagecom.zzh.thisdemo;publicclassStudent{Stringname;publicvoidprint(){//this是一个变量用在方法中用于拿到当前对象//那个对象调用这个方法this就拿到哪个对象System.out.println(this);System.out.println(this.name);}}//Test测试文件packagecom.zzh.thisdemo;publicclassTest{publicstaticvoidmain(String[]args){//目标认识this关键字搞清楚this关键字的应用场景。Students1newStudent();s1.name张三;s1.print();System.out.println(s1);System.out.println();Students2newStudent();s2.print();System.out.println(s2);}}输出com.zzh.thisdemo.Student23fc625e张三com.zzh.thisdemo.Student23fc625ecom.zzh.thisdemo.Student3f99bd52nullcom.zzh.thisdemo.Student3f99bd52this的应用场景解决变量名称冲突问题区分成员变量和局部变量示例每个人或物都有它的名(name)、packagecom.zzh.thisdemo;publicclassStudent{Stringname;publicvoidprintHobby(Stringname){System.out.println(this.name喜欢name);}}packagecom.zzh.thisdemo;publicclassTest{publicstaticvoidmain(String[]args){//目标认识this关键字搞清楚this关键字的应用场景。Students3newStudent();s3.name汪苏泷;s3.printHobby(唱歌!);}}输出汪苏泷喜欢唱歌!四、封装封装是什么就是用类设计对象处理某一个事物的数据时应该把要处理的数据以及处理这些数据的方法设计到一个对象中去。面向对象的三大特征封装、继承、多态类就是一种封装封装的设计规范合理隐藏合理暴露代码层面如何控制对象的成员公开或隐藏公开成员可以使用public(公开)进行修饰隐藏成员使用private私有隐藏进行修饰示例packagecom.zzh.capsulation;publicclassStudent{Stringname;//1.如何隐藏使用private关键字私有隐藏修饰成员变量就只能在本类中被直接访问。//其他任何地方不能直接访问。privateintage;privatedoublechinese;privatedoublemath;//2。如何暴露合理暴露使用public修饰公开的get和set方法合理暴露成员的取值和赋值publicvoidsetAge(intage){if(age0age200){this.ageage;}else{System.out.println(您赋值的年龄数据非法);}}publicintgetAge(){returnage;}publicvoidprintAllScore(){System.out.println(name的总成绩是(chinesemath));}publicvoidprintAverageScore(){System.out.println(name的平均成绩是(chinesemath)/2);}}packagecom.zzh.capsulation;publicclassTest{publicstaticvoidmain(String[]args){//目标搞清楚封装的设计思想合理隐藏合理暴露Students1newStudent();s1.setAge(19);System.out.println(s1.getAge());}}五、Javabean实体类什么是实体类是一种特殊类类中满足以下要求类中的成员变量全部私有并提供public修饰的getter/setter方法类中需要提供一个无参数构造器有参数构造器可选代码示例packagecom.zzh.JavaBean;//实体类publicclassStudent{//1、私有成员变量privateStringname;privatedoublechinese;privatedoublemath;//必须提供无参数构造器publicStudent(){}//提供有参数构造器可选publicStudent(Stringname,doublechinese,doublemath){this.namename;this.chinesechinese;this.mathmath;}//2、提供公开的get和set方法publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.namename;}publicdoublegetChinese(){returnchinese;}publicvoidsetChinese(doublechinese){this.chinesechinese;}publicdoublegetMath(){returnmath;}publicvoidsetMath(doublemath){this.mathmath;}}packagecom.zzh.JavaBean;publicclassTest{publicstaticvoidmain(String[]args){//目标认识实体类是什么搞清楚其基本作用和应用场景//实体类的基本作用创建它的对象存储数据封装数据StudentsnewStudent();s.setChinese(100);s.setMath(100);System.out.println(s.getName());System.out.println(s.getChinese());System.out.println(s.getMath());Students2newStudent(播仔,59,100);System.out.println(s2.getName());System.out.println(s2.getChinese());System.out.println(s2.getMath());}}实体类的应用场景实体类对象只负责数据存取而对数据的业务处理交给其他类的对象来完成以实现数据和数据业务处理相分离。代码示例创建Student类packagecom.zzh.JavaBean;//实体类publicclassStudent{//1、私有成员变量privateStringname;privatedoublechinese;privatedoublemath;//必须提供无参数构造器publicStudent(){}//提供有参数构造器可选publicStudent(Stringname,doublechinese,doublemath){this.namename;this.chinesechinese;this.mathmath;}//2、提供公开的get和set方法publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.namename;}publicdoublegetChinese(){returnchinese;}publicvoidsetChinese(doublechinese){this.chinesechinese;}publicdoublegetMath(){returnmath;}publicvoidsetMath(doublemath){this.mathmath;}}创建Test类packagecom.zzh.JavaBean;publicclassTest{publicstaticvoidmain(String[]args){//目标认识实体类是什么搞清楚其基本作用和应用场景//实体类的基本作用创建它的对象存储数据封装数据StudentsnewStudent();s.setChinese(100);s.setMath(100);System.out.println(s.getName());System.out.println(s.getChinese());System.out.println(s.getMath());Students2newStudent(播仔,59,100);System.out.println(s2.getName());System.out.println(s2.getChinese());System.out.println(s2.getMath());//实体类在开发中的应用场景//创建一个学生的操作对象专门负责对学生对象的数据进行业务处理StudentOperatoroperatornewStudentOperator(s2);operator.printTotalScore();operator.printAverageScore();}}创建StudentOperator类packagecom.zzh.JavaBean;publicclassStudentOperator{//必须要拿到要处理的学生对象privateStudents;publicStudentOperator(Students){this.ss;}//提供方法打印学生总成绩publicvoidprintTotalScore(){System.out.println(s.getName()的总成绩是(s.getChinese()s.getMath()));}//提供方法打印学生的平均成绩publicvoidprintAverageScore(){System.out.println(s.getName()的平均成绩是(s.getChinese()s.getMath())/2);}}