Object oriented
Object-oriented: Suitable for large complex projects, such as building a car, the various components (objects): wheels, frames, glass and so on to complete a whole car manufacturing (need to collaborate)
Process oriented: Suitable for simple projects, such as driving, according to a certain process, 1.2.3.4, the first step of the ignition, the second step of the gear, the third step to start, step by step (do not need collaboration)
These two ideas are mutually reinforcing, even though we are now all object-oriented, but when you build each object, you also use process-oriented thinking to build an object.
Objects and classes
The object contains its properties and behavior, and the property is the variable that the object contains, and the behavior is the method that the object contains. so the object contains variables and methods.
Class, which is the template for the object. For example, all dogs have four legs (the same property) and will be called (the same behavior), so Huskies, Chihuahua are all part of the dog category.
Memory Analysis: Stack stack, heap heap, method area
The JVM contains stacks, heaps, and method areas that are also contained in the heap
Stack: The stack represents the memory model that the method executes. Each time the method is called, a stack frame is created. The JVM creates a stack for each thread, and the stack belongs to each thread private and cannot be shared. The storage feature of the stack is continuous storage space, first-out, backward-out. The system automatically allocates stacks and is fast.
Heap: Stores the created objects and arrays, on one, all threads can be shared. Non-contiguous memory space, slow speed.
Method Zone (static zone): The actual heap is also used to store classes, constants of information (unchanging things).
Constructor (construction method) (constructor)
1. The construction method name must be consistent with the name of the class.
2. Called with the New keyword
3. Cannot return a value with return
4. If you do not define a constructor, the system automatically defines an argument-free constructor
This represents the creation of a good object, so this can be used to differentiate between member variables and local variables. This cannot be used in static classes and methods, and the preceding memory analysis says that static variables and methods belong to the class and do not belong to the object, so this does not refer to any objects.
Java's garbage collection mechanism
1. Discover useless objects
2. Reclaim space occupied by useless objects
Recycling is simple, but how do you find it? Mainly by the following two algorithms:
1. Reference counting method
Each time an object is referenced by a variable, it is recorded once. Determines whether it is garbage by logging the number of citations. However, when two objects are referenced by a loop, the counter cannot be detected.
2. Reference to the law of accessibility
Make a diagram of all the reference relationships of the program, and use the algorithm to track the object's references to see if it can be reached. If not reachable, it is a useless object.
Static keyword
The static modified member variables and methods, which belong to the class, do not belong to the object. Common variables and methods belong to the object.
Static methods cannot invoke normal methods and ordinary variables. Normal methods and variables can be called statically. Because static belongs to a class, there are classes that do not necessarily have objects, but objects must have classes.
Java packages, the use of the package
A Java package, similar to a folder on your computer. Generally in the first sentence of the class, the domain name is written backwards.
Right-New-package can also build packages
Be sure to add the package, easy to manage, do not use the default package
Package Cn.authorx Public class Test { System. out. println ("Hello world! " )}
Object-oriented three main features: inheritance, encapsulation, polymorphism
Inheritance, instance of judgment affiliation
Public classMain { Public Static voidMain (string[] args) {Student Student=NewStudent (); Student.name="Jason"; Student.major="Finance"; Student.studentid="001"; System. out. println (Student.name +"is a student, he ID is"+ Student.studentid +"And his major are"+student.major); Student.study (); Student.rest (); System. out. println (student instanceof person); } Static classperson{String name; String Major; Public voidRest () {System. out. println ("Take a break!"); } } Static classStudent extends person{String StudentID; Public voidStudy () {System. out. println ("I am studying now!"); }}} Run result: Jasonis A student, his ID is 001and his major isfinancei am studying now!Take a Break!true
Packaging
Access rights
Rules for general use of permissions in work:
1. For the properties of a class, it is generally used directly with private and then accessed with getter and setter
Public class computer{ privateint seriesnumber; Private String model; Private String brand; }
2. For the methods inside the class, except for special cases, general public
Polymorphic
The invocation of the same method may have different behavior because of the different objects in it. Like people. Rest () This method, some people may sleep, some people read books, some people play games, different people on the "rest" may have different performance, this is the object different, behavior may be different.
Points:
1, polymorphic refers to the polymorphism of the method, not the polymorphism of the attribute, independent of the attribute
2. Three prerequisites for polymorphic existence: inheritance, method Override, parent reference to child class object
3, the parent class reference to the child class object, with the parent class reference to call the method overridden by the subclass, when Polymorphism appears
/** * Test polymorphism*/ Public classMain { Public Static voidMain (string[] args) {Animal a=NewAnimal (); Animalcry (a); Dog D=NewDog (); Animalcry (d); //The parent class reference points to the subclass object, and the method that called the subclass } Static voidAnimalcry (Animal a) {a.shout (); } Static classanimal{ Public voidshout () {System. out. println ("a shout! "); } } Static classDog extends animal{ Public voidshout () {System. out. println ("Wang-woo! ");//inheritance and overriding methods } } classCat extends animal{ Public voidshout () {System. out. println ("Meow meow Meow ! ");//inheritance and overriding methods } }}
Abstract class/abstract method
Abstract class: A class that contains an abstract method
Abstract method: Only method declaration, no method body
Points:
1. Classes with abstract methods can only be defined as abstract classes
2., abstract class cannot be instantiated, cannot instantiate abstract class with new
3. Abstract classes can contain properties, methods, and construction methods. The constructor method cannot be used with the new instance, it can only be used for the quilt class call
4. Abstract classes can only be used to inherit
5. The abstract method must be implemented by the quilt class
/** abstract class/abstract method*/ Public Abstract classAnimal { Public Abstract voidRun ();//There are methods, there is no method body}classCat extends Animal {@Override Public voidrun () {System. out. println ("Cat trot ....");//subclasses must override methods }}
Interface
/** * Interface * What is the interface? An interface is a specification that defines a set of rules that "if you are ... You have to be able to ... "The meaning of this thought interface is to design and implement a completely separate interface to support multiple inheritance*//** What are the benefits of completely separating design from implementation? For example: How to describe the relationship between airplanes, missiles, basketball, stone? , if you use abstract classes, inheritance, the relationship between them is very limited. It would be very easy to use interfaces to describe the behavior that they can "fly" .*/ Public InterfaceMyInterface {//interface only: Constants and abstract methods! intMax_speed =10000; intMax_height =1; Public voidfly ();}classPlane implements MyInterface {@Override Public voidFly () {System. out. println ("The plane is flying ."); }}classBasketball implements MyInterface {@Override Public voidFly () {System. out. println ("Basketball is flying ."); }}
Callback Callback (Hook hook function)
Write an interface first
Interface imyframe { void paint ();}
/** * Callback callback (Hook hook function)*/ Public classPaintframe { Public Static voidDrawframe (imyframe f) {System. out. println ("Launch ..."); System. out. println ("Preparing ..."); F.paint (); //Call the Paint method } /** The above code only leaves a paint method, how to realize what you pass to him what the object, what use, the essence is a polymorphic implementation. * Here This paint method is a bit like a hook, what you hang up, what he realizes, so called the hook function*/ Static classGAMEFRAME01 implements imyframe{ Public voidpaint () {System. out. println ("Painting GameFrame01 ...");//implementing interfaces, overriding methods } } Static classGAMEFRAME02 implements imyframe{ Public voidpaint () {System. out. println ("Painting GameFrame02 ..."); } } Public Static voidMain (string[] args) {Drawframe (NewGAMEFRAME01 ());//call method, new object }}
Inner class Innerclass
Advantages:
1, better encapsulation, only allow direct access to external classes, do not allow other classes to access directly, other classes want to access through the external class
2, you can directly use all the properties of the external class (including private properties) and methods, in some cases it is easier
Usually a class is used only by another class to define it as the inner class of the class.
/** * Inner class*/ Public classOuter { Public Static voidMain (string[] args) {face F=NewFace ();//to have external class members before you can new members of the inner class and methods of calling inner classesFace.nose n = f.NewNose (); N.breath (); Face.ear e=Newface.ear (); E.listen (); }}classface{String shape; String color; StaticString Skin; classnose{String height; voidBreath () {System. out. println ("Breathing ...");//nose can access shape, color; Face cannot access height } } Static classear{voidListen () {System. out. println ("I ' m listening ...");//A static inner class cannot access the normal properties of an external class, Shape,color. However, you can access the static properties of the outer class: Skin } }}
Java Object-oriented