Eclispe中面向对象的三大特征
1.封装
2.继承
3.多态
封装的原则
将不需要对外提供的内容隐藏起来
属性隐藏,提供公共的访问方式
封装的好处:
提高安全性
代码复用性
封装的关键词
private 私有的
一个修饰符
修饰成员变量,成员方法
被private修饰的成员只能被本类访问
属性私有化,通过公共get/set方法去访问
局部变量隐藏成员变量(就近原则)
关键字this
this所在类的对象引用
方法被谁调用,this就代表那个对象
使用场景:
局部变量隐藏成员变量(就近原则)
比如
package kangdong;
/属性:
姓名name
工号id
工资salary
行为:
工作work()/
public class Coder {
private String mane;
private String id;
private double salary;
public Coder() {
}
public Coder(String mane, String id, double salary) {
this.mane = mane;
this.id = id;
this.salary = salary;
}
public String getMane() {
return mane;
}
public void setMane(String mane) {
this.mane = mane;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
private String mane;
private String id;
private double salary;
这几句对coder的属性进行了私有化
public Coder(String mane, String id, double salary) {
this.mane = mane;
this.id = id;
this.salary = salary;
}这是有参数的构造方法
public String getMane() {
return mane;
}
public void setMane(String mane) {
this.mane = mane;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
最后面的就是进行用set/get方法的访问
对于对封装对象的调用就在下一期博客为大家详细讲解。