Java object oriented -- encapsulation

Posted by ReD_BeReT on Sun, 16 Jan 2022 07:59:32 +0100

1, Definition

  • Hide some information of the class inside the class and do not allow external programs to access it directly
  • The methods provided by this class can realize the operation and access to hidden information
  • Hide object information
  • Set aside an accessible interface

2, Characteristics

  • Data can only be accessed through specified methods
  • Hide the instance details of the class to facilitate modification and implementation

3, Realize

Create cat java

package com.test.animal;

public class Cat {
	// Member attributes: nickname, age, weight, variety
	private String name;
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return "I'm a bird named" + this.name + "My pet cat";
	}
}
 

Create cattest java

package com.test.animal;

public class CatTest {

	public static void main(String[] args) {
         Cat one = new Cat();
         one.setName("Xiaobai");
         System.out.println(one.getName());		
	}
}

eclipse provides us with a convenient method to generate set get

Modify in the code just

package com.test.animal;

public class Cat {
	// Member attributes: nickname, age, weight, variety
	private String name;
    private int month;
	private double weight;
	private String species;
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return "I'm a bird named" + this.name + "My pet cat";
	}
}

Right click in the blank space of eclipse

What we can see in this is that we do not have methods to generate getters and setters. We can select select all to set all, select Getters only sets getter methods (read-only), Select Setters only sets setter methods (write only), and Access modifier can set the methods of modifiers we generate.

Select Select All and the result is:

4, Define package

  • Syntax:
    • package name
  • be careful:
    • Must be placed on the first line of the Java source file
    • There can only be one package statement in a Java source file
    • All package names are in English lowercase
    • Naming method: domain name in reverse order + module + function
  • Three methods of cross package call
    • import com.test.animal.Cat; // Import the class specified in the package
    • import com.test.animal.*; // Import all classes in the package
    • import com.test.animal.Cat cat = new import com.test.animal.Cat(); // Called directly by class name

5, static keyword

  • Access modifier static information method return value type method name (method parameter) public static void main(String[] args)
  • Class object sharing
  • Class is generated when it is loaded and released when it is destroyed, with a long life cycle
  • Static member (method \ variable) access mode
    • Object member
    • Class Member (recommended calling method)
  • be careful
    • static cannot be added before class
    • In member methods, you can directly access static members in a class
    • Static methods cannot directly access non static members in the same class, but can only directly call static members in the same class
    • If you must access, you can instantiate the class member
    • this cannot be used in static methods

6, Code block

  • In Java, {} is called a code block
  • Ordinary code blocks are executed in sequence. They appear first and execute first
  • Construction code block: called when creating an object, which takes precedence over the execution of the construction method
  • Static code block: static {...} When a class is loaded, calling takes precedence over Constructing code blocks. No matter how many instances are generated, static code blocks are called only once
class Code{
    //Create a building block of the Code class (building block)
    {
        System.out.println("Code Tectonic block");
    }

    //Create a static Code block (static Code block) of the Code class
    static {
        System.out.println("Code Static code block");
    }

    //Create the constructor of Code class (constructor)
    public Code(){
        System.out.println("Code Construction method of");
    }
}
public class CodeBlock{
    // Create building blocks for CodeBlock
    {
        System.out.println("CodeBlock Building blocks for");
    }

    //Create a static code block for CodeBlock
    static {
        System.out.println("CodeBlock Static code block for");
    }

    //Construction method for creating CodeBlock
    public CodeBlock() {
        System.out.println("CodeBlock Construction method of");
    }

   public static void main(String[] args){
       //Write code to test the running priority. See the effect drawing for the running results
       //CodeBlock cb = new CodeBlock();
        System.out.println("CodeBlock How to live");
        System.out.println("produce Code Class strength object");
        Code cd = new Code();
        System.out.println("produce CodeBlock Class strength object");
        CodeBlock cb = new CodeBlock(); 
   }
}
Program output:

CodeBlock Static code block for
CodeBlock How to live
 produce Code Class strength object
Code Static code block
Code Tectonic block
Code Construction method of
 produce CodeBlock Class strength object
CodeBlock Building blocks for
CodeBlock Construction method of

Be able to sit on the bench, endure loneliness and keep your original heart!

Topics: Java