this用法-当前对象的引用与调用构造函数
来源:互联网 发布:加入淘宝要多少钱 编辑:程序博客网 时间:2024/05/22 06:20
this作用:当成员变量和局部变量重名,可以用关键字this来区分。
this :代表当前对象。this是所在函数所属对象的引用。
this也可以用于在构造函数中调用其他构造函数。
this :代表当前对象。this是所在函数所属对象的引用。
this也可以用于在构造函数中调用其他构造函数。
注意:只能定义在构造函数的第一行。因为初始化动作要先执行
/*
* 构造函数中调用其他构造函数
* */
public class ThisDemo {
private String name;
private int age;
ThisDemo()
{
name = "baby";
age = 1;
System.out.println("person run");
}
ThisDemo(String name)
{
this();
this.name = name;
}
ThisDemo(String name,int age)
{
this.name = name;
this.age = age;
}
public void speak()
{
System.out.println(this.name+":"+this.age);
}
/*
* 判断是否是同龄人
* 注意:传入对象与调用对象
* */
public boolean compare(ThisDemo d)
{
return this.age==d.age;
}
public static void main(String[] args) {
ThisDemo p1=new ThisDemo("aa", 30);
ThisDemo p2=new ThisDemo("bb", 30);
System.out.println(p1.compare(p2));
}
}
0 0
- this用法-当前对象的引用与调用构造函数
- this关键字的作用 使用this调用其他构造方法 使用this表示当前对象
- this用于调用重载构造函数的用法
- 构造函数中base和this的用法与区别
- 构造函数中base和this的用法与区别
- this指针指向当前调用对象,只是用于类的非静态成员函数。静态函数和全局函数 无当前对象,无this指针。
- java 构造函数、非构造函数的this调用
- 类和对象&对象引用和this的引用&构造器作用和构造器重载&继承的特点和用法
- super,this引用构造函数
- 构造函数与this
- 构造函数与this
- day05对象与类,函数,构造函数,this
- 析构函数与虚析构函数的用法,构造函数与析构函数调用情况
- 黑马程序员----关于构造函数与的学习以及this指针在构造函数间的调用规则
- 拷贝构造函数为何可以调用引用参数对象的私有成员?
- 用this调用构造函数
- 对象的this引用
- this对象的引用
- JavaScript中string对象方法
- hdu2897 邂逅明下 (巴什博弈)
- 深入理解SP、LR和PC
- Linux下突破限制实现高并发量服务器
- 【二分+几何】E - Crossed Ladders
- this用法-当前对象的引用与调用构造函数
- 个人对static关键字理解
- Latex 同时引用多篇参考文献
- 数据库 ACID
- 没有鼠标也能玩转PC-电脑操作常用快捷键
- 出现ClassNotFoundException问题
- VS2015+Fltk1.3.3 编程
- Android基础学习之Android 四大组件详解
- 单例模式