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();
        }
    }
  • 相关阅读:
    安装tomcat
    sed
    a'w'k
    dwr??
    tomcat-性能?
    windows清理命令
    markdown超链接怎么写?
    ※剑指offer系列19:二叉搜索树与双向链表
    剑指offer系列17:二叉搜索树的后序遍历序列
    剑指offer系列18:二叉树中和为某一值得路径
  • 原文地址:https://www.cnblogs.com/codeRose/p/7679148.html
Copyright © 2011-2022 走看看