Shoot射击游戏第三天:
1.设计小敌机数组、大敌机数组、小蜜蜂数组、子弹数组,并测试
2.设计FlyingObject超类,6个对象类继承超类
3.给FlyingObject设计两个构造方法,6个对象类分别调用
今天需要用到的知识点为继承和数组
1.引用类型数组:
1)Student[] stus = new Student[3]; //创建Student数组对象
stus[0] = new Student("zs",25,"LF"); //创建Student对象
stus[1] = new Student("ls",26,"JMS");
stus[2] = new Student("ww",28,"SD");
System.out.println(stus[0].name); //输出第1个学生的名字
stus[1].age = 24; //将第2个学生的年龄修改为24
stus[2].sayHi(); //第3个学生跟大家问好
for(int i=0;i<stus.length;i++){ //遍历stus数组
System.out.println(stus[i].name); //输出每个学生的名字
stus[i].sayHi(); //每个学生跟大家问好
}
2)Student[] stus = new Student[]{
new Student("zs",25,"LF"),
new Student("ls",26,"JMS"),
new Student("ww",28,"SD")
};
2.继承:
1)作用:代码复用
2)通过extends来实现继承
3)超类/父类:派生类所共有的属性和行为
派生类/子类:派生类所特有的属性和行为
4)派生类继承超类后,派生类具有:派生类的+超类的
5)一个超类可以有多个派生类
一个派生类只能有一个超类----单一继承
6)具有传递性
7)java规定:构造派生类之前必须先构造超类
7.1)若派生类的构造方法中没有调用超类的构造方法,
----则默认super()调超类的无参构造方法
7.2)若派生类的构造方法中调用了超类的构造方法,
----则不再默认提供
注意:super()调用超类构造必须位于派生类构造方法的第一行
3.super:指代当前对象的超类对象
super的用法:
1)super.成员变量名-----访问超类的成员变量
2)super.方法名()-------调用超类的方法
3)super()--------------调用超类的构造方法
软件中的继承:
代码不用自己写,自己也能用
生活中的继承:
1)继承财产:
钱不用自己挣,自己也能花
2)继承皇位:
江山不用自己打,自己也能坐
3)继承工作:
工作不用自己找,自己也能干
继承说简单一点就是儿子继承爹,儿子只能有一个爹,但是爹可以有多个儿子继承,然后引用数组可以理解为把学生1,学生2,学生3放到一个数组里面这样new数组就行了
(下面上代码,通过代码可以更好的理解)
在写代码前我们要创建一个爸爸类Flyingobject
这里我们创建了一个爸爸类儿子类通过继承爸爸类就可以用这样就可以少写好多代码,但是爸爸类里面自能写儿子共有的属性/行为,比如所以儿子都有宽,高,x,y,另外有的儿子需要速度有的需要y1这些就不能放在爸爸里面写这是儿子所特有的行为写在儿子类自己里面.
注意:我们还需要养成一个良好的习惯就是在写的时候要先写爸爸类在写儿子类.
package cn.tedu.shoot;
import java.util.Random;
public class Flyingobject {
int x;
int y;
int width;
int height;
Flyingobject(int width,int height){
this.width=width;
this.height=height;
Random rand=new Random();
x=rand.nextInt(400-width);
y=-height;
}Flyingobject(int width,int height,int x,int y){
this.width=width;
this.height=height;
this.x=x;
this.y=y;
}
void step(){
System.out.println("plan");
}
}
这里解释一下为什么写了两个FlyingObject,这是因为上面的是给大敌机,小敌机,蜜蜂用的,他们的xy不确定需要随机生成,我们看飞机大战也可以了解到敌人的出现位置是不确定的,下面的是给英雄机,子弹,天空用的.
package cn.tedu.shoot;
public class Airplane extends Flyingobject{//通过用extends关键字来实现继承
int speed;
Airplane(){
super(48,52);
speed=2;
}
}
package cn.tedu.shoot;
import java.util.Random;
public class Bee extends Flyingobject {
int xspeed;
int yspeed;
int awardType;
Bee(){
super(23,52);
Random rand=new Random();
xspeed=2;
yspeed=4;
awardType = rand.nextInt(36);
}
}
package cn.tedu.shoot;
import java.util.Random;
public class BigAirplane extends Flyingobject{
int speed;
BigAirplane(){
super(52,51);
speed=1;
}
}
package cn.tedu.shoot;
public class Bullet extends Flyingobject{
int speed;
Bullet(int x,int y){
super(20,52,x,y);
speed=10;
}
}
package cn.tedu.shoot;
public class Hero extends Flyingobject {
int life;
int fire;
Hero (){
super(97,139,140,100);
life=3;
fire=0;
}
}
package cn.tedu.shoot;
public class Sky extends Flyingobject{
int speed;
int y1;
Sky(){
super(20,700,22,2);
speed=10;
y1=-700;
}
}
package cn.tedu.shoot;
public class World {
Hero h;
BigAirplane[] big;
Airplane[] as;
Bee[] bee;
Bullet[] bul;
Sky s;
void action(){
as=new Airplane[3];
as[0]=new Airplane();
as[1]=new Airplane();
as[2]=new Airplane();
for(int i=0;i<as.length;i++){
System.out.println(as[i].x+","+as[i].y);
as[i].step();
}
}
public static void main(String[] arge){
World w =new World();
w.action();
}
}
大家可以发现这样的好处是不用单独在儿子类里面写行为了,直接在爸爸类里面继承就好了,大大减少了代码量
最后的输出结果就不给大家展示了,大家可以填写一些其他的数检验一下