Self-study package of Java day06_Java

Packaging concept

Outline
Object-oriented programming language is a simulation of the objective world, the objective world, member variables are hidden inside the object, the outside world can not directly manipulate and modify. Package may be considered as a protective barrier against the class codes and other types of data are accessed randomly. To access this kind of data, you must specify the way through. Appropriate encapsulation makes the code easier to understand and maintain, and enhance the security of the code. Principles: the hidden attribute, if the need to access a property, providing public access to its methods.

Embodied in the encapsulation among the Java

  • A method of packaging is
  • Keyword private is also a package

Step package

  1. Use keywords to modify private member variables.
  2. Member variables need to access, provides a pair of getXxx method, setXxx corresponding method. It can be generated automatically by IDEA

Package key operation --private

private meanings
  • private modifier is a privilege, on behalf of least privilege.
  • Member variables and member methods can be modified.
  • The member variables and member methods after private modification, only be accessed in this class.

--This keyword optimization package

this meaning
references the current object of this class represents the location (address value), that is their object references . Remember: which object method is called, this method represents that object. That is who to call, who this represents.
Use this format:

Optimization Package - Constructor

When an object is created when the method is used to initialize the object constructor, the object member variable to the initial value assigned.

Definition Format constructor

Note: the wording of the constructor, method name with it in the same class name. It does not return a value, so no return type, not even need to void.

Standard Code --JavaBean

JavaBean is a Java standard specification written in a language class. JavaBean meet class requirements and specific class must be public, and constructor with no arguments, to provide a method for operating a set and get member variable. As shown below

demand

Prepared in accordance with standard JavaBean class, student class, for example
Code
JavaBean specifications in line with the definition of the Student class
Package demo05; 

public  class Student {
     // member variable 
    Private String name;
     Private  int Age; 

    // Constructors 
    public Student () { 
    } 

    public Student (String name, int {Age)
         the this .name = name;
         the this .age = Age ; 
    } 

    // member methods 
    public  void the setName (String name) {
         the this .name = name; 
    } 

    public String getName () {
         return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}

Use Stuent class

Package demo05; 

public  class StudentTest { 

    public  static  void main (String [] args) {
         // constructor with no arguments using 
        Student = S new new Student (); 
        s.setName ( "Liu Yan" ); 
        s.setAge ( 18 is ); 
        the System. Out.println (s.getName () + "---" + s.getAge ()); 

        // parameterized configured using 
        Student S2 = new new Student ( "Zhao Liying", 18 is ); 
        System.out.println (S2. getName () + "---" + s2.getAge ()); 
    } 
}

Results of the

 

 

Guess you like

Origin www.cnblogs.com/wurengen/p/11565813.html