Object oriented programming is not how it? Makes you bald!

Java in the core thing is object-oriented, we just put the object-oriented concept, and it also uses some of the syntax, have a good idea, that for us to learn anything behind Java, or even to learn other languages ​​when are hundreds of times ah! So it's still very, very critical role drop!

1, the basic concepts of object-oriented

1. What is object-oriented

Mentioned object-oriented, process-oriented we have to mention what it is process-oriented, if small partners learned C language at the University of time, then surely for process-oriented should be no stranger, or learned C plus plus the front half portion, c plus plus there is object-oriented. . Process-oriented: the step as a unit, step by step, to complete one specific thing, object-oriented: object-unit, by scheduling a combination of different objects to complete one thing, some of the more advanced object-oriented!
For example: Xuan Xuan 600 dollars in the lottery, intend to start a business selling pancakes, I do all the things I process a person to rent three, to buy raw materials, gas filling, personally pancakes, then collect the money every hand things have to be their own hands, the entrepreneurial process is process-oriented, but then I do share the pancake delicious, very delicious, busy, and I began to recruit people with the money of special, special pancakes, specializing in buy side, there are also special Dengsanlun. . . Then it will be responsible for Xuan Xuan dominate them and let them help me sell pancakes, Xuan Xuan manpower scheduling process is the work of the object-oriented!
Object-oriented programming is a kind of thinking
is a thinking object-oriented way of thinking

2, create an object-oriented way of thinking

First overall, then the local
first abstract, more specific
what to do, what to do

3, how to learn object-oriented

An object-oriented language to master syntax
familiar with object-oriented design principles
are familiar with object-oriented design patterns commonly used in about 23 dozen species

2, with the object class

What is class?

1, classes are: classification categories.
2, by classification, we can distinguish between different kinds of things in our daily lives, we often do so.
3, therefore, the class is a group of items with the same characteristics (attributes) and behavior (the method) is set. In addition to our characteristics and behavior, but also classified by other things, we found no! Properties and behavior seem to be able to put all things can be classified

The relationship between classes and objects

1, class represents a commonality of product, is an integrated feature, and the object is the product of a personality, an individual's characteristics.
2, the class attributes and methods:
* Attributes: the equivalent of one feature
· method: the equivalent of one person's behavior, such as: talking, eating, singing, sleeping
class is the class classification object is the class examples inside, such as the classification of men, that I'm a target! Another example is the Xuan Xuan objects, the programmer is class! Programmers property is less hair, more money, less then die early!

3, the object class and format definition

You can use the following statement to define a class in Java:

class 类名称{
	属性名称;
	返回值类型 方法名称(){}
}
复制代码

Definition of an object: a real class in order to operate, it must rely on the object, the object is defined in the following format:

类名称 对象名称 = new 类名称() ;
复制代码

According to the above format can produce an object, I went ... had put forward a target so easy to click on an object Kazakhstan ..new tut tut

If you want to access the class property or method (method definition), you can rely on the following syntax:
access class attributes:

对象.属性 ;  
复制代码

Call the class method:

对象.方法();  
复制代码

There are two meanings in java object declarations
declare an object:

Horse horse= null;  ; 
// 表示声明了一个对象,但是此对象无法使用,horse没有具体的内存指向
复制代码

Examples of objects:

horse= new Horse() ;
// 表示实例化了对象,可以使用
//通过对象调用方法:
horse.eat()

//匿名对象调用方法:
new Horse().eat()
复制代码

Code examples:

public class Test8{
	
	public static void main(String[] args){	
	//类的使用
	Horse h=null;//声明一个类的变量(变量 除了八种基本数据类型 都是引用类型,包括数组)
	//创建一个Horse类型的对象 (实例化一个对象)
	h=new Horse();
	//有了对象,我们就可以调用对象的属性和方法
	h.name="赤兔马";
	h.age=350;
	h.run();//调用方法 方法就被执行了
	h.eat();
	//匿名对象:只能使用一次 用完后就会被释放
	new Horse().eat();
	h=null;//把对象释放掉
	//	h.eat(); 当对象不存在时,调用改对象的方法和属性会报空指针异常
	}	
}
//自定义一个类(类型 引用类型) Horse 类
class Horse{
	//在类中定义属性(特征)
	String name;
	int age;
	//定义方法
	public  void run(){
		System.out.println("我是"+name+",我"+age+"岁了,我还能日行千里");
	}
	public void eat(){
		System.out.println("我吃仙草,还吃得多");
	}
}

复制代码

4, Object Memory Analysis

1, new keyword to create an object represents
2, new keyword indicates instantiate an object
3, new new keyword indicates application memory space

Note: If an application is not the target memory space, will be reported null pointer exception: java.lang.NullPointerException
object structure in memory:
Horse Horse = null;

horse is stored as address = null So now there is no address horse

horse = new Horse();

The default value of the string is null, a default value is 0 int;
to the subject property assignment:
Note that in the name of heap memory is also kept its address, so I put it in order to facilitate understanding and put together a string, name is also stored address it points to a memory space, this space is really small deposit string White.

Create multiple objects in memory :

It was assigned to the properties of two objects:

Declare two objects, one instantiation, a not instantiated

Assignment between objects:

horse2 not instantiated, but the assignment horse1 to horse2, the assignment process is that the horse1 address to the horse2, so now is horse1 and horse2 point to the same piece of memory, so now the horse2.name = "Hey", you horse1 also print out the name equal hey
are examples of two objects:

Assignment between objects:

This assignment process and the top is the same, not the same as the original horse2 at the memory, because there is no point being, so it will be considered garbage gc will be freed, what is it gc, gc is the virtual machine garbage Collection garbage collector

5. Summary

  • the new keyword: application memory space to express, also said instantiate an object, create an object.
  • A size of the object in memory, the sum of all the properties of the object occupied by memory size. Reference type variable is four bytes in the 32-bit system, 8 bytes on the 64-bit system. Plus out hidden objects share data size.
  • The same type can be assigned only
  • Different reference point to the same object, a reference to any change in value of an object, other references will be reflected.
  • When programming problem is to be noted, is not used in determining the object, the object to be released as soon as possible: = null reference
  • When a heap object is not pointed to any reference variable , the object will be the JVM GC program considered garbage objects, so as to be recovered.

Guess you like

Origin juejin.im/post/5e19c67b6fb9a02fcf18d778