this关键字
来源:互联网 发布:jquery ajax json解析 编辑:程序博客网 时间:2024/06/05 06:24
package com.lrq;/** * * @author 李瑞琦 * 解决全局变量跟局部变量同名的问题 * 1、在局部变量前加下滑线 * 2、通过this关键字 * 普通方法中,this总是指向调用该方法的对象。 * 构造方法中,this总是指向正要初始化的对象。 * this不能用于static方法。 * */public class TestThis { public static void main(String[] args) { Dog dog = new Dog("旺财"); System.out.println(dog.name); dog.sayHello("八戒"); }}class Dog{ String name; int age; //空构造器 public Dog(){ System.out.println("我是空构造器"); } /* //把局部变量赋值给全局变量的时候如果重名 //1、可在局部变量前面加下划线 //有参构造器 public Dog(String _name){ name= _name; } //2、可用this关键字 public Dog(String name){ this.name= name; } */ public Dog(String name){ //调用空构造器,构造器在调用构造器的时候需放在方法的最上边并且只能调用一次 this(); this.name=name; } //在普通方法中this总是指向调用该方法的对象 public void sayHello(String name){ System.out.println(this.name+"----"+name); }}
阅读全文
0 0
- 关键字this
- this关键字
- this关键字
- this 关键字
- this 关键字
- this关键字
- this关键字
- this 关键字
- this 关键字
- this 关键字
- this关键字
- 关键字this
- 关键字this
- this 关键字
- this 关键字
- this关键字
- this关键字
- This关键字
- oracle数据库对象的详解
- ubuntu c/c++中调用matlab函数
- python——numpy包的一些功能笔记
- hpu暑期训练:E
- ant的安装和环境配置
- this关键字
- 【bzoj3280】小R的烦恼
- Realm数据库的简单使用
- acm Array
- qt creator 切换到界面语言(切换到英语)
- Java 并发专题 :闭锁 CountDownLatch
- HDU 2017 多校联赛 1011 KazaQ's Socks
- 计算整型数组中的最大值
- Javascript学习小结(1)