java 的this 用法
来源:互联网 发布:比特币网络源代码分析 编辑:程序博客网 时间:2024/06/08 12:16
必须用this关键字的三种情况:
1、我们想通过构造方法将外部传入的参数赋值给类的成员变量,构造方法的形式参数名称与类的成员变量名相同。例如:
class Person
{
String name;
public Person(String name)
{
this.name = name;
}
}
2、假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如: {
String name;
public Person(String name)
{
this.name = name;
}
}
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this);
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}
{
Component comp;
public void addComponent()
{
comp = new Component(this);
}
}
class Component
{
Container myContainer;
public Component(Container c)
{
myContainer = c;
}
}
public class Person
{
String name;
int age;
public Person(String name)
{
this.name = name;
}
public Person(String name,int age)
{
this(name);
this.age = age;
}
}
{
String name;
int age;
public Person(String name)
{
this.name = name;
}
public Person(String name,int age)
{
this(name);
this.age = age;
}
}
1.this可指代对象自己本身: 当在一个类中要明确指出使用对象自己的属性或方法时就应该加上this引用,如下例 子:
输出的结果为: Maybe=love you
this.Maybe=love
this.Maybe=love you
第一行直接对构造函数中传递过来的参数Maybe进行输出,第二行是对成员变量(属性) Maybe的输出,第三行是先对成员变量(属性)Maybe赋值后进行输出。
2.在一个构造函数中调用另一个构造函数时,用this关键字:
以下例子:
public class Flower{
private int petalCount=0;
private String s=new String("null");
public Flower(int petals){
petalCount=petals;
}
public Flower(String ss){
s=ss;
}
public Flower(String s,int petals){
this(petals); //java中在一个构造函数中可以调用一次其他的构造函数,并且这条语句 必须在这个构造函数的第一行
this.s=s;
}
}
private int petalCount=0;
private String s=new String("null");
public Flower(int petals){
petalCount=petals;
}
public Flower(String ss){
s=ss;
}
public Flower(String s,int petals){
this(petals); //java中在一个构造函数中可以调用一次其他的构造函数,并且这条语句 必须在这个构造函数的第一行
this.s=s;
}
}
public class love{
String Maybe="love";
public love(String Maybe){
System.out.println("Maybe=" + Maybe);
System.out.println("this.Maybe="+this.Maybe);
this.Maybe=Maybe;
System.out.println("this.Maybe="+this.Maybe);
}
public static void main(String[] args){
love s=new love("love you");
}
}
String Maybe="love";
public love(String Maybe){
System.out.println("Maybe=" + Maybe);
System.out.println("this.Maybe="+this.Maybe);
this.Maybe=Maybe;
System.out.println("this.Maybe="+this.Maybe);
}
public static void main(String[] args){
love s=new love("love you");
}
}
0 0
- JAVA的this用法
- Java - this的用法
- java This的用法
- java----this的用法
- java 的this 用法
- java this的用法
- java this的用法
- java中this的用法
- java中this的用法
- java中this的用法
- java中this的用法
- java中的this的用法
- java中this的用法
- java里this的用法
- Java this、super的用法
- java中this的用法
- java中this的用法
- Java this、super的用法
- c#.net常用字符串函数(笔记)
- 虚拟机上Rathat6.3安装MySQL-5.6.16-1.rhel5.x86_64.rpm-bundle教程
- Java中的NULL
- 雨人速算24点(HDU1427)
- 数据结构——树的术语
- java 的this 用法
- hdu 1518 Square(dfs)
- unity 扩展编辑器 文件操作时常用的类与方法
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
- 【排序】【二分】【UVa 815】洪水(Flooded!)
- LintCode_205 Interval Minimum Number
- DES/3DES/AES区别
- [源码学习][知了开发]WebMagic四大组件-Downloader,Pipeline,PageProcesser
- DM368学习--捕获视频图像分辨率修改