Ii. object-oriented oriented objects
Encapsulate the data with the class organization code, Object
Process oriented: Organize data by method (function), solve it with linear thinking, when the amount of data is small, there is no problem, when the data is more, the method is more, to
Method for core organization code, tiring, poor extensibility, difficult to maintain (when multi-person collaboration project)
C language Structure, a set of multiple variables in a piece, easy to manage; According to this idea, horizontal vertical diffusion/deep thinking, the similar methods are put together, the method repeatedly called variables,
Depth divergence, the method and the variable package to a piece, this concept in C language does not have a name called class "YY"
Organize your code in classes, easy to maintain, and easy to scale
1000 variables + 1000 methods-----> 50 Classes
The object-oriented advantage is only from the complexity of software, if it is simple, it is not necessary, with the process-oriented implementation can be
Object-oriented thinking: How to drive a car as an example to illustrate
Process oriented: (things are relatively simple, think with linear thinking to solve problems; Just keep the elephant in the freezer)
1. Step on the Clutch
2. Attach Files
3. Step on the accelerator, put on the clutch
4. Open the
Object-oriented: Multi-role, collaborative completion (think about which objects these processes need)
Objects: Drivers, automobiles
Driver drives a car
The way to drive details under the car, Car.start (); To achieve
How to build a car?
Object-oriented thinking: tires, seats, engines, frames ..., assemble the stuff that's built up, build the car
The object-oriented thinking grasps the whole, the process realizes the local detail
The nature of object-oriented programming (OOP): Organizing code in a class, organizing (encapsulating) data in an object's way
Object-oriented thinking: OOA, OOD
Object: is the specific thing
Class: Is an abstraction of an object, (an abstraction, a part of extracting an image), a class is a template for an object. Typical case: Math 1-10 (1-10 people, 1-10 apples, 1-10 etc.)
The logic of reality is implemented by code, static data, dynamic behavior
===================================================================
A variable type other than the base type in the Java language is referred to as a reference type, and a Java object is manipulated by referencing the reference (address).
Unassigned, default initialization value
Numeric type:
--Integral type: 0
--Float type: 0.0
--char Type: \u0000
--boolean Type: 0 false
--string null
Properties (member variables, static data); methods (dynamic Behavior), methods are subordinate to class
Member/instance variable, local variable
The local variable must be initialized, the member/instance variable is not assigned, and the system is initialized with the default value.
======================= Memory analysis of program execution process (preliminary analysis shallow, waiting for later improvement) ====================
Stack, heap, method area
1. Stack
Automatic allocation of contiguous space, LIFO
Storage: Local Variables
2. Heap
Space discontinuity
Storage: New objects
3. Method area (part of space in the heap)
Storage: Class information (code), static variables, Chang (string constants), etc.
The concept of stack frame?? Post-Supplement
The JVM does not find a class in memory, it loads classes from Classpath, finds a class file, the JVM loads the class through the ClassLoader class loader, and after loading, there is student information in the method area
Action object, which is the address of the operation, reference type 4 bytes
garbage collection mechanism (garbage Collection); Space allocation, new object, Object Space deallocation: GC reclaims objects that are not referenced
=============================== Constructor ===================================
The constructor is also called the construction method constructor
The format is as follows:
[Modifier] class name (formal parameter list) {
N-Line statement
}
The construction method is a special method:
1. Call by new
2. The definition does not require a return value; No constructor can use the return statement
3. The constructor's method name must match the public class name
4. If no constructors are defined, the compiler automatically constructs an parameterless constructor during compilation, and if defined, the editor does not add
5. Initialize the object that constructs the class
1 Car.java:2 Public classCar {3 intSpeed ;4 PublicCar () {5System. out. println ("Construct a car");6 }7 }8 9 Testconstructor.javaTen Public classTestConstructor { One Public Static voidMain (string[] args) { ACar C =NewCar (); - } - } the - compile the execution result: - Construct a car -The construction class car is called by new and also initializes the object C
============================ Method Overloading & construction method overloading ===============================
Overloading (overload): The most important thing is that there is no ambiguity when calling (the compiler does not know which one to invoke). )
Overloading of methods refers to multiple methods in a class that can be defined with the same name but with different parameters, and when called, the corresponding method is selected according to the different parameter tables
Two and three different
--Two same: same class, same method name
--Three different: parameter list is different (type, number, order)
Only a different return value is an overload that cannot form a method: int a (string str) {}/void A (string i) {}, when a (string c) is called, there is ambiguity and you do not know which one to call.
Only parameter names are different and cannot be overloaded with methods: int a (int b) {}/int a (int c) {}, calling a (int d) will cause ambiguity
As with the normal method, the construction method can be overloaded, and the overloaded condition is consistent with the normal method.
1 Public classTestoverload {2 Public intAddintAintb) {3 returnA +b;4 }5 Public Static voidMain (string[] args) {6MyMath m =NewMyMath ();7 intresult = M.add (4.2,8);8System. out. println (result);9 }Ten } One A classMyMath { - intA; - intb; the - PublicMyMath () { - } - + PublicMyMath (inta) { - This. A =A; + } A at PublicMyMath (intBinta) { - This. B =b; - This. A =A; - } - - Public intAddintBDoublea) { in return(int) (A +b); - } to + Public intAddDoubleAintb) { - return(int) (A +b); the } * $ Public intAddintAintb) {Panax Notoginseng returnA +b; - } the + Public intAddintAintBintc) { A returna+b+C; the } + -}
====================================static Static ===================================
In a class, a member variable declared with static is called a static member variable, or is called A: Class Property, Class variable
--It is a common variable for the class, belongs to a class, is shared by all instances (objects) of the class, and is explicitly initialized when the class is loaded.
--For all objects of the class, only one copy of the static member variable is shared by all objects of that class
--You can use the object. Class property to invoke, however, it is generally used with the class name. Class attribute
--static variables in the method area
Methods that are declared with static are called static methods
--You can call (class name. Method name) without requiring an object.
--When the method is called, no reference to the object is passed to it, so non-static members cannot be accessed in the static method
Static initialization blocks:
If you want to initialize the entire class after loading, you can use the static initialization block, which executes when the class is initialized, not when the object is created.
Static initialization blocks cannot access non-static members
Execution order: Upstream to the object class, execute the static initialization block of object first, and then execute the static initialization block of the subclass until the static initialization block of our class
Static methods cannot invoke non-static content
Non-static methods can call static variables and static methods
From the method belonging to the class, cannot invoke the object content, the object does not have the static
Student.java Public classStudent {String name; intID; Static intSS; Public Static voidprintss () {System. out. println (ss); } Public voidStudy () {PRINTSS (); System. out. println (name+"in Learning"); } Public voidSayHello (String sname) {System. out. println (name+"to"+sname+"say: Hello! "); }}test.java Public classTest { Public Static voidMain (string[] args) {STUDENT.SS=323;//class variables, which are typically "class names. Class Properties"student.printss (); Student S1=NewStudent (); }}
===========================================this=========================================
This is used inside the method but cannot be used in the static method; The This parameter, which is passed by the JVM when invoked, can be used in the method.
Common method: This always points to the object that called the method
Construction method: This always points to the object being initialized
Implicit parameter: This super
This (); Call other constructor methods, which must be placed in the first sentence
Student.java Public classStudent {String name; intID; PublicStudent (String name,intID) { This(name);//calling other construction methods through this must be in the first sentence! Constructor call must is the first statement in a Constructor This. Name =name; This. ID =ID; } PublicStudent (String name) { This. Name =name; } PublicStudent () {System. out. println ("constructs an object"); } Public voidsetName (String name) { This. Name =name; } Public voidStudy () {System. out. println ( This. Name); This. name="Zhang San"; System. out. println (name+"in Learning"); } Public voidSayHello (String sname) {System. out. println (name+"to"+sname+"say: Hello! "); }}test.java Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubStudent S1 =NewStudent (); S1.name="John Doe"; S1.study (); }} Execution result: Constructs an object Li Shizhang in the Learning construct method: this always points to the object that is about to initialize the normal method: this always points to the reference address of the object that called the method<---Reference addresses for >new objects
Object-oriented three major features:
=================================== Inheritance ======================================
Ood, a class is an abstraction of an object, and inheritance is an abstraction of a group of classes, thus enabling better modeling of the real world//inherited
An OOP perspective to improve the reusability of code
Extends "extended" subclasses are extensions to the parent class (Class A extends B)
Subclass inherits the parent class and can use all the properties and methods of the parent class (except the constructor of the parent class)
Java classes have only one inheritance , which is good for maintenance (for example: there is only one immediate ancestor, no more)
Multiple inheritance in Java, which can be implemented through interface interface
If a class is defined, extends is not called, its parent class is java.lang.Object (Root class)
Method overrides: When the method inherited from the parent class is inappropriate, the method override is required, and the override of the method has no relation to the overload of the method
(Read the source code, need to change the mind; Chinese: Asia, China, Beijing, Daxing District, Streets | English: Streets, Daxing District, Beijing, China, Asia;
Hover over the class you want to view, hold down CTRL, and jump to the class file to view the source code; Ctrl + T, view the level of the source)
The overridden method must be overridden with the same method name as the parameter list and return type
Implicit parameter: This,super public void Run (this,super) {};
This: reference (address) of the current object
Super: A reference (address) to the parent class object that accesses the parent class's properties or values through Super
Construction method: All construction methods, the first sentence super, called the parent class constructor, in addition to the object class; If not added, the compiler automatically adds
Java Learning Notes Chapter II