One of the three main characteristics of Java: encapsulation, is a tool that is widely used in various fields.
From a programmer's perspective, the purpose of using this method is to control the scope of information that cannot be seen by internal members, improve the reliability and maintainability of the encapsulated program, and separate it from the client's user interface
For users or businesses, as long as they can provide a useful website or application, the programmer can place the other redundant details inside the package, making the user interface simple and protecting confidential data from being read or hacked.
To sum up, the benefits of encapsulation can be broken down as follows:
In short, as long as the specifications or content do not change, it will not affect the user's use. The various information in the maintenance package can be shut down and maintained regularly.
Next, we will assume that we want to design the implementation of packet configuration and testing in an ATM.
First, we will need to set some variables, such as the user name, password verification, etc.
/*File : ATM.java*/
public class ATM{
private String name;
private boolean password(int pwd){
if(pwd == 12345678 )
return true;
else
return false;
}
public String getName(){
return name;
}
public void setName(String UserName){
this.name = UserName;
}
public boolean confirm_pwd(int pwd){
boolean information;
information = password(pwd);
return information;
}
}
Next, we will set the interface functions that ATM users will encounter:
/*File : Interface.java*/
import java.util.Scanner;
public class Interface{
public static void main(String[] args){
ATM user = new ATM();
Scanner input = new Scanner(System.in);
String user_name;
int user_pwd;
boolean confirm_info;
System.out.println("請輸入使用者姓名 : ");
user_name = input.nextLine(); /*使用者輸入String類型的語法*/
System.out.println("請輸入使用者密碼(8位數) : ");
user_pwd = input.nextInt(); /*使用者輸入Int類型的語法*/
user.setName(user_name);
confirm_info = user.confirm_pwd(user_pwd);
System.out.println("Your name is : " + user.getName);
System.out.println("Your password confirmation : " + confirm_info);
}
}
The above is a simple implementation of the ATM packet. Due to the content and space requirements, if this implementation is to be implemented for real, the concept must be more complex and there is a lot of information to consider. This only introduces the benefits and practicality of packets!!!