Working some time, using the Java language, looked under thinking in Java, special here to record and share some of their own understanding.
One, about the design, is the management of complexity
With regard to complexity, there are two considerations: the complexity of the problem to be solved and the complexity of the tool for solving the problem. The problem to be solved, the programmer is not good to control, and the solution of the tool is the programming language.
Second, about Java is a completely object-oriented language
1.java is completely object-oriented and needs to be "class" level to exist independently, such as methods, can not exist independently, a method will always belong to a class.
This "class" level is not entirely class, and Interface,enum is also the "class" level.
For the "class" level of existence, can be understood as a person, where there is a lake, there is a class of places between the possibility of association, for this association, a better way to understand is "to treat the class as a service person" (also is the other class of "the Service Person"). this kind of relationship from a small point of view is a class call Class B method, from a large view can also be one system calls another system provides interface.
2. With regard to fully object-oriented, the problem of code reuse must be considered, and there are two types of workarounds for reuse in Java: Composition and inheritance.
Combination: Car owning engine, do not need to write the engine in the car how to implement, but call engine class, put in the car class can, after inheritance again.
For design, it is generally preferable to use a combination to avoid the principle of excessive use of inheritance. (Although inheritance is one of the most important features of Java)
3. With respect to inheritance, the simple thing is Extends,a extends B, then A has all the elements in B, so that when writing the code, you can avoid some of the properties of excessive repetition of writing.
4. For polymorphism, the common scenarios are as follows:
1 classAnimal {2 3 Public voidf () {}4 }5 6 classFishextendsAnimal {7 @Override8 Public voidf () {}9 }Ten One classDogextendsAnimal { A @Override - Public voidf () {} - } the - Public classTest { - Public Static voidMain (string[] args) { -Animal Animal =NewFish (); + animal.f (); - } +}
Notice animal here, the parent declaration points to the subclass object, you can change the fish to dog, the rest of the time, and the F () method will automatically discriminate the object described in the F () method, whether the dynamic binding is to run F () in fish or to run F () in dog. This is polymorphic.
Iii. four features in Java: Abstraction, encapsulation, inheritance, polymorphism
Reference to the contents of the second, the "class as a service" category, corresponding to the abstract and packaging---level1
In order to reduce the code duplication design out of the parent-child relationship, the corresponding is inherited--------------Level2
On the basis of inheritance can dynamically determine the method of execution, corresponding to the polymorphic------------level3
First, the initial impression of Java