zoukankan      html  css  js  c++  java
  • Java学习笔记三:This用法

    一般情况下,当我们在外部引用其他类的时候,需要声明这个类型引用,比如

    ThisDemo td = new ThisDemo();

    在”Java编程思想”中有这一段例子:

    Banana a = new Banana();
    Banana b = new Banana();        
    a.peel(1);
    b.peel(2);

    我们在声明对象引用的时候,通常用上述的方法,但是在编译器做了一些操作,实际操作结果为:

    它暗自把“所操作对象的引用”作为第一个参数传递。

    Banana.peel(a, 1);
    Banana.peel(b, 2);

    但是如果我们要在当前类的内部使用当前类的引用,这个时候就会用到this关键字。

    This使用方法:

    this关键字表示:对“调用方法的那个对象”的引用,其实就是对当前类的对象的引用。

    1.区分数据成员变量与传参变量,通常使用在get和set方法中。

    class ThisDemo
    {
        private String name;
        private int age;
        
        /**
         * @return the name
         */
        public String getName()
        {
            return name;
        }
        
        /**
         * @param name the name to set
         */
        public void setName(String name)
        {
            this.name = name;
        }
        
        /**
         * @return the age
         */
        public int getAge()
        {
            return age;
        }
        
        /**
         * @param age the age to set
         */
        public void setAge(int age)
        {
            this.age = age;
        }
    }

    2.返回当前对象的引用

    满足在一条语句中对同一个对象执行多次操作。

    这里使用“Java编程思想”的一个例子:

    class Leaf
    {
        int i = 0;
        
        public Leaf increment()
        {
            i++;
            return this; // 返回当前对象Leaf的引用
        }
       
        public void print()
        {
            System.out.println(i);
        }
        
        public static void main(String[] args)
        {
            Leaf leaf = new Leaf();
            leaf.increment().increment().increment().increment().print(); 
        }
    }

    输出结果为:4。这里能够递增的原因是:在 Leaf leaf = new Leaf();  初始化的时候 i = 0,在调用方法 increment() 返回 Leaf 引用的时候 i 没有再次被初始化,而是使用 i++后的值。

    3.将当前对象的引用传递给其他方法。

    例子还是使用“Java编程思想”的,这里先大概说明下场景:

    (1)用户想吃苹果,并且提供出了一个完整的苹果给其他人(这里的“其他人”我们可以理解为加工厂,更深层的可以理解为第三方jar包,工具类等等);

    (2)用户不关心处理方法,只要能返回给一个已经削好皮的苹果,至于“其他人”中什么方法、工具,用户不关心。

    /**
     * 用户类
     */
    class Person
    {
        public void eat(Apple apple)
        {
           // 用户只关心得到削皮的苹果
            // Apple apple 是完整的苹果,没有削皮
            Apple peeled = apple.getPeel();
        }
    }
    
    /**
     * 苹果类
     */
    class Apple
    {
        public Apple getPeel()
        {
            // 将苹果传给给Peeler
            return Peeler.peel(this);
        }
    }
    
    /**
     * 削皮操作
     * 这里只用static放,一般是工具类
     */
    class Peeler
    {
        public static Apple peel(Apple apple)
        {
            // TODO 削皮操作,返回一个已经削皮的苹果
            return apple;
        }
    }

    1).对于 Person 来说,Peeler 是不可见的,并且 Peeler 做什么操作我们也是不可见的。同时,用户也关心。

    2).在传递给Peeler 的使用,在 peel 方法中使用了 this 。

    例子二:

    场景:我们需要组装某个设备(如手机),需要用到不同的组件,如果是多个产品线同时工作,怎么知道没有重复的组件装在同一设备上(一台手机装上两个电池?)

    /**
     * 手机
     */
    class Phone
    {
        private Screen screen;
        private Battery battery;
        
        /**
         * 组装屏幕
         */
        public void addScreen()
        {
            screen = new Screen(this);
        }
        
        /**
         * 组装电池
         */
        public void addBattery()
        {
            battery = new Battery(this);
        }
    }
    
    /**
     * 屏幕
     */
    class Screen
    {
        private Phone p;
        public Screen(Phone phone)
        {
            // TODO 组装屏幕
            p = phone;
        }
    }
    
    /**
     * 电池
     */
    class Battery
    {
        private Phone p;
        public Battery(Phone phone)
        {
            // TODO 组装屏幕
            p = phone;
        }
    }

    使用 this 将当前的 Phone 设置到需要安装的组件上,把 Phone 的引用传递给其他组件。

    PS:现实场景,方便理解:到海鲜市场买海鲜,然后到附近的酒楼进行加工,传递给酒楼的海鲜就是 this。

    4.在构造函数中调用其他构造函数

    如果一个类中有多个构造函数,某个构造函数有想调用当前类的其他构造函数,可以使用this。

    注意:

    1.一个构造函数中不能嗲用两个其他的构造函数;

    2.构造函数必须放在其实位置。

    //----------------方法4--------------
    class ThisSstructure
    {
        public ThisSstructure(int i)
        {
            System.out.println(i);
        }
        
        public ThisSstructure(String string)
        {
            System.out.println(string);
        }
        
        public ThisSstructure(int i, String string)
        {
            this(i); // 调用第一个构造函数,必须放那个在最开始
            // this(Stirng); // 编译报错,不能同时使用两个
        }
        
        public static void main(String[] args)
        {
            ThisSstructure ts = new ThisSstructure(1, "test");
        }
    }

    总结:

    this 表示对当前对象的引用,只要记住这个特性,在使用时多加注意即可。

  • 相关阅读:
    《人类简史》八、融合统一(下)——宗教的法则、历史的混沌
    《今日简史》七、融合统一(中)——帝国的愿景
    《人类简史》六、融合统一(上)——历史的方向、金钱的味道
    《人类简史》五、监狱高墙——想象构建的秩序
    设计模式之职责链模式(Chain of Responsibility)
    设计模式之代理模式(Proxy)
    设计模式之享元模式(FlyWeight)
    设计模式之外观模式(Facade)
    设计模式之装饰模式(Decorator)
    设计模式之组合模式(Composite)
  • 原文地址:https://www.cnblogs.com/winlrou/p/3996254.html
Copyright © 2011-2022 走看看