JAVA Basic Packaging

Posted by nicx on Fri, 02 Aug 2019 05:48:38 +0200

encapsulation

Object-oriented has three characteristics: encapsulation, inheritance and polymorphism.

encapsulation

Packaging performance:
Method is a basic package.
Class is also an encapsulation.
From the above two points, it is concluded that the benefits of packaging are:
1. Enhanced code reusability.
2. Hide the implementation details and provide access to the outside world.

3. Facilitate the use of callers. This is one of the core concepts. It can also be understood as the concept of encapsulation.
4. Enhanced security.

Private private ownership

 

private: Privatization;

private-modified methods and attributes can only be used in this class; other methods (main) cannot be invoked and accessed;
Because private ly modified attributes and methods cannot be used by other classes, write methods are required:

Assignment set and call get; assignment of private attributes.

Calling private methods

 

 

Code examples


Understanding the embodiment of encapsulation in life, but also back to Java, to elaborate on the embodiment of encapsulation in Java code, starting with the description of Person.
Describe people. Person
Attribute: Age.
Behavior: Speak: Say your age.

                  publicstaticvoid main(String[] args) {

             perso p = new perso();

                     /*Person p=new Person();

                      *p.speak()Because the method speak() of input statement is written in Person class.

                      * So call it directly here; if you don't write the System output statement*/
                      *  You need to write System.out.println(p.getName()+"..."+p.getAge()) (=p.speak);

                      *  Transfer private Attribute Requirements for Modification:*/

                                           //  Variable name. getName();
                           // Biography
                           p.setName("rapid strike");
                           // Biography
                           p.setAge(23);
                           perso p1 = new perso();
                           // Biography
                           p1.setName("Rigid cut");
                           // Biography
                           p1.setAge(24);
       /*
        * p.speak(); System.out.println(p.getName()+"..."+p.getAge());
        */

}

A serious problem is that the behavior of attributes in Person can be accessed and used arbitrarily. This obviously does not meet the actual needs.
The keyword in Java that needs to be used is also a modifier private (private, permission modifier). As long as Person's attributes and behaviors are private, they cannot be accessed directly.

 

class Person {
    //Membership variables
      private  int age;
      private String name;

      public  void speak() {

       System.out.println("My name is" + name + ",This year" + age + "Years old.");

}

 



Age has been privately owned, the wrong value can not be assigned, but the correct value can not be assigned, add judgment.

General access actions to member attributes:

Assignment (set)

Get a value (get)

Therefore, the way private variables are accessed can provide corresponding setXxx or getXxx methods.

 

The method of variable name. set (value) is used to assign private ly modified attributes.

class Person {
    // Private Membership Variables
               // Adding Private Ownership
    private String name;
    // Adding Private Ownership
    privateintage;
    // Providing external methods for setting member variables
publicvoid setName(Sring n){
name=n;
         /*name=n:Is equal to this.name(this.name is the name of the member variable is private String name)=n
(n It is the value to be assigned by the instantiated object.
*/ }
//A Method of Providing Access to Membership Variables Externally
    public String getName() {
       // The caller calls the method, and the method is returned to the caller.
       returnthis.name; }

 publicvoid setAge(int a) {
        // Since the value of the member variable is set, data validation can be added here.

        if (a < 0 || a > 130) {
           System.out.println(a + "Age-inconsistent data range");
            return;
        }
          age = a; }
    // A Method of Providing Access to Membership Variables Externally
    public  void getAge() {
        returnage;//// The caller calls the method, and the method is returned to the caller.
    }
}

 

 


Summary:
The content that does not need to be provided to the outside world in a class is privatized, including attributes and methods.
Describe things later, privatize attributes, and provide setXxx get Xxx methods to access them.
Note: Private ownership is only a form of encapsulation.

 publicclass demo01 {
    public static void main(String[] args) {
        perso p = new perso();
        // Biography
        p.setName("rapid strike");
        // Biography
        p.setAge(23);
        perso p1 = new perso();
        // Biography
      p1.setName("Rigid cut");
        // Biography
        p1.setAge(24);
        /*
         * p.speak(); System.out.println(p.getName()+"..."+p.getAge());
         */
        //Biography
        boolean flag = p.compare(p1);
        //Printing of Return Value
        System.out.println(flag);
    }

}
public boolean compare(boolean p){
return this.age=p.age;

}
1. stay main The method of invocation is boolean flag=p.compare(p1);So the return value is boolean So here it is. compare()The method is useful   
boolean meet;In fact, it is.:stay main Method p Called Persong In the class compare()Method;
2. Method return this.age=p.age;The return value for comparison is boolean type;

 

publicclass perso {
    // Adding Private Ownership
    private String name;
    // Adding Private Ownership
   privateintage;
    // Exit
    publicvoid setName(String name) {
      this.name = name;
    }
    // Returns a return value
    public String getName() {
        // The caller calls the method, and the method is returned to the caller.
        returnthis.name;
    }
    // age Assignment has return value
    publicvoid setAge(intage) {
        this.age = age;
    }
    // Value has return value
    publicint getAge() {
        // The caller calls the method, and the method is returned to the caller.
        returnage;}
    // Judgment of peers
    publicboolean compare(perso p)

        returnthis.age == p.age;}



this.age refers to p.age;p.age refers to p2.age (Person object P2 is defined in main method); P2 object is passed to Person class comparison method for comparison by comparison method; then the result false is returned to the method called by p.compare() object in main; the return value is boolean;
this.age is the age of the invoked object, that is, p.age; = p.age is the age of the comparee; (the whole p object, including the age attribute) is passed through the comparison (Personp); the age of = p.age is assigned and compared by the age of the P object passed in.
boolean flag=p.compare(p2); / / here is the comparison method of tones, the return value is boolean type, so the return value of compare() method in Person class is boolean joined;

 

Topics: PHP Java Attribute