Code Hosting Http://git.oschina.net/null_289/Java2016/tree/master
Summary of learning contents of textbook
This week, the sixth and seventh chapters of the textbook are studied, and the code design is based on a project, so we focus on code inheritance, polymorphic operation and interface syntax.
1. Inheritance
-
Definition: Object-oriented, prone to repetitive behavior when too many objects involved, in order to avoid this situation, you can write the duplicate part of the parent class, subclass to inherit the parent class.
-
Key words:
-
extends: The subclass inherits the parent class to use extends, such as the public class subclass extends parent class, which means that the newly created class expands the behavior of the original class.
-
protected: Similar to the private function, but in the actual operation, the parent class defines the private member cannot access in the subclass, is more inconvenient, at this time can be changed to declare it as protected, that can be in the same package class directly access, Inherited subclasses can also be accessed directly.
-
final: Used before class to indicate that this class does not have subclasses and cannot be inherited.
-
Override: After inheriting the parent class, define the same method deployment as the parent class, but the execution is different, this is called redefinition, and adding @override in the code can be used to check if the method really redefined a method in the parent class, preventing typos, Otherwise, an error will occur.
3. Redefining the details: If you want to get the method definition in the parent class, you can add the Super keyword before calling the method, redefining the method in the parent class in the subclass, the permission can only be expanded, the return type is a subclass of the method return type in the parent class
4. Polymorphic is-a:
-
Subclasses can inherit only one parent class, and there is a is-a relationship between the child class and the parent class, the subclass is a parent class , when writing code: The parent class =new subclass, let not one class to play another class needs to add a syntax similar to coercion type conversion, such as Swordsman swordsman= (Swordsman) role1.
-
Using a single interface to manipulate multiple types of objects is polymorphic, and one's understanding is to use a parent class to control many subclasses.
2. Abstract methods, abstract classes
- Definition: If a method chunk really does not have any program code operations, can be defined as an abstract class, the definition of incomplete classes cannot be used to generate instances, (PS: The class containing abstract methods must also be defined as incomplete abstract classes, in the work if there are some aspects are not determined, you can invoke the abstract method, Wait until the platform is determined, and then the code to conform to the platform of the perfect operation)
- Abstraction: Abstract classes and methods require the method to be marked with an abstract, such as
public abstract void fight
3. java.lang.object: The topmost parent class of any class must be java.lang.object, so an object array can be used to collect various types of objects.
4. instanceof: The left operand is an object and the right operand is a class that can be used to determine whether an object is created by a class
3. Interface
-
Using the Interace keyword definition, the class to operate the interface, must use the Implements keyword, the interface is defined in two ways, one is the method defined in the Operation interface, and the second is to represent the method as abstract. The operating interface represents the "owning behavior" and is no longer a "is a" relationship.
-
Polymorphism of behavior: similar to inheritance, it is also possible to force an object to act in a certain way.
-
Interface Syntax Details: The method in the interface must be public and abstract when it is not operational.
Enumeration constants can often be defined in the interface interface (example: public static final int ), in fact, only the enumeration constants of public static final are defined in the interface interface, and it is important to use = To specify the value, otherwise compile the error. Personal sense enumeration constants are more clearly expressed in meaning and are easy for programmers to read programs. Interfaces can also inherit interfaces.
4. Other
- Anonymous inner class: the need to temporarily inherit a class or manipulate an interface and establish an instance (example:
Some some=new Some(){}; )
- Enum enumeration constants:
public enum Action{} , in fact, the enum defines a special class, and the element in the enum is the enumeration constant.
Problems in teaching materials learning and the solving process
Just start learning to play the first character game code error, in the inheritance, you need to put the full code in the same folder, sometimes just show a part of the code, but the previous role definition to knock up, to ensure the integrity of the code, this way the class will inherit the parent class.
Some of the contents of the textbook are very detailed, such as polymorphism and is-a, over and over the different statements may appear to compile the failure of the situation and reasons, this part of the study is also very important, related to the later written things are not flexible, good maintenance problems; There are some less expressions in the book, Places that are not very well understood, such as the instanceof operator, override, and so on, can only be learned through video. At this stage still stay in the process of knocking on the book Code, I hope that I can put forward some more valuable questions.
Problems in code debugging and the resolution process
The question of inheritance is not clear in the study. To ask the students to learn about their own shortcomings.
Other (sentiment, thinking, etc., optional)
I've been studying for four weeks, but I don't think the course is really getting started. I hope I can learn Java more efficiently in the future.
Learning progress Bar
|
Number of lines of code |
Study time |
Number of Blogs |
Learn to Grow |
| Goal |
4000 |
300 |
20 |
|
| First week |
100/100 |
5/5 |
2/2 |
|
| Second week |
130/230 |
15/20 |
1/3 |
|
| Third week |
240/470 |
12/32 |
1/4 |
|
| Week Four |
500/970 |
18/50 |
1/5 |
|
20145123 Liu Senming "Java Programming" Fourth Week study summary