1. Basic concepts of object Orientation

1. What is object-oriented

When it comes to object oriented, we have to mention process oriented, what is process oriented? If you have learned C language in college, you must be familiar with process oriented, or learned the first half of C Plus Plus, C Plus plus also has object oriented. Process-oriented: take steps as the unit to complete a specific thing step by step, object oriented: take objects as the unit to complete a certain thing by dispatching and combining different objects, object oriented is more advanced! Here’s an example: Xuan xuan won 600 dollars in the lottery, going to venture selling pancakes, all the process I do things I a person to rent three rounds, to buy the raw material, filling gas, pancakes, hand stand then have paid everything to myself, this process of entrepreneurship is a process oriented, but later I stand pancakes are delicious, very delicious, busy don’t come over, I began to recruit people, there are special money, special stand pancakes, special buy noodles, and special pedal three rounds… “, and then Xuan Xuan is responsible for controlling them, let them help me sell pancakes, Xuan Xuan dispatching the process of work is object-oriented! Object oriented is a kind of programming thought object oriented is a kind of thinking way of thinking about problems

2. Establish object-oriented thinking mode

First the whole, then the part first abstract, then concrete what can be done, what can be done

3. How to learn Object-oriented

Be familiar with the syntax of an object-oriented language be familiar with the principles of object-oriented design be familiar with about a dozen of the 23 object-oriented design patterns that are commonly used

2. Classes and Objects

What is a class?

1, category is: category, category. 2. By classification, we can distinguish different kinds of things. We often do this in our daily life. 3. Thus, a class is a collection of things that share the same properties (properties) and behaviors (methods). When we could classify ourselves by anything other than features and behaviors, we found none! Attributes and behaviors seem to sort everything into categories

The relationship between classes and objects

1, class represents a common product, is a comprehensive characteristics, and the object, is a product of personality, is an individual characteristics. 2, the class is composed of attributes and methods: attributes: is equivalent to a characteristic method: is equivalent to a human behavior, such as: talking, eating, singing, sleeping class is the category classification, the object is the class inside the instance, such as the classification of men, then I am an object! Another example: Xuan Xuan is an object, programmers are classes! The attribute of programmer is hair less, money more, words less, die early!

3, class and object definition format

You can define a class in Java using the following statements:

classThe class name{attribute name; Return value Type Method name (){}}Copy the code

Object definition: In order for a class to truly operate, it must rely on an object, which is defined in the following format:

Class name Object name =newClass name ();Copy the code

To generate an object in the format above, I go… It was so easy to get a date.. When you click new, you tut an object

If you want to access an attribute or a method (the definition of a method) in a class, you can rely on the following syntax:

Object. Property;Copy the code

Call a method in a class:

Object. Method ();Copy the code

In Java, object declaration has two meanings:

Horse horse= null;  ; 
// Indicates that an object is declared, but the object is not usable. Horse has no specific memory reference
Copy the code

Instantiate an object:

horse= new Horse() ;
// Indicates that the object is instantiated and can be used
// Call a method from an object:
horse.eat()

// An anonymous object calls a method:
new Horse().eat()
Copy the code

Example code:

public class Test8{
	
	public static void main(String[] args){	
	// Use of class
	Horse h=null;// Declare variables of a class (all variables are reference types except for the eight basic data types, including arrays)
	// Create a Horse object (instantiate an object)
	h=new Horse();
	// With an object, we can call its properties and methods
	h.name="Red Rabbit horse";
	h.age=350;
	h.run();// Call the method and the method is executed
	h.eat();
	// Anonymous objects: can only be used once and then released
	new Horse().eat();
	h=null;// Release the object
	// h.eat(); Calls to methods and properties of an object that does not exist will give a null pointer exception}}// Customize a class (type reference type), Horse class
class Horse{
	// Define attributes in a class
	String name;
	int age;
	// Define the method
	public  void run(a){
		System.out.println("I am"+name+", I"+age+"I can still travel a thousand miles a day.");
	}
	public void eat(a){
		System.out.println("I eat grass, and I eat a lot of it."); }}Copy the code

4. Object memory analysis

1. The new keyword indicates that an object is created. 2. The new keyword indicates that an object is instantiated

Note: if you use a memory space without application object, will be submitted to the null pointer exception: Java. Lang. NullPointerException objects in memory structure: Horse Horse = null;

horse = new Horse();


Assign values to attributes of an object:

Notice that the name in the heap it’s also the address, I’m putting it with the string just to make it easier to understand, the name is also the address and it’s referring to a memory space where the real string is stored.

Create multiple objects in memory:

Assign values to the properties of two objects:

Declare two objects, one instantiated and one uninstantiated

Assignment between objects:


Instantiate two objects separately:

Assignment between objects:

5, summary

  • The new keyword: applies for memory space. It also means to instantiate an object and create an object.
  • The memory size of an object, which is the sum of the memory size occupied by all the properties of the object. Reference type variables are 4 bytes on 32-bit systems and 8 bytes on 64-bit systems. Plus the size of the hidden data of the outside object.
  • The same type can be assigned
  • Different references to the same object. Any reference that changes the value of the object will be reflected by the other references.
  • A programming concern is to release objects as early as possible when you are sure they will not be used: reference =null
  • When an object in the heap is not referred to by any reference variable, it is considered a garbage object by the JVM’s GC program and thus collected.