访问权限:指对象能否通过“.”运算符操作自己的变量或通过“.”运算符调用类中的方法;
访问限制修饰符有:private,protected,public,友好权限
特别注意:类的实例方法总是可以操作该类的实例变量和类变量,类方法总是可以操作该类中的类变量;
面向对象编程:提倡对象应当调用方法来改变自己的属性,类应当提供操作数据的方法,这些方法经过精心的设计,使得对数据的操作更加合理。
package demo;
class Tom{
private float weight;
private float setWeight(float weight){
return this.weight=weight;
}
//方法重载1参数个数不同,2参数个数相同,相同参数的类型不同
float setWeight(float weight,float weight2){
return this.weight=weight+weight2;
}
}
public class Demo03 {
public static void main(String[] args) {
Tom tom=new Tom();
//tom.weight=120.0;//非法,对象不能通过"."运算符访问private变量
//float sum=tom.setWeight(120.0);//非法,
float sum2=tom.setWeight(120.0f, 23.0f);
System.out.println(sum2);
}
}