Java Learning: Object-oriented use and precautions

 Object-oriented use and precautions

Process-oriented: When the need to implement a function of each specific steps need to force the pro detail every detail
of object-oriented: When the need to implement a function of the specific steps do not care, but to find that already has a people function, to help me do things.

The relationship between classes and objects

  1. Class is a description of a class of things, is abstract.
  2. Objects are instances of a class of things, it is specific.
  3. Class is a template object, the object is an instance of the class.

Define a class, used to simulate the "students" of things. Of which there are two components:

Property (what is):

  • Full name
  • age

Behavior (what to do):

  • eat
  • go to bed
  • Learn

Which correspond to Java classes :
member variables (attributes):

Name String; // Name 
int Age; // Age

Member method (behavior);

public  void EAT () {} // eat 
public  void SLEEP () {} // sleep 
public  void Study () {} // Learning

Precautions:

  1. Member variable is defined directly in the class methods to the outside among.
  2. Do not write member method static keyword

Creating and using objects

Typically, a class can not be used directly, we need to create an object based on the class to use.

1. guide package: that is the class that you want to use, in what position.

import package name class name.;

For this category belongs and the same package case, the leader packet may be omitted sentence is not written.

2. Create, format:

Name of the object class name = new new class name ();
Student stu = new Student();

3. Use, divided into two situations:

Use member variables: object name member variables.
Use member method: Object members of the method name (parameters)
(That is, who would like to use it with a little who object name.)

Precautions:

  • If a member does not carry out an assignment, then there will be a default value, and an array of different rules.
  • When an object is passed as a parameter to which method: in fact, the value of the address is passed in the object.
  • When using an object type as the return value: The return value is actually the address value of the object.


Local variables and member variables difference:

1. Define the position is not the same [focus]

  • Local variables: the method of internal
  • Member variables: the external method of directly writing them in class

2. The scope is not the same [focus]

  • Local variables: The only way which can only be used, the method can not be used again
  • Member variables: the whole class can be common

3. The default value is not the same as [focus]

  • Local variables: There is no default value, if it is to use, you must manually assign
  • Member variables: If there is no assignment, there will be defaults, rules, and the same array

4. The memory locations differ (learn)

  • Local variables: Located in the stack memory
  • Member variables: Located heap memory

The life cycle is not the same (to know)

  • Local variables: With the push method and the birth, along with a stack method disappear
  • Member variables: born with the creation of the object, as the object is garbage disappear

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

Encapsulation of Java which is reflected in:

  1. A method of packaging is
  2. Keyword private is also a package

Packaging is to hide some of the details, not visible to the outside world.

Problem Description: When defining the age of Person, can not prevent unreasonable value is set to come in.
Solution: The private key will need to be protected member variables modified

Once modified by the private, then this class which can be freely accessed any course.
But: beyond the beyond the scope of this class can no longer directly accessible.

Indirect access private member variables, is the definition of a pair of children Getter / Setter methods

Precautions:

  • It must be called setXxx or getXxx naming rules.
  • For Getter, the parameters have not, return type and the corresponding member variable;
  • For Setter, you can not return a value, parameter types, and the corresponding member variable.


this keyword is defined:

When the class member variables and local variables of the same name of the method, in accordance with "the principle of proximity" preferentially use local variables.
If you need to access the member variables of this class which requires using the format:

the this . member variables
"By the method who called, who is this.

Construction method:

Constructor method is designed to create an object, when we create an object by keyword new, in fact, and then call the constructor.

Format:
 public class name (parameter type parameter name) {
The method body
}

Precautions:

  1. And the name of the constructor must be exactly the same as the name of the class, and even have the same case
  2. Do not write constructor return type, not even void do not need to write
  3. Constructor does not return a specific return value
  4. If you do not write any constructors, the compiler will default presented a constructor, with no parameters, the method body does nothing at all
public Student(){}

  5. Once you've written at least a constructor, the compiler will no longer presented.

  6. The method of construction also can be overloaded.

Overloaded: The same method name but different parameter list.


A standard class usually have the following four components:

  1. All member variables must be modified using the private key
  2. Write Getter / setter methods one pair for each member variable
  3. Writing a no-argument constructor
  4. Write a full-constructor parameters

Such a standard class, also known as Java Bean

Standard class:
 public  class Student {
    
    Private String name; // Name 
    Private  int Age; // Age
    
    public Student () {
         // constructor with no arguments 
    }
     public Student (String name, int Age) {
         the this .name = name;
         the this .age = Age;
    } // There constructor parameters
    
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    } // Name
    
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age = age;
    } // Age 
}
    
//==================//
public class CaiNiao{
    
    public static void main(String[] args){
        Student stu1 = new Student();
        stu1.setName ( "Rookie" );
        stu1.setAge(18);
        System.out.println ( "Name:" + stu1.getName () + " , Age:" + stu1.getAge ());
         // The second method 
        Student STU2 = new new Student ( "Legend", 28 );
        System.out.println ( "Name:" + stu2.getName () + " , Age:" + stu2.getAge ());
        stu2.setAge ( 22); // modified 
        System.out.println ( "Name:" + stu2.getName () + " , Age:" + stu2.getAge ());
        
    }
}
    

 








 

Guess you like

Origin www.cnblogs.com/cainiao-chuanqi/p/11073986.html