Shoot射击游戏连载1
知识点内容:继承
格式:class a extends b{
}
1)继承作用:代码复用
2)通过extends继承
3)超类:派生类所共有的属性和行为
派生类:派生类所特有的属性和行为
4)派生类继承超类 派生类具有:超类的+派生类的
5)一个超类有多个派生类
一个派生类只有一个超类
6)继承具有传递性
class Person{-----------超类/父类 String name; int age; } class Teacher extends Person{-----------派生类、子类 }
shoot代码改写
原代码:
public class Airplane { int width; int height; int x; int y; int speed; Airplane(){ width=48; height = 50; Random rand = new Random(); x = rand.nextInt(400-width); y=-height; speed = 2; } void step() { System.out.println("小敌机的y向下"); }; }
增加一个FlyingObject 超类/父类
超类里面放了共有的属性和行为
public class FlyingObject { int width; int height; int x; int y; void step() { System.out.println("小敌机的y向下"); } }
子类中放特有的属性和行为
public class Airplane extends FlyingObject { int speed; Airplane(){ width = 48; height = 50; Random rand = new Random(); x = rand.nextInt(); y = -height; speed = 2; } }

浙公网安备 33010602011771号