Object-oriented knowledge points summary (a) java language

Classes and Objects

The definition of class

   A class definition, can contain the three most common members: constructor, member variables and methods.

Member variable : Status data definition for the class or instance of the class contains
a method : for instance define the behavior characteristics of the class or class of functions or implement.
Constructor : for instance constructor of the class, Java language to call the constructor by the new keyword, which returns an instance of the class. If you do not write a constructor for the class, the system will provide a default constructor for the class.

Member variables Definition Format

[修饰符] 类型 成员变量名 = [默认值]

Modifiers: public, protected, private, static, final, in which the public, protected, private three one of the most frequent, can be static, final combined modified member variable.

Type: Basic type + reference type

Method definition format:

[修饰符] 方法返回值类型 方法名(形参列表){
    //由零到多条可执行性语句组成的方法体
}

Parameter list: the parameters used to define acceptable methods, who is who is responsible for calling the method parameter assignment.

Object generation and use

   Is the fundamental way to create an object constructor to call the constructor of a class can be created by the new keyword.

Person p = new Person();

Object Role:
   access to the object instance variables
   call the object's method
   if allowed access, methods and member variables defined in the class can be invoked through the class or instance, the syntax is as follows:

.类变量|方法
//访问p的name实例变量,直接为该变量赋值
p.name = "李刚";
//调用p的say()方法,声明say()方法时,定义了一个形参
//调用该方法必须为形参指定一个值
p.say("java语言很简单")//直接输出p的name实例变量,将输出李刚
System.out.println(p.name);
Objects, references and pointers

Execute Person p = new Person ();
This line of code creates a Person object, the Person object is assigned to variables p.
In this line of code actually produces two things: one is the variable p, is a Person object.
Here Insert Picture Description
   When an object is created, this object will be stored in the heap memory, the object can only operate by reference to the object. As shown, p reference variable itself only stores an address only, it does not include the actual audit data, but it points to an actual Person object, when the member variables and methods to access reference variables p, in fact, is to visit the referenced p member variables and methods of an object.

   If the memory is no object heap variable points to the object, then the program will no longer be able to access the object that will become garbage, java garbage collector will reclaim the object, release the memory area occupied by the object. If you want to inform the garbage collector to an object, the object is simply cut all references to the relationship between variables Jike and it is to these references is variable assignment to null.

this object reference

this keyword always points to the object that calls the method.
this as the default reference to an object There are two cases:
   the object constructor is initializing the constructor references 1.
   2. reference object calls this method in the method in
this keyword biggest role is to make a class method, visit another method in this class or instance variables.
For example as follows:

public class Dog{
    public void jump(){
        System.out.println("正在执行jump方法")}
    public void run(){
       //使用this引用调用run()方法的对象
        this.jump();
        System.out.println("正在执行run方法")}
}

public class DogTest{
    public static void main(String[] args){
        Dog dog = new Dog();
        dog.run();
    }
}

   If you need an object object calls this method in the run () method, the this keyword to meet this requirement, this can represent any object, when this occurs in a method body, it represents is not determined, but the type is determined, represent only the current instance of the class; only when this method is called, was only the object it represents the determined, who call this method, this on behalf of anyone.
   staitc modified method can not use this keyword. Because static method call is the class itself, rather than the instance of the class, and therefore can not use this points to a valid object.

   If the method has had a member variable of the same name and local variables, and the program needs to access the member variables covered in this method, you must use this prefix, in addition, this reference can also be used as the default constructor application Since the constructor is invoked using the new keyword directly instead of using the object to call, so this object representing the constructor is initialized in the constructor.
For example as follows:

public class ThisInConstructor {
    //定义一个成员变量
    public int foo;
    public ThisInConstructor(){
        int foo = 0;
        this.foo = 6;
    }
    public static void main(String[] args){
        System.out.println(new ThisInConstructor().foo);
    }
}

   The sixth line of code being executed ThisInConstructorfoo object initialization of member variables to 6, foo member variables for all objects means that the constructor returns are equal to 6.

   When this default object as a reference to the use of the program can visit as ordinary as reference variable to access this this quote, as this can even ordinary method's return value. If this method as a return value in one, you can call the same method multiple times in a row.
Examples are as follows:

public class ReturnThis {
    public int age;
    public ReturnThis grow(){
        age++;
        return this;
    }
    public static void main(String[] args){
        ReturnThis rt = new ReturnThis();
        rt.grow().grow().grow();
        System.out.println("rt的age成员变量值是"+rt.age);
    }
}
Published 16 original articles · won praise 12 · views 162

Guess you like

Origin blog.csdn.net/DIDI___/article/details/104076554