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();
}
}

随机推荐

  1. webpack htmlWebpackPlugin 静态资源 版本控制

    plugins: [ new webpack.optimize.UglifyJsPlugin({ // 压缩webpack 后生成的代码较长时间,通常推到生产环境中才使用 compress:{ war ...

  2. html2canvas手机端模糊问题

    待解决.测试对于图片之类的没有影响.但是文字在手机上看起来比较模糊.

  3. VS代码片段(snippet)创作工具——Snippet Editor(转)

    原文:http://blog.csdn.net/oyi319/article/details/5605502 从Visual Studio 2005开始,IDE支持代码片段.代码片段以代码缩写和TAB ...

  4. [C#]動態叫用Web Service

    http://www.dotblogs.com.tw/jimmyyu/archive/2009/04/22/8139.aspx 摘要 Web Service對大家來說想必都不陌生,也大都了解Web S ...

  5. [Android] 【视频】黑马安卓62、66期等教程+源码

     下载地址:http://fu83.cn/thread-58-1-1.html

  6. Caffe学习系列(4):激活层(Activiation Layers)及参数

    在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...

  7. Android 主题和选择器

    今天在做底部tab的时候因为样式都一样 所以就自定义一个style 这样省的写很多重复的样式(懒懒懒懒), 修改的话直接在样式里修改省去一个一个修改一样的代码 1 在values/styles.xml ...

  8. 学习Shell脚本编程(第3期)_在Shell程序中使用的参数

    位置参数 内部参数 如同ls命令可以接受目录等作为它的参数一样,在Shell编程时同样可以使用参数.Shell程序中的参数分为位置参数和内部参数等. 3.1 位置参数 由系统提供的参数称为位置参数.位 ...

  9. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  10. 接收content-type:multipart/form-data类型的参数

    一.问题描述 最近在写接口程序,该接口需要与其他公司的程序对接.对方发送content-type:multipart/form-data类型的参数,结果通过request.getParameter(p ...