📜  java 封装

📅  最后修改于: 2020-09-24 04:48:58             🧑  作者: Mango

用Java封装

Java中的封装是将代码和数据包装到单个单元(例如,混合了多种药物的胶囊)中的过程。

我们可以通过将类的所有数据成员设为私有来在Java中创建一个完全封装的类。现在我们可以使用setter和getter方法来设置和获取其中的数据。

JavaBean类是完全封装的类的示例。

Java封装的优势

通过仅提供setter或getter方法,可以将类设置为只读或只写。换句话说,您可以跳过getter或setter方法。

它为您提供了对数据的控制。假设您要设置的id值仅应大于100,您可以在setter方法中编写逻辑。您可以编写不将负数存储在setter方法中的逻辑。

这是一种在Java中实现数据隐藏的方法,因为其他类将无法通过私有数据成员访问数据。

封装类易于测试。因此,最好是进行单元测试。

标准的IDE提供了生成getter和setter的工具。因此,使用Java创建封装类非常容易且快速。

Java封装的简单示例

让我们看一下简单的封装示例,该封装的setter和getter方法只有一个字段。

档案:Student.java

"//A Java class which is a fully encapsulated class.  
//It has a private data member and getter and setter methods.  
package com.javatpoint;  
public class Student{  
//private data member  
private String name;  
//getter method for name  
public String getName(){  
return name;  
}  
//setter method for name  
public void setName(String name){  
this.name=name  
}  
}  

文件:Test.java

"//A Java class to test the encapsulated class.  
package com.javatpoint;  
class Test{  
public static void main(String[] args){  
//creating instance of the encapsulated class  
Student s=new Student();  
//setting value in the name member  
s.setName("vijay");  
//getting value of the name member  
System.out.println(s.getName());  
}  
}  

只读类

"//A Java class which has only getter methods.  
public class Student{  
//private data member  
private String college="AKG";  
//getter method for college  
public String getCollege(){  
return college;  
}  
}  

现在,您无法更改学院数据成员“AKG”的值。

"s.setCollege("KITE");//will render compile time error  

只写类

"//A Java class which has only setter methods.  
public class Student{  
//private data member  
private String college;  
//getter method for college  
public void setCollege(String college){  
this.college=college;  
}  
}  

现在,您无法获得大学的价值,只能更改大学数据成员的价值。

"System.out.println(s.getCollege());//Compile Time Error, because there is no such method  
System.out.println(s.college);//Compile Time Error, because the college data member is private.   
//So, it can't be accessed from outside the class  

Java封装的另一个示例

让我们看一下封装的另一个示例,该示例只有四个带有setter和getter方法的字段。

文件:Account.java

"//A Account class which is a fully encapsulated class.  
//It has a private data member and getter and setter methods.  
class Account {  
//private data members  
private long acc_no;  
private String name,email;  
private float amount;  
//public getter and setter methods  
public long getAcc_no() {  
    return acc_no;  
}  
public void setAcc_no(long acc_no) {  
    this.acc_no = acc_no;  
}  
public String getName() {  
    return name;  
}  
public void setName(String name) {  
    this.name = name;  
}  
public String getEmail() {  
    return email;  
}  
public void setEmail(String email) {  
    this.email = email;  
}  
public float getAmount() {  
    return amount;  
}  
public void setAmount(float amount) {  
    this.amount = amount;  
}  
  }  

文件:TestAccount.java

"//A Java class to test the encapsulated class Account.  
public class TestEncapsulation {  
public static void main(String[] args) {  
    //creating instance of Account class  
    Account acc=new Account();  
    //setting values through setter methods  
    acc.setAcc_no(7560504000L);  
    acc.setName("Sonoo Jaiswal");  
    acc.setEmail("sonoojaiswal@javatpoint.com");  
    acc.setAmount(500000f);  
    //getting values through getter methods  
    System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+" "+acc.getAmount());  
}  
}