java this的使用(翻译自Java Tutorials)

原文出自 http://www.cnblogs.com/ggjucheng/archive/2012/11/28/2793257.html

前言

在一个实例方法或者是构造方法中,this引用指向当前的对象---方法调用或者是构造方法调用的对象。你可以在实例化方法或者构造方法中,使用this引用任何成员。

 

在字段中使用this

使用this关键字的最常见的原因,是字段被方法或构造函数的参数隐藏了。

例如,Point类是这样写的:

public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}

但是它也可以这么写:

public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}


构造方法的每个参数都隐藏了对象的字段---在构造方法里,x是构造方法的第一个参数的局部副本,引用Point字段x,构造方法必须使用this.x.

 

在构造方法使用this

在构造方法里,你可以使用this关键字调用类的另一个构造方法。这种是显式构造方法调用。这里有一个Rectangle类:

public class Rectangle {
    private int x, y;
    private int width, height;
        
    public Rectangle() {
        this(0, 0, 0, 0);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}


这个类有一系列构造方法,每个构造方法初始化Rectangle的部门变量。如果没有为参数提供初始化值,构造方法为每个成员变量提供了默认值。例如,无参构造方法,传入四个值为0的参数,调用四个参数的构造方法,还有两个参数的构造方法,传入两个0的参数,调用四个参数的构造方法。之前说过,编译器是根据参数的个数和类型,决定调用哪个构造方法。

 

如果存在,另一个构造函数的调用必须是构造函数中的第一行。

转载于:https://www.cnblogs.com/ggjucheng/archive/2012/11/28/2793257.html

Here are some tips that could help you in your journey to learn Java: 1. Get Familiar with the Basics: Start with the basics of Java syntax, data types, variables, operators, control structures, loops, and functions. 2. Practice, Practice, Practice: The more you practice, the better you get. Write code to solve simple problems, and gradually increase the complexity of the problems you tackle. 3. Use Online Resources: There are many online resources available for learning Java, including online tutorials, video courses, and forums. Take advantage of these resources to deepen your understanding of the language. 4. Join a Study Group: Joining a study group can be a great way to stay motivated and learn from others. You can also learn from others’ mistakes and share your own experiences. 5. Read Java Documentation: The official Java documentation is a wealth of information, and it’s a great resource for learning the ins and outs of the language. 6. Work on Projects: Choose a project that interests you, and work on it until it’s complete. This will help you put into practice everything you’ve learned and also give you a sense of accomplishment. 7. Debugging: Learn to debug your code effectively. This will help you find and fix bugs quickly and easily, and improve your overall programming skills. 8. Stay Up-to-Date: Java is constantly evolving, and new features and libraries are added regularly. Stay up-to-date with the latest developments in the Java world to improve your skills and keep your knowledge fresh. Remember, learning Java takes time and effort, but with persistence and determination, you can become a proficient Java programmer.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值