iT邦幫忙

2022 iThome 鐵人賽

DAY 20
0

Intro

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:

  1. Read-only and write-only increase data security, and internal members are better modified and checked regularly.
  2. The client end is separated from the background, which can hide details and unnecessary information and maintain the convenience of consistency.
  3. Properties and functions within the program can be better controlled, reducing interference from bugs for the programmer and improving reliability and development efficiency.

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.


Implmentation

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;
	}
}
  • Program analysis:
  1. In this program, we can see the concept of simple packet usage. The name and password are set to private, and the content is accessed or confirmed as correct by the user's manual entry. Like the password, we define it here first, which means that this function is protected by a packet and cannot be read by the user.
  2. The next three functions are a channel that links the front end with the back end within the package. By entering a name and password, the back end will conduct a review to use the ATM service.

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);
	}
}
  • Program analysis: We use the entered pattern to transfer the information to the packet for storage confirmation, and finally return the information to the screen of our ATM.

Outcome

https://ithelp.ithome.com.tw/upload/images/20221005/20151216z6OD3zpV5R.png

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!!!


上一篇
Day 19 : Modifier
下一篇
Day 21 : Inheritance ( 1 )
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言