This is the 10th day of my participation in the August More Text Challenge. For details, see:August is more challenging

⭐ August more text challenge day 10 ⭐, review and consolidate Java😁 with friends

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… There is still a long time to go. Let’s work hard together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❀

Self-recommendation, to recommend their own column 😁, welcome friends to collect attention 😊

MybatisPlus column

App Crawler Column

PC crawler column

Big factory interview questions column


Java update directory 😁

🌈Java from entry to grave ⭐ study notes ⭐ (1) String Common methods summary 😊 (small white essential knowledge!!

🌈Java from entry to grave ⭐ study notes ⭐ (2) final, static keywords summary 😊 (small white necessary knowledge points!!

🌈Java from entry to grave ⭐ study notes ⭐ (3) encapsulation and four kinds of permission modifiers detailed explanation 😊 (small white essential knowledge!

1. Overview of packaging

Encapsulation is an important principle of the object-oriented approach, which combines the properties and operations (or services) of an object into a single entity, and hides the internal implementation details of the object as much as possible.

Advantages of encapsulation

  1. Good encapsulation reduces coupling.
2. The internal structure of the class can be modified freely. 3. Member variables can be controlled more accurately. 4. Hide information and implement details.Copy the code

2. Use of encapsulation

public class Main {

    public static void main(String[] args) {
        Student student = new Student();
        student.name="Mantis shrimp";
        student.age=20;
        
        student.show();
    	student.age=-5; student.show(); }}class Student{
    String name;
    int age;

    public void show(a) {
        System.out.println("name:"+name+",age:"+age); }}Copy the code

The above code looks fine on the surface, but in real development there must be some restrictions on the properties, such as: can’t age be negative

== Solution ==

  • Provides getter and setter methods to get and assign properties, and conditions can be set in this method
  • Unregister student.age=20; unregister student.age=20; This assignment method.
public class Main {

    public static void main(String[] args) {
        Student student = new Student();

        student.setAge(5);
        student.show();
        student.setAge(-2); student.show(); }}class Student{
    private String name;
    private int age;

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
    	// Conditional judgment
        if (age >= 0) {this.age = age;
        }else {
            this.age = 0; }}public void show(a) {
        System.out.println("name:"+name+",age:"+age); }}Copy the code


3, the role of encapsulation

  • The data encapsulation feature of the object completely eliminates the problems caused by the separation of data and operation in the traditional structure method, improves the reusability and maintainability of the program, and reduces the burden of the programmer to maintain the data and operation content.
  • The data encapsulation feature of the object can also separate the private data from the public data, protect the private data, reduce the possible interference between modules, and achieve the purpose of reducing the complexity of the program and improving the controllability.

4. Four permission modifiers

In Java, access control characters can be used to protect access to classes, variables, methods, and constructors. Java supports four different access permissions.

  • Default: visible in the same package without any modifiers. Use objects: classes, interfaces, variables, methods.

  • Private: Visible in the same class. Use objects: variables, methods. Note: You cannot modify classes (external classes)

  • Public: Visible to all classes. Use objects: classes, interfaces, variables, methods

  • Protected: visible to classes and all subclasses in the same package. Use objects: variables, methods. Note: Classes (external classes) cannot be decorated.

Use the following table to illustrate the access rights:

The public class can be accessed anywhere. The default class can be accessed only by classes inside the same package


5, four kinds of permission modifiers test

Under the same package, create the Main and test classes

Main.java

public class Main {

    String defaultName;
    private String privateName;
    public String publicName;


    private void privateMethod(a) {
        defaultName = "1";
        privateName = "2";
        publicName = "3";
    }

    public void publicMethod(a) {
        defaultName = "1";
        privateName = "2";
        publicName = "3";
    }

     void defaultMethod(a) {
        defaultName = "1";
        privateName = "2";
        publicName = "3"; }}Copy the code

test.java

Create the test02 class under another package

test02.java

Under the other package, the original private structure, the default declared structure, is not available.





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~