原文链接:https://dzone.com/articles/encapsulation-with-a-simple-example-in-java
(想自学习编程的小伙伴请搜索圈T社区,更多行业相关资讯更有行业相关免费视频教程等待你来学习。完全免费哦! )
在这篇博客中,我将尝试尽可能简单地解释Java中的封装原则。
如果你做一些研究,你会发现封装是OOP的三个主要原则之一。要在Java中实现封装,您需要:
1.将类的变量声明为私有
2.提供公共setter和getter方法来读取和写入变量的值
以下示例进一步说明了这一点:
public class Coat {
private double price;
private String customer;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
}
现在,我们来谈谈封装的好处:
- 其他类将无法直接访问数据,因为变量是私有的。
- 您可以使类成为只读(通过仅创建getter方法)或只写(通过仅创建setter方法)。
- 您可以控制您的数据。在setter和getter方法中,您可以编写特殊条件的逻辑。
- 它提高了灵活性和可重用性。
在了解了封装的好处之后,您可能会问:只有将变量声明为私有并为它们设置setter和getter方法,您是如何实现这些好处的?如果我们不遵循封装原则会出现哪些问题?
大多数初学者一开始会问这些问题。但这些问题的答案并不难理解。我们只需要让我们的例子更复杂。这是因为如果示例过于简单,我们无法向您展示如何防止在将来使用类时可能出现的问题。让我们对代码进行一些调整并逐一解释它们的好处:
假设外套的价格因客户而变化。如果客户是学生,他或她可享受20%的折扣。因此, getPrice() 和setPrice()方法应该是这样的:
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(customer.equals("student")){
this.price = price * 0.8;
}else{
this.price = price;
}
}
现在,如果没有封装,那么 Coat 该类是:
public class Coat {
//instance variables are public
public double price;
public String customer;
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(customer.equals("student")){
this.price = price * 0.8;
}else{
this.price = price;
}
}
public String getCustomer() {
return customer;
}
public void setCustomer(String customer) {
this.customer = customer;
}
}
让我们说两位程序员使用你的课程。在这种情况下,这些不能算错,是吗?
class FirstUserOfCoat{
public static void main(String[] args) {
Coat firstCoat = new Coat();
calculateCoatPrice(firstCoat, 100);
}
public static void calculateCoatPrice(Coat coat, double price){
//if price is public variable,we can set its value like this
coat.price = price * 0.7;
System.out.println(coat.price);
}
}
class SecondUserOfCoat{
public static void main(String[] args) {
Coat secondCoat = new Coat();
calculateCoatPrice(secondCoat,100);
}
public static void calculateCoatPrice(Coat coat, double price){
//if price is public variable,we can set its value like this
coat.price = price * 0.5;
System.out.println(coat.price);
}
}
如您所见,它们不应用您定义的业务逻辑。但是,当您将变量声明为私有,并且用户只能使用getter和setter访问它们时,它们只能应用您的业务逻辑。这就是为什么实例变量必须是私有的。
接下来,让我们假设你的老板不想改变大衣的价格和顾客,即使用初始值创建它也是如此。在这种情况下,代码看起来像这样:
public class Coat {
private double price;
private String customer;
public Coat(){
this.price = 100;
this.customer = "student";
}
public double getPrice() {
return price;
}
public String getCustomer() {
return customer;
}
}
现在,每件外套的价格都是100美元,每件外套的顾客都是学生。实例变量是私有的,并且没有setter方法。这就是为什么 Coat 班级用户不能改变外套的价格和顾客的原因。这件外套现在是只读的。
与案例1一样,我们可以根据需要在getter和setter方法中编写业务逻辑。那时,该类的用户将只访问您为其定义业务逻辑的数据。
最后,假设您的老板后来决定学生的折扣为30%。那时,您必须更改setter方法中的百分比,并且您的用户不需要对其代码进行任何更改:
public double getPrice() {
return price;
}
public void setPrice(double price) {
if(customer.equals("student")){
this.price = price * 0.7; //just change 0.8 to 0.7
}else{
this.price = price;
}
}
希望这对您的下一个项目有所帮助!
Java中封装原则的简单解释

本文主要解释Java中的封装原则,实现封装需将类变量声明为私有,并提供公共setter和getter方法。还阐述了封装的好处,如其他类无法直接访问数据、可控制数据等,通过具体案例说明遵循封装原则能避免问题,提高代码灵活性和可重用性。
268

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



