JAVASE类和对象-6 1.类与对象的关系类型务虚、对象务实对象行为履行者 new 类型()类型特征塑造者事物的特征受到类型的约束2.类与对象的创建类型属性列表非必须语法访问修饰符 属性类型 属性名 [ 初始值]方法列表非必须语法访问修饰符 [静态修饰符] 返回值类型||void 方法名(参数列表){方法体}this指向属性对象语法类型 对象名 new 类型()packageA6.entity;publicclassStudent{// 属性列表姓名、年龄、体重publicStringname;publicintage;publicdoubleweight;// 方法列表// - 语法访问修饰符 [静态修饰符] 返回值类型||void 方法名(参数列表){方法体}publicvoidstudy(){System.out.println(各位同学早上好);System.out.println(我的名字是this.name);System.out.println(我的年龄是this.age);System.out.println(我的体重是this.weight);System.out.println(我正在学习Java);}}*entity储存创建的类型 test储存测试类packageA6.test;importA6.entity.Student;publicclassA6_test0{publicstaticvoidmain(String[]args){// 1.1创建学生类对象Students1newStudent();// 1.2调用学生对象的属性s1.name张三;s1.age18;s1.weight60.5;// 1.3调用学生对象的方法s1.study();Students2newStudent();s2.name李四;s2.age19;s2.weight65.0;s2.study();}}案例练习packageA6.entity;publicclassTeacher{publicStringname;publicStringsubject;publicStringclassName;publicintteachAge;publicvoidteach(){System.out.println(this.name);System.out.println(专业方向this.subject);System.out.println(教授课程this.className);System.out.println(教学年龄this.teachAge);}}packageA6.test;importA6.entity.Teacher;publicclassA6_test1{publicstaticvoidmain(String[]args){// 1.1创建教师类对象Teachert1newTeacher();// 1.2调用教师对象的属性t1.name王老师;t1.subject计算机;t1.className使用Java语言理解程序逻辑;t1.teachAge5;// 1.3调用教师对象的方法t1.teach();}}---------------------------------------------------------------//运行结果王老师 专业方向计算机 教授课程使用Java语言理解程序逻辑 教学年龄53.方法普通方法、构造方法普通方法概念普通方法的创建只能基于类调用只能基于方法语法访问修饰符 [静态修饰符] 返回值类型||void 方法名(参数列表){方法体}根据返回值的选择和参数列表的选择可以为普通方法归纳出4大类型有参有返有参无返无参有返无参无返*有返回值时 必须有return关键字 return后面必须出现值 返回的值类型必须与声明的一致要有东西接收才会打出return后面的值类的方法重载现象当一个类中出现方法名相同形式参数列表不同与访问修饰符返回值无关时触发方法重载//1.创建无参数无返回普通方法指令簇publicvoidfun1(){for(inti1;i10;i){System.out.println(i);}}//2.创建有参数无返回普通方法publicvoidfun2(Stringname){for(inti1;i10;i){System.out.println(i name);}}//类的方法重载现象publicvoidfun2(Stringname,intage){for(inti1;i10;i){System.out.println((i age)name);}}//3.创建无参数有返回普通方法publicStringfun3(){System.out.println(hahahaha);returnHello;}//4.创建有参数有返回值普通方法publicStringfun4(Strings){System.out.println(hahahaha);returnHellos;}构造方法功能创建对象创建一个自定义类型时系统会默认创建其隐藏的无参构造方法通过调用Teacher类的无参构造方法创建了具体的Teacher类对象当人为创建了构造方法后默认创建的那个隐藏的无参构造方法将失效想两个都有效只需要明文写个public Dog(){}就可以//创建一个自定义类型时系统会默认创建其隐藏的无参构造方法publicclassTeacher{publicStringname;publicStringsubject;publicStringclassName;publicintteachAge;publicvoidteach(){System.out.println(this.name);System.out.println(专业方向this.subject);System.out.println(教授课程this.className);System.out.println(教学年龄this.teachAge);}}publicclassA6_test1{publicstaticvoidmain(String[]args){//通过调用Teacher类的无参构造方法创建了具体的Teacher类对象t1Teachert1newTeacher();t1.name王老师;t1.subject计算机;t1.className使用Java语言理解程序逻辑;t1.teachAge5;t1.teach();}}packageA6.entity;publicclassDog{publicStringname;publicinthealth;publicintlove;publicStringtype;publicDog(Stringname,inthealth,intlove,Stringtype){this.namename;this.healthhealth;this.lovelove;this.typetype;}publicvoidshow(){System.out.println(this.name);System.out.println(健康值this.health);System.out.println(爱值this.love);System.out.println(类型this.type);}}System.out.println(this.name);System.out.println(健康值this.health);System.out.println(爱值this.love);System.out.println(类型this.type);}}