(一)、创建一个武器类Weapen
定义类中属性:攻击力Power,速度speed,并实现构造方法
创建一个Tank子类从Weapen类继承
具有私有的方向字段dir(上下左右),并用属性封装
定义攻击方法Attack(),打印“我是坦克,向**方向运动,速度***,攻击力***”
创建一个子弹类Bullen从Weapen类继承
具有私有的type字段(表示子弹类型,如:机枪子弹,步枪子弹),用属性封装
定义攻击方法Attack(),打印”我是子弹***,速度***,攻击力***”
为Tank类和Bullen类定义构造函数初始化属性(要求使用super调用父类的构造函数)
创建一只Tank对象,调用其方法;创建一只Bullen,调用其方法
package ja1;
public class Weapen {
int power;//攻击力
double speed;//速度
public Weapen(int power,double speed){
this.power=power;
this.speed=speed;
}
}
package ja1;
public class Tank extends Weapen{
private String dir;//方向
public Tank(String dir){
super(45,23.3);
this.dir=dir;
}
public void Attack(){
System.out.println("我是坦克,向"+dir+"方向运动,速度"+speed+",攻击力"+power);
}
}
package ja1;
import java.util.Scanner;
public class Bullen extends Weapen{
private String type;
public Bullen(String type){
super(85,52.1);
this.type=type;
}
public void Attack(){
System.out.println("我是"+type+",速度"+speed+",攻击力"+power);
}
}
package ja1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入坦克运动方向:");
String dir=input.next();
Tank tan=new Tank(dir);
tan.Attack();
System.out.println("请输入子弹类型:");
String type=input.next();
Bullen bul=new Bullen(type);
bul.Attack();
}
}