Packaging knowledge combing

Posted by aaronlzw_21 on Thu, 09 May 2019 21:46:02 +0200

Our daily computer mainframe encapsulates cpu, memory, motherboard and so on into the chassis. If there is no chassis, then there is no boot button, then we need to operate the jumper directly to turn on the computer. In this way, if the operation is not careful, the machine will be damaged and dangerous, then if encapsulated with the chassis, then there is no need to do so. It embodies the encapsulation - Security characteristics.
If you take the computer to add memory, you can give it directly to the person who repairs it. After he has added memory, you can give it to the person who repairs it. You got the same cabinet. You don't know what's going on inside. The second benefit of encapsulation - isolating change.

Providing a boot button in the cabinet without requiring you to use jumper to boot directly embodies the encapsulated and easy-to-use feature.

As long as the chassis provides a boot function, then no matter where the chassis goes, you can use the boot function. It embodies the encapsulated - providing repeatability.

I. No Packaging

Simulation problem
1. Describe the Employee class. Define name, work number, gender member variables, and work methods. Members are decorated with public.
2. Create Employee objects, and assign values in the form of objects and members. Finally, the object calls the working method.
3. Summary: If you do not use encapsulation, it is easy to assign errors, and anyone can change, resulting in information insecurity.
4. Problem solving: using encapsulation

package oop01;

public class EmployeeDemo {
    public static void main(String[] args) {
        // create object
        Employee jack = new Employee();

        // Members are invoked in the form of class names. Members. Initialize instance variables
        jack.name = "jack";
        jack.id = "123456";
        jack.gender = "male";

        // Call member methods
        jack.work();
        System.out.println();

        // Input illegal parameters
        jack.gender = "Not a man";
        jack.work();

    }
}

class Employee {
    String name;
    String id;
    String gender;

    public void work() {
        System.out.println(id + ":" + name + ":" + gender + " Work hard!!!");
    }
}

2. Implementation of Packaging

1: Set the property of the class to private (keyword), you can not use the object name. Attribute name to access the property of the object directly.

package oop01;

public class EmployeeDemo {
    public static void main(String[] args) {
        // create object
        Employee jack = new Employee();

        //Compile and report errors
        jack.name = "jack";
        jack.id = "123456";
        jack.gender = "male";



        // Compile and report errors
        jack.gender = "Not a man";
        jack.work();

    }
}

class Employee {
   //private is used to modify member variables
    private String name;
    private String id;
    private String gender;

    public void work() {
        System.out.println(id + ":" + name + ":" + gender + " Work hard!!!");
    }
}       

Question:
1: Why was it accessible by object name and attribute name before?
2: The public member modifier is accessible to anyone who is public.
3: Private member modifiers, private, accessible only by themselves.

Modify the gender modifier of the Employee class to private
1: Compilation failed
2: private-modified members can be used in their own classes, but not outside.
3: After the gender modifier of Employee class is changed to private, it can't be called out of class. So how to set the value of gender?
1: Provide an open public method for setting object attributes
1: set up
2: Get get
2: Logical judgment is added to set method to filter out illegal data.
3: Encapsulate all member variables with private to provide get and set methods

package oop01;

public class EmployeeDemo {
    public static void main(String[] args) {
        // create object
        Employee jack = new Employee();

        // Call public methods to assign values to member variables.
        jack.setId("007");
        jack.setName("jack");
        jack.setGender("male xx");

        // Get the value of the instance variable
        System.out.println(jack.getGender());
        System.out.println(jack.getId());
        System.out.println(jack.getName());

        // Call member methods
        jack.work();

    }
}

class Employee {
    private String name;
    private String id;
    private String gender;
    // Provide a public get set method
    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    public String getId() {
        return id;
    }

    public void setId(String i) {
        id = i;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gen) {
        if ("male".equals(gen) || "female".equals(gen)) {
            gender = gen;
        } else {
            System.out.println("Please input\"male\"perhaps\"female\"");
        }

    }

    public void work() {
        System.out.println(id + ":" + name + ":" + gender + " Work hard!!!");
    }
}   

3. Benefits of Packaging

1: Hidden the concrete implementation of the class
2: Easy to operate
3: Improving the security of object data

IV. Examples of Packaging

Example: Describe a calculator class

/**
 Demo9 Describes a calculator class.
 */
// 0. Use Word Hegemony to Determine Class Names
class Calculator
{
    // 1. View specific calculator objects to extract common properties of all calculators
    public String name = "My calculator is my master";
    public double num1;
    public double num2;
    public char option;
    // 2. View specific calculator objects to extract common functions of all calculators
    // 2.1 Define the function of accepting data
    public void init( double a , char op , double b  ){

        num1 = a;
        option  = op;
        num2 = b;
    }
    // 2.2 Define the Functions of Computing
    public void calculate(){

       switch ( option )
       {
       case '+': System.out.println(  name + " : " + num1 + " + " + num2 + " = " + ( num1 + num2 ) );
                 break;
       case '-': System.out.println(  name + " : " + num1 + " - " + num2 + " = " + ( num1 - num2 ) );
                 break;
       case '*': System.out.println(  name + " : " + num1 + " * " + num2 + " = " + ( num1 * num2 ) );
                 break;
       case '/': {
                 if( num2 != 0 ) 
                    System.out.println(  name + " : " + num1 + " / " + num2 + " = " + ( num1 / num2 ) );
                 else
                    System.out.println("The divisor cannot be zero!");
                 break;
                            }
       case '%': {
                 // 1. Processing the symbolic problem of the result so that the symbols of the result meet the requirements of mathematics.
                 // 2. Solving the problem of NaN
                 System.out.println(  name + " : " + num1 + " % " + num2 + " = " + ( num1 % num2 ) );
                 break;
                 }
       default : System.out.println("You're making trouble. I ignore you. I'm mad at you.......");

       }

    }
}
class Demo9 
{
    public static void main(String[] args) 
    {

        Calculator  cal = new Calculator();
        cal.init( 41 , '%' , 0 );
        cal.calculate();
        System.out.println("Completed calculation!Once more......");
    }
}

Topics: calculator Attribute