20145334 Zhao Writers "java program Design" The 4th Week study summary textbook Learning content Summary
Sixth chapter summary of knowledge points
1-Inheritance common behavior: if there is duplication in the program design, it needs to be modified, so the same program code promotion (pull up) is the parent class. The extends keyword indicates that the new class expands the behavior of the original class.
2-polymorphic and is-a: Subclasses can inherit only one parent class, and there is a is-a relationship between the subclass and the parent class, so that not one class will play another class that needs to be accompanied by a syntax similar to coercion type conversions. -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.
3-redefine behavior: After inheriting the parent class, define the same method deployment as in the parent class, but the execution content is different, which is called redefinition (Override). When redefining a method in a parent class, the subclass must compose the same signature as the parent class method, plus @override can be used to check if the method really redefined a method in the parent class, preventing typos, or an error occurs.
4-abstract method, abstract class: If a method chunk really does not have any program code operations, you can use the abstract to mark the method public abstract void fight, the definition of the incomplete class can not be used to generate an instance, Classes containing abstract methods must also be defined as incomplete abstract classes, subclasses inherit abstract classes, and for abstract methods, one is to continue to mark them as abstract classes, and the other is to manipulate abstract methods.
5-protected members: Members declared as protected, classes in the same package can be accessed directly, and classes in different packages can be accessed directly from the inherited subclass. So remember Java 3 (4) Permissions keyword and its corresponding permission range, see P172 table 6.1
6-Redefined Details: If you want to get the method definition in the parent class, you can add the Super keyword before calling the method, redefine the method in the parent class in the subclass, the permission can only be enlarged, cannot be reduced, the return type after Jdk5, or the subclass of the method return type in the parent class
7-Look again at the constructor: After the subclass instance is created, the initial process of the parent class definition is first performed, and in the initial process of the subclass definition, if the subclass constructor does not specify which constructor in the parent class is executed, the parameterless constructor in the parent class is called by default if the constructor is defined in the parent class. The subclass constructor should pay attention to which function in the parent class is called
8-Look again at the final keyword: If you add the final keyword to a class, it means that the class is the last, and no more subclasses, that is, cannot be inherited.
9-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. 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
10-Garbage collection: For objects that are no longer useful, the JVM's garbage collector frees them. Different requirements will have different garbage collection algorithms.
Abstract class: If there are some aspects are not determined, you can call the abstract method, wait until the platform is determined, then the code to conform to the perfect platform for the line.
Seventh chapter summary of Knowledge points
The interface definition behavior: For defining the behavior, you can use the Interace keyword definition, the class to operate the interface, you 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.
2-Behavior polymorphism: Same as inheritance, you can also let an object to play (cast) a certain behavior, and the same as polymorphic, as long as the operation of the same interface of the class can have the behavior of the interface, maintainable type has also been greatly improved.
3-Solve demand changes: In Java classes can operate more than two classes, can inherit a class and manipulate some interfaces at the same time, in Java, the interface can inherit from another interface, that is, inherit the parent interface behavior, and then in the sub-interface to define the additional behavior.
When the method in the interface is not operational, it must be public and abstract. Enumeration constants (for example: public static final int) can often be defined in the interface interface, and in fact, only the enumeration constants of public static final are defined in the interface interface, and must be used = to specify values. Otherwise, a compilation error occurs. Personal sense enumeration constants are more clearly expressed in meaning and are easy for programmers to read programs. Interfaces can also inherit interfaces.
5-Anonymous Inner class: the need to temporarily inherit a class or manipulate an interface and establish an instance (example: Some some=new Some () {};)
6-Using the Enum enumeration constant: Public enum action{}, in fact the enum defines a special class, and the element in the enum is the enumeration constant.
Problems and solutions in textbook learning after-school exercises:
Ch6:a d d AC DC C B A D
Ch7:a B A b b a D a A A
Problems in teaching materials learning and the solving process
Define the ArrayList class to collect objects of unlimited length
```
Import Java.util.Arrays;
public class ArrayList {private object[] list; private int next;
public ArrayList(int capacity){ list=new Object[capacity];}public ArrayList() { this(16);}public void add(Object o) { if(next==list.length) { list=Arrays.copyOf(list, list.length*2); } list[next++]=o;}public Object get(int index) { return list[index];}public int size() { return next;}
} ```
Design a part of the RPG game code
```
public class prg{public static void Main (string[] args) {Demoswordman (); Demomagician ();
static void demoSwordsMan(){ SwordsMan swordsMan = new SwordsMan(); swordsMan.setname("Justin"); swordsMan.setlevel(1); swordsMan.setblood(200); System.out.printf("剑士:(%s,%d,%d)%n",swordsMan.getName(),swordsMan.getLevel(),swordsMan.getBlood());}static coid demoMagician(){ Magician magician =new Magician(); magician.setname("Monica"); magician.setLevel(1); magician.setBlood(100); System.out.printf("魔法师:(%s,%d,%d)%n",magician.getName(),magician.getLevel(),magician.getBlood());}
} ```
About override description Override is also called function Override, override, the private method in the parent class of the replication cannot be overridden in the subclass override method, and continues to use the overridden method to obtain overrides by using Super. The Subclass method permission must not be less than the parent class method permission When a subclass needs the functionality of a parent class, and a feature theme subclass has its own content, you can override the method in the parent class, which is to follow the functionality of the parent class and define the content that is unique to the subclass
Problems in code debugging and the resolution process
Questions about using enum classes in a switch statement
```
Import static java.lang.System.out;
public class Enumgame {
public static void main(String[] args) { play(EnumAction.RIGHT); play(EnumAction.UP);}public static void play(EnumAction action) { switch (action) { case STOP: out.println("播放停止动画"); break; case RIGHT: out.println("播放向右动画"); break; case LEFT: out.println("播放向左动画"); break; case UP: out.println("播放向上动画"); break; case DOWN: out.println("播放向下动画"); break; }}
} ```
Use interface to define the behavior and appearance of abstractions, such as methods in interfaces that can be declared public abstract
```
public interface action{
public static final int STOP=0;public static final int RIGHT=1;public static final int LEFT=2;public static final int UP=3;public static final int DOWN=4;
}
Import static java.lang.System.out;
public class game{
public static void main(String[] args){ play(Action.RIGHT); play(Action.UP);}public static void play(int action){ switch(action){ case Action.STOP: out.println("bofangtingzhidonghua"); break; case Action.RIGHT: out.println("bofangxiangyoudonghua"); break; case Action.LEFT: out.println("bofangxiangzuodonghua"); break; case Action.UP: out.println("bofangxiangshangdonghua"); break; case Action.DOWN: out.println("bofangxiangxiadonghua"); break; default: out.println("buzhichicidongzuo"); }}
} ```
This week's code hosting
Other (sentiment, thinking, etc., optional)
In the busy completed this week's study, this week's study around the inheritance and interface, the two similar but not the same, the learning content is more abstract, read the teacher to share the Bi Xiangdong teacher's video, I personally feel some abstract things or to see the video, the way of expression in the video and accept the depth to a higher point, Also slowly feel the charm of Java. Language problem or more practice more practice!
Learning progress Bar
|
Lines of code (new/cumulative) |
Blog volume (Add/accumulate) |
Learning time (new/cumulative) |
Important growth |
Goal |
3000 rows |
20 |
300 |
|
Week Four |
250/350 |
1/3 |
21/30 |
|
- Java Learning Notes (8th Edition)
- Java Learning Note (8th Edition) Learning Guide
- ...
20145334 The 4th Week study summary of the Java program design by Zhao Literary writers