Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept of Java encapsulation and interface

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the concept of "Java encapsulation and interface". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the concept of Java encapsulation and interface.

Encapsulation and interface

Encapsulation is a common term for computers, that is, to retain a limited external interface (interface) to hide the specific implementation details. For example, in the Linux architecture, we can see that the Linux operating system encapsulates the details of the underlying hardware, leaving only a set of interfaces for system calls. The user is on the outside of the package and can only perform the required operations through the interface.

Encapsulation is common in life. For example, here is a rechargeable flashlight:

Even without reading the instructions, a user can guess the operation of the flashlight: switching and charging. The flashlight uses a plastic shell to hide internal details that the user does not need to touch, leaving only two interfaces, switches and electrical plugs. With these two interfaces, users are sufficient to use the functions that the product wants to achieve in its design. If all the details are exposed to the user at the same time, the user will be at a loss about the product (such as the unshelled remote control below). Therefore, packaging improves the ease of use of the product.

If the product is not packaged, many details of the flashlight or remote control will be exposed to the user: batteries, circuits, sealed rubber, and so on. Although this gives users more freedom to operate on the product, such as directly discharging the battery, taking out a LED light, and so on. However, users tend to bear a greater risk of damaging the product. Therefore, packaging improves the security of the product.

A Java software product is the same as a daily product. There can be many members (data members and methods) inside an object. Some data members and methods are for internal use only. At this point, we want to have a mechanism to "shell" the object so as to encapsulate the object. This makes it easier for users to learn and use external interfaces without having to contact internal members.

Encapsulation of object members

Java controls the external visibility (visibility) of members of an object through three keywords: public, private, and protected.

Public: the member is externally visible, that is, it is part of the interface

Private: this member is not visible externally, can only be used internally, and cannot be accessed from the outside.

(protected deals with the concept of inheritance, which will be discussed later.)

Let's start by encapsulating the previously defined Human class:

Public class Test {public static void main (String [] args) {Human aPerson = new Human; System.out.println (aPerson.getHeight ()); aPerson.growHeight (170); System.out.println (aPerson.getHeight ()); aPerson.repeatBreath (100);}} class Human {/ * * constructor * / public Human (int h) {this.height = h System.out.println ("Ihumm born");} / * accessor * / public int getHeight () {return this.height;} / * mutator * / public void growHeight (int h) {this.height = this.height + h } / * * encapsulated, for internal use * / private void breath () {System.out.println ("hu...hu...");} / * call breath () * / public void repeatBreath (int rep) {int i; for (I = 0; I

< rep; i++) { this.breath(); } } private int height; // encapsulated, for internal use} 内部方法并不受封装的影响。Human的内部方法可以调用任意成员,即使是设置为private的height和breath() 外部方法只能调用public成员。当我们在Human外部时,比如Test中,我们只能调用Human中规定为public的成员,而不能调用规定为private的成员。 通过封装,Human类就只保留了下面几个方法作为接口: getHeight() growHeight() repBreath() 我们可以将Human类及其接口表示为如下图的形式:

"remote control with a shell."

If we forcibly invoke height from main:

System.out.println (aPerson.height)

There will be the following error message:

Test.java:6: height has private access in Human

System.out.println (aPerson.height)

^

1 error

Beep, you've been electrocuted! A member that is specified as private cannot be called externally.

In the usual specification of Java, the data member that expresses the state (such as height) is set to private. Modifications to data members are made through the methods provided by the interface (such as getHeight () and growHeight ()). This specification plays a role in protecting data. The user cannot modify the data directly and must read and write the data through the corresponding method. The designer of the class can add data usage specifications to the interface methods.

Class encapsulation

In a .java file, there can be and only one class with the public keyword, such as the Test class above. So, from any other class, we can call this class directly. The Human class has no keywords. Even earlier, members of our object did not have keywords. This absence of keywords also represents a kind of visibility, which I will delve into in the explanation of package.

Practice wrapping a Torch class to represent a flashlight. The interface has a switch and charging. The internal members have electricity.

Thank you for reading, the above is the content of "the concept of Java encapsulation and interface". After the study of this article, I believe you have a deeper understanding of the concept of Java encapsulation and interface, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report