this的三种常见用法

this的常见用法

一、第一种:this用来区分局部变量和成员变量;

当形参名称与该方法内部的成员名称冲突时,使用this可以对其进行区分;

public class usingOfThis {
	private int age;
	private String name;
		
	public usingOfThis(int age, String name) {
		this.age = age; //this.age就代表的是该方法的成员变量
		this.name = name; //this.name代表的是该方法的成员变量
	}
}
	
二、第二种:this用来指代对象本身,(类里面的方法要通过对象来调用,即:对象.方法()。此时this就代表调用方法的对象)

测试代码

public class ThisTest {
	public static void main(String[] args) {
		usingOfThis one = new usingOfThis(23, "zjh");
		usingOfThis twO = new usingOfThis(23, "zjh");
		System.out.println(one.equals(twO));
	}
}

类中的方法

@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof usingOfThis)) {
			return false;
		}
		usingOfThis number = (usingOfThis)obj;
		return this.age == number.age && this.name.equals(number.name);
	}

测试代码中有这样一行代码:System.out.println(one.equals(twO));one这个对象引用类中的equals()方法时,此时方法中的this就是one,this.age就是one.age。

三、第三种:this用来表示构造方法。
public usingOfThis() {
		this(0, null);
}
	

这个this(0, null)就是该类中的双参构造方法

用来表示构造方法时,需注意

  • this()只能在构造方法中使用。
  • this()只能出现在构造方法中的第一行。
  • this()使用要避免递归问题。
  • 10
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Symbol 常见用法如下: 1. 创建唯一的属性名 由于 Symbol 值是唯一的,因此可以用它来创建唯一的属性名,避免对象属性名冲突。例如: ```javascript const key = Symbol(); const obj = {}; obj[key] = 'value'; console.log(obj[key]); // 'value' ``` 2. 作为常量定义 由于 Symbol 值是唯一的且不可变,因此可以用它来定义常量。例如: ```javascript const RED = Symbol(); const GREEN = Symbol(); const BLUE = Symbol(); function getColor(color) { switch (color) { case RED: return 'red'; case GREEN: return 'green'; case BLUE: return 'blue'; } } console.log(getColor(GREEN)); // 'green' ``` 3. 作为私有属性 由于 Symbol 值不会被暴露在对象的属性列表中,因此可以用它来作为私有属性。例如: ```javascript const name = Symbol(); class Person { constructor(n) { this[name] = n; } getName() { return this[name]; } } const person = new Person('John'); console.log(person.getName()); // 'John' console.log(person[name]); // undefined ``` 4. 作为 Iterator 对象的键 由于 Symbol 值是唯一的,因此可以用它来作为 Iterator 对象的键。例如: ```javascript const myIterable = {}; myIterable[Symbol.iterator] = function* () { yield 1; yield 2; yield 3; }; for (const value of myIterable) { console.log(value); } // Output: // 1 // 2 // 3 ``` 5. 作为内置 Symbol 值 JavaScript 还提供了一些内置的 Symbol 值,如 Symbol.iterator、Symbol.toPrimitive、Symbol.toStringTag 等,可以用来定制对象的行为。例如: ```javascript const obj = { [Symbol.toPrimitive](hint) { if (hint === 'number') { return 42; } else if (hint === 'string') { return 'forty-two'; } else { return true; } } }; console.log(obj + 1); // 43 console.log(obj.toString()); // 'forty-two' console.log(String(obj)); // 'forty-two' console.log(Number(obj)); // 42 console.log(Boolean(obj)); // true ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值