Directory one, the concept of objects and classes two, the relationship between the class three, the object and the reference four, the definition of Java class Five, the constructor 1. How to construct a function 2. Overloading of methods and memory resolution 1) methods
The basic idea of object-oriented is to construct the software system from the reality of the real world, and to use the natural way of thinking of human in the structure of the system as much as possible.
Object-oriented emphasizes the use of methods and principles commonly used by humans in everyday thinking logic, such as abstraction, classification, integration, aggregation, polymorphism, etc.
I. Concepts of objects and classes
Object uses computer language to describe the things in the problem domain, and the objects have their own static and dynamic properties by means of "attribute (attribute)" and "method".
A class is an abstract concept used to describe an object of the same type, which defines the static and dynamic properties of this class of objects
A class can be called a template for a class of objects, and an object can be seen as a concrete instance of
Ii. relationship between Classes 1. Association relationship (The weakest relationship in Java programming) 2. Inheritance (a stronger relationship)
A class may inherit from more than one class, so C + + has multiple inheritance but it is cumbersome to handle duplicate functions in different variables.
For example:
Swimming cloud Mobilization is athlete (general and special)
Football players are ball players, but also the type of athletes, this relationship is called the inheritance relationship
3. Aggregation relationship (whole and part)
For example:
The captain is a part of the team and the team is part of the squad (gathering)
The body and limbs of the head constitute a human being, an inseparable relationship (combination)
4. Implementing the relationship 5. Polymorphism
Iii. objects and references
object is the core of a Java program, in a Java program "Everything is Object"
objects can be called packages of static attributes (member variables) and dynamic properties (methods)
A class is a "template" used to create an object of the same type, defining the member variables and methods that the class object should have in a class
A set of objects that interact with each other through communication to accomplish a particular function is a reference to an object
Variable types other than level types in the Java language are referred to as reference types
The need I love in Java is manipulated by reference, for example:
Iv. Definitions of Java classes
Let's look at an example:
- The definition of a class consists of two main components: member variables and methods.
- The declaration member variable format is: [<modifiers>] type <attr_name> [= defaultvalue];
- The format of the Declaration method is:
[<modifiers>] <modifiers> <return_type> <name> ([<argu_list>]) {
[<statements>]
}
Member variables:
member variables in the Java language can use any of the data types (including basic data types and reference types)
A member variable can be initialized when it is defined, and if it is not initialized, Java initializes it with the default value (the ID in the example above is the default initialization, age is the custom initialization, the default numeric type initialization is 0,boolean is Faulse, the reference type is null)
Member variables are scoped to the entire class body
How to classify and object in memory Central:
Class is a static concept, stored in the code area
The object is new, in memory, and each member variable of the class is represented by a different value in different objects (except for static variables), and only one copy of the method is used, which takes up memory when executed.
object creation and use:
You must use the New keyword to create an object
Use objects (references). Member variables to refer to the member variables of an object
Use objects (references). Method (parameter list) to invoke the object's method
There are different member variable storage spaces for each object of the same class
Methods for sharing this class with each object of the same category
The relationship of the class object:
V. Constructor 1. How to construct a function
The constructor is also the constructor method:
Create a new object using the new+ construction method
A constructor is a function that is defined in a Java class to initialize an object
The constructor has the same name as the class and no return value
For example:
Attention:
When no constructor is specified, the compiler automatically adds a constructor for the class, such as: Class name () {}, for example:
When a constructor has been specified, the compiler no longer automatically adds constructors for the class, such as the person function in the example above, if you enter the code in the Main method "Persion Jian = new Jian ();"
2. Overloading of methods and memory resolution 1) methods
Overloading of methods refers to multiple methods in a class that can be defined with the same name, but with different parameters. When called. The corresponding method is selected according to the different parameter tables.
For example:
Public class test1 { publicstaticvoid main (String args[]) { new test1 (); P.info (); P.info ("Hello"); } void info () { System.out.println ("Method1"); } void info (String t) { System.out.println ("Method2"); } }
Similarly, construction methods can be overloaded, and rules are the same as method overloads
2) Memory resolution
First, define such a class to facilitate analysis of the following examples:
classBirthDate {Private intDay ; Private intmonth; Private intYear ; PublicBirthDate (intDintMinty) { day=D; Month=m; year=y; } Public voidSetday (intd) { Day=D; } Public voidSetmonth (intm) {month=m; } Public intGetDay () {returnDay ; } Public intGetMonth () {returnmonth; } Public voiddisplay () {System.out.println ( day+ "-" + month + "-" +Year ); } }
The following is the example code:
Public class test1 { publicstaticvoid main (String [] args) { new test1 (); int date = 9; New BirthDate (7, 7, 1970); New BirthDate (1, 1, +); Test. Change1 (date); Test. Change2 (d1); Test. Change3 (D2);
SYSTEM.OUT.PRINTLN (date);
D1.display ();
D2.display (); } Public void Change1 (int i) { = 1234; } Public void Change2 (BirthDate b) { new BirthDate (2, 2004); } Public void Change3 (BirthDate b) { b.setday (+); }}
Program Run Result:
1) when calling the Change1 method, the stack memory should be allocated a space called I, the value of the call method in the process of the parameters passed in, the example is equivalent to assigning 9 to I, and then the method began to execute, when the completion of i=1234, I became the value of 1234,date unchanged, Because the value that was just equivalent to date is copied to I, after the method executes, all of the local variables allocated for this method are completely gone, so the value of date is still 9,i when the Change1 method is executed.
2) next See Change2 parameter B is a local variable, the incoming is the content of D1, so the stack memory space allocated a piece of space, when the method is executed, the values of B and D1 point to the same address of the object, the method began to execute, b=new a new birthdate out, after the method is finished, The local variable b disappears and the stack memory disappears, and the heap memory does not disappear immediately, and is automatically reclaimed by the Java garbage collection mechanism. At this point the value of D1 is still the original value, no change.
3) Similarly, the method call in the stack memory space allocated an area, named B, the value of the parameters passed in, when the method starts, object B calls the Birthdate class inside the Setday method, so the value of D1 is changed, after the execution of the method B's stack memory space disappears
The memory before the method call executes:
Memory After method invocation:
The value of the heap memory can be changed only when there is a reference in the referenced method, that is, the value is eventually changed.
Third, Java object-oriented programming _1