【Java封装】包封在Java是包装代码和数据一起成一个单一的单元,例如,它是混合的几种药物的胶囊的方法。
我们可以通过使私有类的所有数据成员在Java中创建一个完全封装类。现在我们可以使用setter和getter方法来设置,并在其中获得数据。
在Java Bean类是完全封装的类的实例。
在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.srcmini;
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.srcmini;
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());
}
}
Compile By: javac -d . Test.java
Run By: java com.srcmini.Test
输出:
vijay
只读类
//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@srcmini.com");
acc.setAmount(500000f);
//getting values through getter methods
System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+" "+acc.getAmount());
}
}
输出:
7560504000 Sonoo Jaiswal sonoojaiswal@srcmini.com 500000.0
推荐阅读
- Java正则表达式
- Java访问修饰符
- Java包package
- Java接口
- Java抽象类和接口的区别
- Java抽象类
- Java instanceof操作符
- Java静态绑定和动态绑定
- Java多态性