this关键字
2024-09-14 23:59:01
class Demothis关键字//哪个对象在调用this所在的函数,this就代表哪个对象。
{ //当定义类中功能时,该函数内部要用到该函数的对象时,这时用this来表示这个对象。
public static void main(String[] args)
{
Person P1=new Person(23);
Person P2=new Person(24);
boolean b=P1.compare(P2);
System.out.println(b);
}
}
class Person
{
private int age;
Person(int age)
{
this.age=age;
}
Person(String name)
{
this.name=name;
}
Person(String name,int age)
{
this(name);//this语句只能放语句的第一行: this(name); this.name=name; 对的 this.name=name; this(name); 错的
this.age=age;
}
public boolean compare(Person P)
{
return this.age==P.age;//注意==强制赋值,不然不同类型无法转换: 不兼容的类型: int无法转换为boolean:return this.age=P.age;
}
}
最新文章
- 萌新笔记——封装hiredis——C++与redis对接(一)(string的SET与GET操作)
- 初学者--bootstrap(四)栅格系统----在路上(8)
- php版本引起的const问题
- python中几个常见的黑盒子之“字典dict” 与 “集合set”
- ant打包命令
- Effective Objective-C 2.0 笔记三(Literal Syntax简写语法)
- ESFramework ——可堪重任的网络通信框架
- DEDECMS开启邮箱验证通知的解决方法
- Orleans之EventSourcing
- 解决fiddler无法抓取本地部署项目的请求问题
- LabelFrame
- CSS布局十八般武艺都在这里了
- winrar+目录穿透复现
- XamarinSQLite教程添加测试数据
- Codeforces Round #281 (Div. 2) D(简单博弈)
- MFC树形控件的使用(右键点击)
- Arduino 串口输出LM35温度
- URL传值乱码
- Java工具类(util) 之01- 数学运算工具(精确运算)
- Caliburn.Micro - Getting Started - Introduction