Java Foundation object-oriented objects and the like
2017-01-14
1, the basic concept of object-oriented
Code design in a form of organization
1) The following features are included in object-oriented programming
? Encapsulation: Protecting the security of the internal structure
Inheritance: Extending new functionality on existing program structures
Polymorphism: Within a certain Niang range to meet
2) Object-oriented development is divided into the following steps:
OOA (Object-oriented analysis)
OOD (object oriented design)
OOP (Object-oriented programming)
2. Classes and objects
1) Understanding the relationship between classes and objects
? recognize classes and objects
Classes and objects are the most basic components of an object, and a class is a set of commonalities, and an object is a product of one body.
For example:
Human (name, age, work, besides these attribute classes also include some methods such as eating, playing, etc.)
Object (object is specific to a person)
A class is actually a template for an object operation, but the class cannot be used directly and must be instantiated by instantiating the object to use
2) Mastering the definition structure of classes and objects
? basic definitions for classes and objects
A class needs to contain properties and methods
To define a class:
1 Public classClassDemo1 {//define a class2 PublicString name;//the Name property in the class3 Public intAge;//The Age property in a class4 Private voidEat () {//the Eat () method in a class5System.out.println ("I'm Eating");6 }7}
Class has now been generated, but if you want to use a class, you have to instantiate the object.
Two ways to instantiate an object:
Declaration and instantiation of an object: Class Name Object name = New class name ();
? Distribution Complete:
Class Name Object name = NULL;
Object name = New class name ();
The biggest difference between the reference data type and the basic data type is that the reference data type requires memory to be opened up and used, the keyword new is to open up memory space, as long as the reference data type want to use, you must use the keyword new to open up memory space
Use class:
Object. Property: Represents the contents of a Class Action property
Object. Method (): Represents the contents of a class action method
1 classperson1{2 PublicString name;//the Name property in the class3 Public intAge;//The Age property in a class4 Public voidEat () {//the Eat () method in a class5System.out.println ("I'm Eating");6 }7 }8 Public classClassDemo1 {//define a class9 Public Static voidMain (String args[]) {TenPerson1 P1 =NewPerson1 ();//declaring and instantiating an object OneP1.eat ();//call the Eat () method in the Person1 class A } -}
Analysis of the above procedures:
Heap Memory: Saves the object's property content, typically using the new keyword to open up heap memory space
Stack memory: The address of a heap of memory is saved, from the strictly speaking of memory is the address of the object, but a simple understanding of the stack memory is the name of the object is saved.
An java.lang.NullPointerException (null-pointing) exception occurs if only objects are declared and no objects are instantiated. This exception is possible as long as the reference data type is present.
3) Master the memory analysis of the object
Preliminary analysis of reference data
object's Reference passing
1 classperson2{2 PublicString name;//the Name property in the class3 Public intAge;//The Age property in a class4 Public voidEat () {//the Eat () method in a class5System.out.println ("Name:" +name + "age" +Age );6 }7 }8 Public classClassDemo2 {//define a class9 Public Static voidMain (String args[]) {TenPerson2 P2 =NewPerson2 ();//declaring and instantiating an object OneP2.name= "Zhangbojie" ; AP2.age = 20 ; -Person2 P3 =NULL ; -P3 = p2;//Pass the P2 reference to P3 theP3.name = "Zhang Bojie";//P3 modified the name and age -P3.age = 21; -P3.eat ();//call the Eat () method in the Person2 class - } +}
At this point two objects point to the same piece of memory space, so any object that modifies the contents of the property affects the other object. During a reference operation, a heap of memory can be indexed by multiple stacks of memory. In turn, however, a stack of memory can only hold the address of a heap of memory.
Continue observing reference passing
1 classperson2{2 PublicString name;//the Name property in the class3 Public intAge;//The Age property in a class4 Public voidEat () {//the Eat () method in a class5System.out.println ("Name:" +name + "age" +Age );6 }7 }8 Public classClassDemo2 {//define a class9 Public Static voidMain (String args[]) {TenPerson2 P2 =NewPerson2 ();//declaring and instantiating an object OnePerson2 P3 =NewPerson2 (); AP2.name= "Zhangbojie" ; -P2.age = 20 ; -P3 = p2;//Pass the P2 reference to P3 theP3.name = "Zhang Bojie";//P3 modified the name and age -P2.eat ();//call the Eat () method in the Person2 class - } -}
Through the analysis of memory can be seen, in the reference data relationship, a piece of heap memory points without stack memory space, will become garbage, all garbage will be garbage collection mechanism (GC) is not regularly recycled, after recycling will release its occupied memory.
Summarize:
1, the class describes the public characteristics, the class must be instantiated to use the object.
2, heap memory is the contents of all properties, and stack memory is the address of the heap memory is saved.
3. Garbage space refers to the heap memory space that is not referenced by the stack memory space.
Java Foundation object-oriented objects and the like