zoukankan      html  css  js  c++  java
  • java基础总结(二)---关键字

    1.this

      在java中, this 对象,它可以在类里面来引用这个类的属性和方法。

      1.它在方法内部使用,即这个方法所属的对象的引用。

      2.它在构造器内部使用,表示该构造器正在初始化的对象。

      3.this表示当前对象,可以调用类的属性、方法和构造器。

      在方法内需要用到调用该方法的对象是,就用this。

    public class ThisDemo {
        int num;
        ThisDemo increment() {
            num++;
            return this;
        }
        private void print() {
            System.err.println("num="+num);
        }
        public static void main(String[] args) {
            
            ThisDemo tt=new ThisDemo();
            tt.increment().increment().increment().print();
        }
        
    
    }

    通过this 这个关键字返回自身这个对象然后在一条语句里面实现多次的操作。

      使用this调用本类的构造器

    public class ThisDemo2 {  
        String name;
        int age;
        public ThisDemo2 (){ 
            this.age=21;
       }     
        public ThisDemo2(String name,int age){
            this();
            this.name="Mick";
        }     
      private void print(){
             System.out.println("最终名字="+this.name);
             System.out.println("最终的年龄="+this.age);
        }
        public static void main(String[] args) {
           ThisDemo2 tt=new ThisDemo2("",0); //随便传进去的参数
           tt.print();
        }
    }
  • 相关阅读:
    POJ 2251 Dungeon Master
    HDU 3085 Nightmare Ⅱ
    CodeForces 1060 B Maximum Sum of Digits
    HDU 1166 敌兵布阵(树状数组)
    HDOJ 2050 折线分割平面
    HDU 5879 Cure
    HDU 1878 欧拉回路
    HDU 6225 Little Boxes
    ZOJ 2971 Give Me the Number
    HDU 2680 Choose the best route
  • 原文地址:https://www.cnblogs.com/codeRose/p/7679148.html
Copyright © 2011-2022 走看看