匿名对象的说明和匿名对象作为方法的参合和返回值
匿名对象的说明
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
new 类名称();
注意事项:匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象
使用建议:如果确定有一个对象只需要使用唯一的一次,就可以用匿名对象
案例:
public static void main(String[] args) {
  //普通的使用方式
    Student stu = new Student();
    stu.name="111";
    stu.age=10;
    //匿名对象
    new Student().student();
}
匿名对象作为方法的参数和返回值
方法的参数案例:
//使用一搬写法传入参数
Scanner sc = new Scanner(System.in);
aaa(sc);
//使用匿名对象来进行传参
aaa(new Scanner(System.in));
}
public static void aaa(Scanner sc){
int num = sc.nextInt();
System.out.println("输入的参数是:"+num);
}
方法的返回值案例:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.println("输入的返回值是:"+i);
public static Scanner bbb(){
return new Scanner(System.in);
}
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号