Course Note: Create Methods,Public Access Modifier,Method Overload,Documentation Standards

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值