this的使用
1、使用this调用本类中的属性
class Person{
private String name;
private int age;
public Person(String name,int age){
name=name;
age=age;
}
public String getInfo(){
return "姓名: "+name+"年龄: "+age;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person("张三",33);
System.out.println(per1.getInfo());
}
}
上述结果说明:现在的构造方法并不能成功把传递进去的值赋值给类中的属性。也就是说,在赋值时属性并不是明确地被指出。实际上name=name;age=age;都是构造方法中的参数。
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String getInfo(){
return "姓名: "+name+"年龄: "+age;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person("张三",33);
System.out.println(per1.getInfo());
}
}
2、使用this调用构造方法
class Person{
private String name;
private int age; public Person()
{
System.out.println("一个新的Person对象被实例化。 ");
} public Person(String name,int age)
{
this();
this.name=name;
this.age=age;
} public String getInfo(){
return "姓名: "+name+"年龄: "+age;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person("张三",33);
System.out.println(per1.getInfo());
}
}
class Person{
private String name;
private int age; public Person()
{
System.out.println("一个新的Person对象被实例化。 ");
} public Person(String name,int age)
{
//this();
this.name=name;
this.age=age;
} public String getInfo(){
return "姓名: "+name+"年龄: "+age;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person("张三",33);
System.out.println(per1.getInfo());
}
}
class Person{
private String name;
private int age; public Person()
{
System.out.println("一个新的Person对象被实例化。 ");
} public Person(String name,int age)
{
//this();
this.name=name;
this.age=age;
this();
} public String getInfo(){
return "姓名: "+name+"年龄: "+age;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person("张三",33);
System.out.println(per1.getInfo());
}
}
说明构造方法是在实例化对象时被自动调用的,也就是说在类中的所有方法中,只有构造方法是被优先调用的,所以使用this调用构造方法必须也只能放在构造方法的第一行。
class Person{
private String name;
private int age; public Person()
{
this.("xh",20);
System.out.println("一个新的Person对象被实例化。 ");
} public Person(String name,int age)
{
//this();
this.name=name;
this.age=age;
this();
} public String getInfo(){
return "姓名: "+name+"年龄: "+age;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person("张三",33);
System.out.println(per1.getInfo());
}
}
说明this调用构造方法时一定要留一个构造方法作为出口,即程序中至少存在一个构造方法不使用this调用其他构造方法。
3、this表示当前对象
class Person{
public String getInfo(){
System.out.println("Person类: "+this);
return null;
}
} public class ThisDemo{
public static void main(String[] args)
{
Person per1=new Person();
Person per2=new Person();
System.out.println("main: "+per1);
per1.getInfo(); System.out.println("main: "+per2);
per2.getInfo();
}
}
随机推荐
- 转:程序员必须知道的几个Git代码托管平台
http://www.open-open.com/lib/view/open1420704561390.html
- linux下正向代理/反向代理/透明代理使用说明
代理服务技术对于网站架构部署时非常重要的,一般实现代理技术的方式就是在服务器上安装代理服务软件,让其成为一个代理服务器,从而实现代理技术.常用的代理技术分为正向代理.反向代理和透明代理.以下就是针对这 ...
- SQL2008安装后激活方式以及提示评估期已过解决方法(转)
第一步:进入SQL2008配置工具中的安装中心第二步:再进入维护界面,选择版本升级第三步:进入产品密钥,输入密钥第四步:一直点下一步,直到升级完毕.SQL Server 2008 Developer: ...
- 【转】【C#】在 Windows 窗体 DataGridView 单元格中承载控件
using System; using System.Windows.Forms; public class CalendarColumn : DataGridViewColumn { public ...
- 【C#】【邮件】C#发送邮件出现 "指定字符串与主题所要求的形式不符"
用C#发送邮件的时候有时会出现指定字符串与主题所要求的形式不符的问题. 经过查阅资料发现原因是主题里面你的字符串中有一些特殊字符导致出错.去掉改类字符即可成功. 比如: 我出现的错误原因是主题中有回车 ...
- Linux下用信号量实现对共享内存的访问保护
转自:http://www.cppblog.com/zjl-1026-2001/archive/2010/03/03/108768.html 最近一直在研究多进程间通过共享内存来实现通信的事情,以便高 ...
- jQuery Event.stopPropagation() 函数详解
stopPropagation()函数用于阻止当前事件在DOM树上冒泡. 根据DOM事件流机制,在元素上触发的大多数事件都会冒泡传递到该元素的所有祖辈元素上,如果这些祖辈元素上也绑定了相应的事件处理函 ...
- ASP.NET中进行消息处理(MSMQ) 一
MSMQ是微软消息队列的英文缩写.那么什么是消息队列?这些介绍网上一大片这里就不多说了.本文对于大虾级的人物来说这只是小玩意而已,对于初学者来说这文章还是有一定的帮助,希望路过的大虾们别笑话我班门弄斧 ...
- 获取iOS系统版本 --- UIDevice
UIDevice类是一个单例,其唯一的实例( [UIDevice currentDevice] ) 代表了当前使用的设备. 通过这个实例,可以获得设备的相关信息(包括系统名称,版本号,设备模式等等). ...
- 删除 windows 下 node_modules 过深的目录
本文同步自我的个人博客:http://www.52cik.com/2015/11/13/node-modules-del.html 说到 node 的模块,确实既好用又蛋疼.相信无数人吐槽 node_ ...