Object-oriented course completion you can write a simple aircraft war program. I think I need to summarize.
The design of the class in aircraft wars:
-
- Parent class: Flyingobject (abstract Class)
- Interface: Award, Enemy
- Subclass: Hero, Bullet, airplane (implements enemy interface), Bee (Implements award interface)
- Run class: Shootgame
Hero, Bullet, airplane, Bee inherit from the Flyingobject class, Flyingobject have their public properties and behavior, because Flyingobject does not need to be instantiated, so large can be designed as abstract classes.
Award, Enemy is two interfaces, respectively, for the reward and the enemy, the award defines the reward type constants and the method of obtaining rewards, enemy defines the method of acquiring the score of the enemy aircraft. Bee and Airp inherit the Flyingobject class and implement the award and enemy interfaces respectively, allowing them to inherit the parent class and also have the attribute method in the interface. The design of the interface improves the extensibility of the program, and if you want to add a Hornet (Bigbee) class, you can get both rewards and scores, and this class inherits Flyingobject in implementing the award and enemy interfaces. A class can inherit a parent class, but it can implement multiple interfaces, and the interface makes the program much more extensible. is not immediately feel the interface Meng Meng Da, particularly good to use it.
Shootgame is the implementation of application specific logic, which defines the properties and methods of the stage interface. Its essence is the use of data layer, the previous classes are the data base, this class is to assemble data. Well, I hope my understanding is fine.
A good application must have a good design, following the requirements analysis phase, the design of classes and data structures is also a very important stage, in several project practice, I have probably learned this. There is no good design, the next road is only one word-bitter ... Wow!
As for the design principle of the class : first to find out the properties and methods common to all objects, to design the parent class, as to whether the parent class is designed as an abstract class or a non-abstract class to see if the program needs to instantiate the object. Then see if each class has a common method feature other than the parent class that abstracts it as an interface. Next is the method of the class, the design of the method as far as possible to reduce its coupling, plainly speaking is to take apart each function, decomposition and decomposition.
About Object-oriented:
What is object-oriented? Object-oriented is to regard objective things as objects with state and behavior, to find out the common state and behavior of the same kind of objects by abstraction, and to form the model-class (the smallest unit composing Java program).
Object-oriented has the following basic characteristics :
- Abstraction and encapsulation. Abstract result is the formation of classes, the data and methods in the class are protected, you can set different access control properties according to the needs, so as to achieve the encapsulation of data, ensure the security of the data, hiding the implementation details of the method, but also convenient to use.
- Inherited. You can add properties and functionality to existing classes, or make partial modifications to create new classes that implement code reuse.
- Polymorphic.
- Different behaviors can result when the same message is received by a different object (that is, a reference of the same type points to a different object). ---behavioral polymorphism
- The same object can have different functions after being shaped into different types. ---object polymorphism
A summary of the error-prone points in object-oriented:
1. About static:
- static modifier variable: Then the variable belongs to the class, there is a method area, only one copy (all object sharing), accessed through the class name.
- Static Modification method: There is only one copy of the method area. Because there is no implicit this pass, instance variables and non-static methods cannot be accessed directly in a static method.
- Static BLOCK: Executed automatically during the class being loaded, because the class is loaded only once, so the static block is only executed once. Often used to load (initialize) static resource slices, audio, video, and so on.
2. Questions about calls after method overrides in subclasses: So, what is the purpose of rewriting? Of course, for different objects have different methods Ah, if the subclass rewrite the parent class method, but still call the parent class method, it is useless, so, anyway, as long as the method that refers to the call is a subclass of the overridden method, the subclass method is called. Except for the overridden method, the other data depends on the reference type, when a subclass object is shaped into a parent object (or its implemented interface), the reference can be accessed by the method is not reduced Ah, because this is the need to see what the parent class has.
Aircraft war writing and Java Object-oriented summary