喷砂除锈设备怎么选择?| 广东鑫百通喷砂机厂家
2026/1/7 18:35:04
Studentstu=newStudent();//有名字的对象//对象名: stunewStudent();//创建的对象没有名字newStudent("张三",23);匿名对象可以像有名对象一样使用,但是只能使用一次
publicclassStudent{privateStringname;privateintage;//构造方法publicStudent(){}publicStudent(Stringname,intage){this.name=name;this.age=age;}//成员方法publicvoidstudy(){System.out.println("学习方法!");}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}publicclassTest1{publicstaticvoidmain(String[]args){//有名对象// Student student = new Student();// student.study();//匿名对象可以像有名对象一样使用,但是只能使用一次//当只使用对象中的某个方法一次时:使用匿名对象newStudent().study();//匿名对象直接调用成员方法}}publicclassTest2{publicstaticvoidshow(Studentstudent){System.out.println("学生姓名:"+student.getName());System.out.println("学生年龄:"+student.getAge());}publicstaticvoidmain(String[]args){//情况一:有名对象// //创建学生对象// Student student = new Student("熊大", 13);// show(student);//把创建的学生对象,作为参数传递//情况2:匿名对象show(newStudent("熊二",12));//匿名对象直接当做方法参数传递}}publicclassTest3{//匿名对象,作为方法的返回值publicstaticStudentcreateStudent(Stringname,intage){returnnewStudent(name,age);}publicstaticvoidmain(String[]args){Studentstu=createStudent("光头强",24);System.out.println("姓名:"+stu.getName());System.out.println("年龄:"+stu.getAge());}}