Define A Method
Step 1: Define the method signature
• name
• input parameters
• return type
Write the method signature and body in the class
Step 2: Return value

Step 3: Method body



当一个类的属性(成员变量)名与访问该属性的方法(如主方法)参数名(局部变量)相同时,则需要使用 this 关键字来访问类中的属性,以区分类的属性和方法中的参数。
在类中,属性的变量名已经被命名为 name,而在这个类的方法里,参数名也为 name

编译器将根据就近原则,只使用局部变量(参数)。
加了this 之后 编译器将会给 this.name 使用成员变量:

下图为main方法所在类

Using "this" key word to access the current object(replace p by the keyword "this")
The Public Access Modifier
Write "public" in front of attributes and method signatures,means that “everyone” (e.g. other Java classes) can access your
attributes and methods.

Overloading Methods
Two or more methods in the same class with the same name is an overloaded method.
The compilor will know which method you wanna use by the number and type of paraments:

Code Documentation Standards



6317

被折叠的 条评论
为什么被折叠?



