What is an abstract class?
Abstract class is one of the important features in java. A class or method can be modified with abstract keyword. The class modified with abstract cannot be instantiated, and the method modified with abstract cannot have a specific implementation, but must be implemented by subclasses
public abstract class Class name{//abstract class public abstract Data type method name();//Abstract method }
Characteristics of abstract classes:
1. Abstract methods and abstract classes are decorated with abstract. Abstract methods must be placed in abstract classes
2. Ordinary methods can be written in abstract classes, and the methods must be implemented
3. When defining an abstract class object, if there are abstract methods in a class, all abstract methods must be implemented
If there are ordinary methods in the abstract class, and these methods can be called when creating the abstract class object
However, abstract classes, like interfaces, define rules for subclasses, mainly to let subclasses implement method content. If there are no abstract methods in abstract classes
If all methods are instantiated, there is no point in defining abstract classes
4. After the subclass inherits the abstract class, it needs to implement all the abstract methods in the abstract class. If there is an implementation of a specific method in the abstract class (generally not so written), the subclass can also be called
5. Abstract classes do not necessarily contain abstract methods, but the abstract classes of abstract methods are meaningless
6. Abstract classes can also inherit abstract classes, but only one can inherit
7. Abstract classes contain constructors, which can be instantiated, and can also write properties and get,set and toString methods. However, these operations are generally not written in abstract classes, because the definition purpose of abstract classes is to formulate rules like interfaces
Title Requirements:
Design a tank war game related class, including tank class, including attributes: attack power, life value and defense value; Including methods: shooting, moving, death; There are three types of tanks (type A, type B and type C). The attack power, life value and defense value of each tank are different, and the behavior of each tank is also different, including:
Type A tank: it has fast shooting speed, fast movement speed, high attack power, weak HP and defense value, and takes A short time to destroy
Type B tank: moderate shooting speed, moderate movement speed, moderate attack, HP and defense. The effect is cool when destroyed
Type C tank: slow shooting speed, slow movement speed, weak attack, high HP and defense value, and slow destruction time
The code is as follows:
public abstract class Tank { private String gj; //aggressivity private String sm; //Life value private String fy; //Defense value public Tank() { } public Tank(String gj, String sm, String fy) { this.gj = gj; this.sm = sm; this.fy = fy; } public String getGj() { return gj; } public void setGj(String gj) { this.gj = gj; } public String getSm() { return sm; } public void setSm(String sm) { this.sm = sm; } public String getFy() { return fy; } public void setFy(String fy) { this.fy = fy; } @Override public String toString() { return "Tank{" + "gj='" + gj + '\'' + ", sm='" + sm + '\'' + ", fy='" + fy + '\'' + '}'; } }
public abstract class DAO extends Tank{ public abstract String biubiu(); public abstract String yd(); public abstract String sw(); }
public class TankA extends DAO{ @Override public String biubiu() { return "Fast shooting speed"; } @Override public String yd() { return "Fast moving speed"; } @Override public String sw() { return "Destruction takes less time"; } }
public class TankB extends DAO{ @Override public String biubiu() { return "Moderate shooting speed"; } @Override public String yd() { return "Moderate movement speed"; } @Override public String sw() { return "Cool when destroyed"; } }
public class TankC extends DAO{ @Override public String biubiu() { return "Slow shooting speed"; } @Override public String yd() { return "Slow moving speed"; } @Override public String sw() { return "Slow destruction"; } }
public class TankTest { public static void main(String[] args) { TankA a = new TankA(); a.setGj("High attack power"); a.setSm("HP 100"); a.setFy("Defense value is weak"); System.out.println("Tank A:" + a.biubiu() + "," + a.yd() + "," + a.getGj() + "," + a.getSm() + "," + a.getFy() + "," + a.sw()); TankB b = new TankB(); b.setGj("Moderate attack power"); b.setSm("HP 120"); b.setFy("Moderate defence value"); System.out.println("Tank B:" + b.biubiu() + "," + b.yd() + "," + b.getGj() + "," + b.getSm() + "," + b.getFy() + "," + b.sw()); TankC c = new TankC(); c.setGj("Low attack power"); c.setSm("HP 200"); c.setFy("High defence value"); System.out.println("Tank C:" + c.biubiu() + "," + c.yd() + "," + c.getGj() + "," + c.getSm() + "," + c.getFy() + "," + c.sw()); } }