Java self-study notes (10): classes and objects [object-oriented], constructor, this keyword

Classes and Objects

  When Java object-oriented development language, you first need to define a legal category, depending on the object and then use this to create a legal class, which is stored data.

How to define a class?

[Access specifier] [Modifier] class class name {

  [Access specifier] [Modifier] datatype property 1;

  [Access specifier] [Modifier] 2 datatype property;

  [Access specifier] [Modifier] 3 Data type attribute;

  ········

  method 1;

  Method 2;

       ········

}

Java class method to describe the dynamic properties of the entity

public  class Person { 
    String name; 
    int Age;     // member variable 
    void say () { 
        System.out.println ( "say Method" ); 
    }    // member method 

}

Creating objects

  A class name reference variable name;

  Reference variable name = new class name ();

Can be combined into    a class name reference variable name = new class name ();

Person p =new Person();

Access object member variables and member methods

  A reference variable name property name; // here is to take a member of character

p.name = "exaggerate~";
p.age = 17;
p.say();

Construction method

Each class has a special method called constructor

1, the same as the class name and method name

2, an object instance of the class is automatically called when the new

3, excluding the return value type

class the Person { 
    the Person () { 
        System.out.println ( "This is the constructor" ); 
    } 
}

4, the configuration method may also reload


this keyword  : Indicates the current instance of the object being referenced, members can access the object

1, when the member variables with the same name as a local variable, you can use this. Variable name to call the member variable, or default access local variables

2, you can use this (); call the constructor, but must be written in front of all statements

 

Guess you like

Origin www.cnblogs.com/tkj521Ya/p/11220548.html