zoukankan      html  css  js  c++  java
  • java —— this 关键字

    this关键字的用法

    什么时候用this?

    在程序产生二义性的时候,用 this 来指明当前对象;

    • 普通方法:this 指调用该方法的对象
    • 构造方法:this 指要初始化的对象

    注意!!!

    this 不能用于 static 方法中

    只是看概念的话还是不行

    撸代码:

    /**
     * @author DREAM_yao
     * this关键字的用法
     */


    /*this:创建好的当前对象对象的地址。
     * 构造方法:指向要初始化的对象
     * 
     * 普通方法:指向调用该方法的对象
     * */

    class Score{
        int t1,t2;
        public Score(int t1) {
            this.t1=t1;
            /*必须位于构造方法的第一句*/
        }
        public Score(int t1,int t2){
            this.t1=t1;
            this.t2=t2;/*构造方法*/
        }
        public void getSum() {/*普通方法*/
            System.out.println(this.t1+this.t2);
        }
    }
    public class TestConstructor {
        public static void main(String[] args) {
            Score s1 = new Score(10);
            Score s2 = new Score(5,15);
            s2.getSum();
        }
    }
  • 相关阅读:
    BZOJ2061 : Country
    BZOJ3591: 最长上升子序列
    BZOJ4356 : Ceoi2014 Wall
    BZOJ2159 : Crash 的文明世界
    BZOJ2149 : 拆迁队
    BZOJ2739 : 最远点
    BZOJ4068 : [Ctsc2015]app
    BZOJ4361 : isn
    BZOJ4404 : [Neerc2015]Binary vs Decimal
    BZOJ4402 : Claris的剑
  • 原文地址:https://www.cnblogs.com/HappyKnockOnCode/p/12659756.html
Copyright © 2011-2022 走看看