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

    package com.itheima_01;
    /*
    * 匿名对象:没有名字的对象
    * 匿名对象的应用场景:
    * 当方法只调用一次的时候可以使用匿名对象
    * 可以当作参数进行传递,但是无法在传参之前做其他的事情
    *
    * 注意:匿名对象可以调用成员变量并赋值,但是赋值并没有意义
    *
    */

    package com.itheima_01;
    /*
     * 匿名对象:没有名字的对象
     * 匿名对象的应用场景:
     *         当方法只调用一次的时候可以使用匿名对象
     *         可以当作参数进行传递,但是无法在传参之前做其他的事情
     *     
     * 注意:匿名对象可以调用成员变量并赋值,但是赋值并没有意义
     *             
     */
    public class AnonymousObejctDemo {
        public static void main(String[] args) {
            //Student s = new Student();
            //s.study();
            //s.study();
            //s.study();
            
            //new Student();//匿名对象,没有变量引用的对象
            //new Student().study();
            //new Student().study();
            //new Student().study();
            
            //new Student().age = 18;
            //System.out.println(new Student().age);
            
            
            //Student s = new Student();
            //s.age = 18;
            //s.name = "张三";
            //method(s);
            
            method(new Student());
            
        }
        
        public static void method(Student s) {
            
        }
    
            
    }
    
    
    class Student {
        String name;
        int age;
        
        public void study() {
            System.out.println("好好学习,高薪就业");
        }
    }
  • 相关阅读:
    第一次软件作业
    第四次作业之单元测试
    第四次作业之代码规范
    第四次作业之psp
    第四次作业之四则运算
    第三周作业之功能测试
    第三周作业之效能分析
    第三周作业之例行报告
    第二周例行报告(王伟东)
    软件工程第二次作业(王伟东)
  • 原文地址:https://www.cnblogs.com/longesang/p/11225756.html
Copyright © 2011-2022 走看看