zoukankan      html  css  js  c++  java
  • 匿名对象

    匿名对象的概述:

    指的就是“没有名字”的对象

    有名字的对象:
        Student stu = new Student();
        stu.show();
        stu.study();
    匿名对象:
        new Student();

    使用匿名对象:

    特点:匿名对象只能使用一次

    public class Test {
        public static void main(String[] args) {
            /*
                匿名对象:
                    概述:没有名字的对象
                    特点:匿名对象只能使用一次
                    使用场景:当某个类的对象只需要使用一次的时候,就可以使用该类的匿名对象
                            例如:方法的参数,方法的返回值
             */
            // 创建对象
            Student stu1 = new Student("热巴",18);// 有名字的对象
            stu1.show();
            stu1.show();
    
            System.out.println("==================================");
            //匿名对象
            new Student("热巴",18).show();// 没有名字的对象
            new Student("热巴",18).show();// 没有名字的对象
    
            System.out.println("==================================");
            // 调用method1方法
            Student stu2 = new Student("热巴",18);// 0x11901
            method1(stu2);// 有名字的对象传参
            method1(new Student("热巴",18));// 匿名对象的方式传参数
    
            System.out.println("==================================");
            Student stu3 = method2();// 0x11908
            stu3.show();// 丽颖,18
    
        }
    
        public static void method1(Student stu){// 0x11901
            stu.show();
        }
    
    
        public static Student method2(){
            //Student stu = new Student("丽颖",18);// 0x11908
            //return stu;// 0x11908
    
            return new Student("丽颖",18);
        }
    
    
    }
  • 相关阅读:
    PHP 之sha256 sha512封装
    PHP 之中文转为拼音
    Redis 之仿微博demo
    PHP操作Redis相关函数
    存储过程和变量
    视图
    查询
    约束
    基础一
    轮播图--JS手写
  • 原文地址:https://www.cnblogs.com/YwhsR0129/p/13622494.html
Copyright © 2011-2022 走看看