Class encapsulation and packaging (JAVA)

Table of contents

encapsulation

In the same package:

Bag

custom package

In different packages:


encapsulation

All OOP languages ​​will have three characteristics:

  1. encapsulation
  2. Inheritance (click to jump)
  3. Polymorphism (click to jump)

This article will bring you knowledge about packaging.

In our daily life, we can see that the TV has only a few buttons (switches, menus...) and some interfaces, and we can realize our needs through these things without delving into its structure. And some of its internal structures (CPU, ...) Manufacturers will use a shell to package them to prevent us from directly seeing the contents inside.

Encapsulation in the OOP language is: organically combining data and methods of manipulating data, hiding the attributes and implementation details of objects, and only exposing interfaces and methods to interact with objects.

Then the question comes. We know that we can use the '.' operator outside the class to access its internal properties, so how should it hide properties and implementation details?

In Java, encapsulation is mainly achieved through classes and access rights : classes can combine data and methods of encapsulating data, which is more in line with human perception of things, and access rights are used to control whether methods or fields can be directly outside the class use.

scope private

       default

(write nothing)

protected public
same class in same package Can Can Can Can
different classes in the same package Can Can Can
subclasses in different packages Can Can
Non-subclasses in different packages Can
  • public: It can be understood as a person's appearance characteristics, which can be seen by anyone;
  • private: only you know, others do not know 

At this point, we can restrict the members of the class from being accessed by the outside world through access qualifiers.

It can be seen from the table that their scope is both packages and classes, so we know what a class is, and what is a package? For the time being, you can ignore the introduction later in the article.

In the same package:

At this point, you don't have to worry about what the package is, because we usually write code in the same package during the learning stage.

 

At this point we can see that although there is a name attribute in the class, because its permission is private, the system will report an error when we access it outside the class. The permission of age is the default and can be accessed by different classes in the same package. 

So what should we do if we want to modify the name? Because it is modified by private and can only be accessed by members of the class, so at this time we can write a method to modify the name or the simplest and rude method to directly modify the private.

class Student{
    private String name;
    int age;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class Main {
    public static void main(String[] args) {
        Student a = new Student();
        a.setName("zhangsan");
        System.out.println(a.getName());
        System.out.println(a.age);
    }
}

Bag

The concept of a package:

        In order to better manage classes, multiple classes are collected together into a group called a package.

Note: Classes with the same name are allowed in the same project, as long as they are in different packages.
In fact, you can simply understand a package as a folder .

 In Java, you can import classes in a package through the import statement.

That's right, the statement similar to the C language header file written in the first line of the code when we usually input and output is actually the class in the import package. for example:

import java.util.Scanner;

custom package

Basic rules for custom packages: 

  1. Add a package statement at the top of the file to specify which package the code is in;
  2. The package name needs to be specified as a unique name as much as possible, usually in the reversed form of the company's domain name (for example: com.baidu.www);
  3. The package name should match the code path. For example, if you create a com.baidu.www package, there will be a corresponding path com/baidu/www to store the code;
  4. If a class has no package statement, the class is placed in a default package.

First how we create a package:

The process of creating a package in IDEA is as follows, first right-click on src and then place the mouse on new and then click on package;

Then right click on the package you created to create a class:

 At this point, you can see that IDEA has created a file under this path:

At this time, we can see that there is an additional statement on the created class

And we used to be the default package of the system so there is no.

In different packages:

 At this time, because the two classes do not form a parent-child relationship, only sex does not report an error.

As follows, only protected and public modifiers can be accessed in subclasses of different packages.

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/132025189