简体   繁体   English

新手需要帮助理解java代码

[英]Newbie need help understanding java code

I'm very new to the java programming language and I would really like some help understanding what the following code is doing. 我是java编程语言的新手,我真的很想帮助理解以下代码的作用。 I have a pretty decent understanding of what is going on within the Main class. 我对Main类中发生的事情有了相当不错的理解。 My problem is what part "this._" plays within the code. 我的问题是“this._”在代码中扮演的角色。 How exactly are the names getting transferred? 这些名字究竟是如何转移的? This is not homework just self study. 这不是自学的作业。 The exercise can be found here: http://www.learnjavaonline.org/Functions Also, suggested reading would be great! 练习可以在这里找到: http//www.learnjavaonline.org/Functions此外,建议阅读将是伟大的! Thanks! 谢谢!

class Student {
    private String firstName;
    private String lastName;
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public void printFullName(){
        System.out.println(this.firstName+" "+this.lastname);
  }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[] {
            new Student("Morgan", "Freeman"),
            new Student("Brad", "Pitt"),
            new Student("Kevin", "Spacey"),
        };
        for (Student s : students) {
            s.printFullName();
        }
    }
} 

this references to the object your is working in. this引用了你正在使用的对象。

so in your sample 所以你的样本

class Student {
    private String firstName;
    private String lastName;
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public void printFullName(){
        System.out.println(this.firstName+" "+this.lastname);
  }
}

this.firstName is the private String firstName; this.firstNameprivate String firstName; value in your object/class 对象/类中的值
and firstName is the method parameter. firstName是方法参数。

the this is required in this example as it otherwise would be firstName = firstName and that would assign the value of your parameter to itself. 在这个例子中this是必需的,否则它将是firstName = firstName ,并且会将参数的值赋给它自己。

See that variables with "this" are in constructor . 看到带有“this”的变量在构造函数中 This means THE OBJECT, so in the lines: 这意味着THE OBJECT,所以在行中:

public Student(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;

you assing variable to your object. 你为你的对象分配变量。 Remember that these variables are in constructor ! 请记住,这些变量都在构造函数中

The reason this is used is because the variables firstName and lastName are shadowed by the constructor parameters. 究其原因this是用来是因为变量firstNamelastName是通过构造函数的参数阴影。 See the differences with this : 看到与this的差异:

class Student {
    private String firstName;
    private String lastName;
    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
}

Compared to without this : 与没有this相比:

class Student {
    private String myFirstName;
    private String myLastName;
    public Student(String firstName, String lastName) {
        myFirstName = firstName;
        myLastName = lastName;
}

You use this to reference variables in the current object. 您可以使用this来引用当前对象中的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM