Java foundation ten (classes and objects)

Classes and Objects

We know that objects, on behalf of an entity can be clearly identified in the real world (all things are objects), each object has its own unique identity, state and behavior.
Class is something collectively have similar characteristics and behavior of. Class used to define a generic object of the same type. Class is a template or blueprint is a contract that defines the object's data fields and methods is what is doing. An object is an instance of a class. Multiple instances can be created from a class. Create an instance of a process known as instantiating the object instance and is often interchangeable.

NOTE : The main functions are not required to create the object and to be called directly (manufactured by the JVM)
main program and only the main entrance, where the main function of the type generally referred to as the main class.

Classification class

  • Main categories: primarily responsible for running the program does not need to be responsible for the need to describe specific things
  • Entity classes: mainly responsible for describing things
  • Tools: Generally not create the object calling the function (static) directly through the class

Here are several common is that we are more familiar with a few tools

Math.abs() Math.random()
Arrays.toString();
System.out.println();

The relationship between objects and classes : class is the definition of an object, the object created from the class. For example, a dog is a class, a generic term for all dogs and husky dog is an entity object of this class.

How to define a class
mainly define its characteristics and behavior

  • Features: Variable
  • Behavior: Functions

The definition of a dog category, features the names, sex, age, height, weight and hair color, define six member variables to store, eating and sleeping behavior, define two member function to perform.

class Dog{
    //直接定义在类中的变量 成员变量
    姓名 String name;
    性别 String sex;
    年龄 int age;
    身高 double heigth;
    体重 double weight;
    毛色 String color;
    
    //成员函数(非static函数)
    吃   public void eat(){... return;}
    睡   public void sleep(){... return;}
}

Husky create objects

public class Mian{
    public static void main(String[] args) {
        Dog Huskies=new Dog();
    }
}

Process object created
1, the main function of advanced stack.
2, the right execution of the statement new Dog (), to open up a space in the heap memory, randomly assigned address.
3, in the object space to create space for member variables and member variables are initialized by default, create member function (not in the object space, is no longer the stack, call stack).
4, create a stack of local variables in the function, is stored in the address of the object in the heap memory.

The difference between local variables and member variables of
different definitions of location: a difference

  • Member variables defined in the class
  • Local variables are defined in the process or statements which

Difference between the two: a different location in memory

  • Member variables are stored in the heap memory object
  • In the method of local variables are stored in the stack memory

Three differences: different lifecycle

  • With the advent of the member variable objects out of the heap now, with the disappearance of the object disappear from the heap
  • With the local variables of the method now run out of the stack, with the popping method disappear

Distinguish four: different initialization

  • Because the member variables in heap memory, all the default initialization value
  • Local variables are not the default initialization values, you must manually assign it a value before you can use
Published 70 original articles · won praise 56 · views 2003

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/103283642