zoukankan      html  css  js  c++  java
  • java中this的用法

    ///this的用法  this 用在类内,构造函数或一般函数中
                   ///当函数内部的变量有和属性相同的变量时 改变的是属性(类内的变量)
    
    
    public class ThisDemo {  
        int number=0;
        ThisDemo increment(){
             number++;///
             return this;
        }  
      private void print(){
             System.out.println("number="+number);
        }
        public static void main(String[] args) {
            ThisDemo tt=new ThisDemo();
             tt.increment().increment().increment().print();
        }
    }
    结果number=3;
    
    public class ThisDemo {  
        int number=0;
        ThisDemo increment(){
              int number=1;
             this.number++;///
             return this;
        }  
      private void print(){
             System.out.println("number="+number);
        }
        public static void main(String[] args) {
            ThisDemo tt=new ThisDemo();
             tt.increment().increment().increment().print();
        }
    }
    
    输出结果是 number=3;
    
  • 相关阅读:
    生成器笔记
    迭代器笔记
    hashilib_module
    Ubuntu操作及各种命令笔记
    python正则表达式2
    python正则表达式1
    python文件操作(with关键字)
    python文件操作
    python包
    python模块
  • 原文地址:https://www.cnblogs.com/weiweiyi/p/5229094.html
Copyright © 2011-2022 走看看