Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

java - how to understand the agrument return the variables

I find the similar code in the c# language, now I still do not understand it in java ,the code is below

public class TaskItemAnswerObject {
    private Integer examPaperId;
    private Integer examPaperAnswerId;
    private Integer status;

    public TaskItemAnswerObject(){

    }

    public TaskItemAnswerObject(Integer examPaperId, Integer examPaperAnswerId, Integer status) {
        this.examPaperId = examPaperId;
        this.examPaperAnswerId = examPaperAnswerId;
        this.status = status;
    }
}

so why we fist define the integer variable examPaperId,then why we repeat do such thing ,like "this.examPaperId= examPaperId",it is strange for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are three places where the name examPaperId is referenced. The purpose of each line is as follows:

private Integer examPaperId;

declares that the TaskItemAnswerObject class contains an instance variable name examPaperId of type Integer.

public TaskItemAnswerObject(Integer examPaperId, Integer examPaperAnswerId, Integer status) {

declares a constructor for the TaskItemAnswerObject object that takes a parameter of type Integer, named examPaperId, as its first parameter.

this.examPaperId = examPaperId;

is executed when the constructor mentioned above is called. It takes the incoming parameter named examPaperId and assigns it to the instance variable named examPaperId on the instance of the TaskItemAnswerObject class that is being constructed.

There are other languages that can do all of this in less code. Kotlin, specifically, can do all of this in a single line of code. But Java's syntax is what it is. This is just the way all of this works in Java.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...