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

    this关键字是面向对象编程里面的一个比较重要的概念。学好this关键字可以更深入地理解面向对象传递引用的运用。我们先给大家说一下this关键字的几个特点 (1)this,代表一个类的对象,或者说就是本类的对象 (2)this不能用在static静态方法中 (3)this不能单独出现,要出现都在方法中使用
    我们来看几个示例:

    1. class TestThis {
    2.         public int a;
    3.         public TestThis(int a) {
    4.                 this.a = a;
    5.                 System.out.println(this.a);
    6.                 System.out.println(a);
    7.         }
    8.         public static void main(String[] args) {
    9.                 new TestThis(100);
    10.         }
    11. }
    复制代码

    上面的代码用到this关键字,其中需要给大家解释一下“this.a”这个用法 (1)它相当于对象打点调用变量的应用 (2)一般在一个方法中,局部变量和全局变量同名时,局部变量在方法的使用会将全局的变量隐藏掉。如果你想在一个方法中使用全局变量,就可以this.全局变量名
    示例二:

    1. class TestThis {
    2.        public void dis() {
    3.                System.out.println("===dis方法===");
    4.        }
    5.        public void method() {
    6.                this.dis(); //方法之间也可以利用this关键字打点调用其他方法
    7.        }
    8.         public static void main(String[] args) {
    9.                 new TestThis(100);
    10.         }
    11. }
    复制代码

    示例二代码:告诉我们“方法之间也可以利用this关键字打点调用其他方法

  • 相关阅读:
    EF实现增删改查
    托管代码与非托管代码的区别
    堆、栈以及队列
    C#装箱和拆箱
    Leecode刷题之旅-C语言/python-349两个数组的交集
    Leecode刷题之旅-C语言/python-344反转字符串
    Leecode刷题之旅-C语言/python-217存在重复元素
    Leecode刷题之旅-C语言/python-206反转链表
    Leecode刷题之旅-C语言/python-204计数质数
    Leecode刷题之旅-C语言/python-203移除链表元素
  • 原文地址:https://www.cnblogs.com/yaowen/p/2988279.html
Copyright © 2011-2022 走看看