Eclispe中面向对象的封装

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方法的访问

对于对封装对象的调用就在下一期博客为大家详细讲解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小康康953

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值