飞机大战和继承
飞机大战和继承
1 业务规则
主要运用Java基础,面向对象知识点。
toString方法是Java约定的特殊方法,printh等API输出数据时候,会自动调用toString方法获得对象数据,添加此方法可以简化输出对象数据的代码。
案例代码:
飞机
public class Airplane{
double x;
double y;
doule width;
double height;
double step;
public Airplane(double x, double y, double width,
double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y+=step;
}
@Override
Public String toString(){
return"Airplane[x="+x+",y="+y+",width="+width
+",height="+height+",step="+step+"]";
}
}
大飞机
public class Bigplane{
double x;
double y;
double width;
double height;
double step;
public Bigplane(double x, double y, double width,
double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y+=step;
}
@Override
Public String toString(){
return"Bigplane[x="+x+",y="+y+",width="+width
+",height="+height+",step="+step+"]";
}
}
子弹
public class Bullet{
double x;
double y;
double width;
double height;
double step;
public Bullet(double x, double y, double width,
double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y-=step;
}
@Override
Public String toString(){
return"Bullet[x="+x+",y="+y+",width="+width
+",height="+height+",step="+step+"]";
}
}
英雄机
public class Hero{
double x;
double y;
double width;
double height;
public Bullet(double x, double y, double width,
double height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
/**
*将英雄机移动到鼠标位置x,y
*@param x鼠标位置x
*@param y鼠标位置y
*/
public void move(int x, int y){
this.x=x;
this.y=y;
}
@Override
Public String toString(){
return"Hero[x="+x+",y="+y+",width="+width
+",height="+height+"]";
}
}
天空
public class Sky{
double x;
double y;
double width;
double height;
double step;
public Sky(double x, double y, double width,
double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y+=step;
}
@Override
Public String toString(){
return"Sky[x="+x+",y="+y+",width="+width
+",height="+height+",step="+step+"]";
}
}
测试
public class Demo01{
public static void main(String[] args){
//创建对象
Airplane plane=new Airplane(40,40,50,40,1);
Bigplane plane2=new Bigplane(300,20,150,180,2);
Bullet bullet=new Bullet(300,500,10,20,5);
Hero hero=new Hero(200,600,150,150);
Sky sky=new Sky(0,0,560,800,0.8);
plane.move();
System.out.println(plane);//println方法会自动调用toString
plane.move();
System.out.println(plane);
plane2.move();
System.out.println(plane2);
plane2.move();
System.out.println(plane2);
bullet.move();
System.out.println(bullet);
bullet.move();
System.out.println(bullet);
sky.move();
System.out.println(sky);
sky.move();
System.out.println(sky);
hero.move();
System.out.println(hero);
}
}
2 继承
2.1 什么是继承
特点:
- 子类可以继承父类中的属性和方法
- 子类无需声明就可以获得父类中的属性和方法
- 被继承者是父类(超类 Super class)
- 继承者是子类(派生类 Sub class)
继承实例:
class Super{
int a=5;
public void test(){
System.out.println("test()");
}
}
class Sub extends Super{
int b=6;
}
public class Demo02{
public static void main(String[] args){
Sub sub=new Sub();
System.out.println(sub.a);
System.out.println(sub.b);
sub.test();
}
}
2.2 利用“泛化”实现继承
泛化是指:将子类中共同的属性和方法抽取到父类中的过程。
案例:
父类(超类)抽取子类中公共属性和公共方法:
/*
* 父类(Super Class)
* 声明子类(Sub Class)中公共的属性和方法
*/
public class FlyingObject {
double x;
double y;
double width;
double height;
@Override
public String toString() {
return getClass().getName()+" [x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]";
}
}
子类:
/*
* extends FlyingObject继承于FlyingObject表示当前类继承类
* 父类中的属性和方法
*/
public class Airplane extends FlyingObject{
double step;
public Airplane(double x, double y, double width, double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y +=step;
}
}
public class Bigplane extends FlyingObject{
double step;
public Bigplane(double x, double y, double width, double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y +=step;
}
}
public class Bullet extends FlyingObject{
double step;
public Bullet(double x, double y, double width, double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y -=step;
}
}
public class Hero extends FlyingObject{
public Hero(double x, double y, double width, double height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public void move(double x, double y){
this.x=x;
this.y=y;
}
}
public class Sky extends FlyingObject{
double step;
public Sky(double x, double y, double width, double height, double step){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.step=step;
}
public void move(){
y +=step;
}
}
2.3 Java继承特性
-
单继承:Java不支持类的多继承
- 一个类只能有一个超类
- 一个超类可以有多个子类
-
传递性:子类可以继承全部父类的属性和方法
案例:
public class Demo01 {
public static void main(String[] args) {
Sub1 sub=new Sub1();
System.out.println(sub.a);
}
}
class Koo{
}
class Super{
int a;
}
class Sub1 extends Super{
}
class Sub1 extends Super,Koo{//编译错误,Java不支持多继承}
}
public class Demo02 {
public static void main(String[] args) {
/*
* 继承的传递性
*/
Coo coo=new Coo();
System.out.println(coo.a);
System.out.println(coo.b);
System.out.println(coo.c);
}
}
class Aoo{
int a;
}
class Boo extends Aoo{//Boo包含两个属性a,b
int b;
}
class Coo extends Boo{//Coo包含三个属性a,b,c
int c;
}
2.4 继承中的构造器
构造器不能继承!
关于子类调用父类构造器:
- super()用于子类调用父类构造器
- 在没有写super()的时候,编译器会自动添加super(),自动调用服务无参数构造器
- 可以使用super(参数)调用父类中有参数构造器
- super()只能在子类构造器中使用,只能写在第一行
案例:
public class Demo03 {
/*
* 子类构造器会默认调用父类无参数构造器
*/
public static void main(String[] args) {
Student studet=new Student();
}
}
class Person{
public Person() {
System.out.println("Person()");
}
}
class Student extends Person{
//java 子类中默认构造器形式
public Student() {
//子类构造器会默认调用父类无参数构造器
super();//子类构造器默认存在的语句,不写也有
}
}
当父类没有无参数构造器,同时子类又默认调用父类无参数构造器,则因为无法调用父类无参数构造器,而导致编译错误!解决办法就是子类构造器中使用super调用父类有参数的构造器:
public class Demo04{
public static void main(String[] args){
/*
* 使用super调用父类有参数构造器
*/
Sub sub=new Sub();
}
}
class Super{
public Super(int a){
System.out.println("Super(a)");
}
}
class Sub extends Super{
//子类会自动调用父类无参数构造器
//如果父类型没有无参数构造器,将出现编译错误
//解决办法:使用super调用父类有参数构造器
public Sub(){
super(5);
}
}
飞机大战案例:
/*
* 父类中定义从子类抽取的属性和方法
* 这种抽取方式称为“泛化”
*/
public class FlyingObject {
double x;
double y;
double width;
double height;
public FlyingObject(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public String toString() {
String className=getClass().getName();
return className+" [x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]";
}
}
public class Airplane extends FlyingObject{
double step;
public Airplane(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
public void move(){
y +=step;
}
}
public class Bigplane extends FlyingObject{
double step;
public Bigplane(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
public void move(){
y +=step;
}
}
public class Bullet extends FlyingObject{
double step;
public Bullet(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
public void move(){
y -=step;
}
}
public class Hero extends FlyingObject{
public Hero(double x, double y, double width, double height) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
}
/**
*将英雄机移动到鼠标位置x,y
*@param x鼠标位置x
*@param y鼠标位置y
*/
public void move(int x,int y){
this.x=x;
this.y=y;
}
}
public class Sky extends FlyingObject{
double step;
public Sky(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
public void move(){
y +=step;
}
}
3 方法的继承
3.1 利用继承复用方法
子类不仅可以继承父类的属性,还可以继承父类的方法,继承的意义在于子类复用父类中的方法,将子类中冗余算法抽取到父类中。
3.2 方法重写
子类中定义的与父类签名相同的方法,目的是为了修改从父类继承的方法。
方法重写的语法:子类定义一个与父类“一样”的方法
- 方法名一样
- 参数一样
- 返回值,一般是一样的,可以是更小的子类型
- 修饰词,一般是一样的,可以更加扩大
- 异常,一般是一样的,可以更加具体
- 创建子类对象以后,执行重写方法时候执行的是重写以后的方法。
重写案例
public class Demo02 {
public static void main(String[] args) {
Sub sub=new Sub();
sub.test();
}
}
class Super{
public void test(){
System.out.println("Super,test()");
}
}
class Sub extends Super{
public void test() {
System.out.println("Sub.test()");
}
}
3.3 使用继承重构飞机大战
public class FlyingObject {
double x;
double y;
double width;
double height;
double step:
public FlyingObject(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void move(){
y+=step;
}
@Override
public String toString() {
String className=getClass().getName();
return className+" [x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]";
}
}
public class Airplane extends FlyingObject{
public Airplane(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
}
public class Bigplane extends FlyingObject{
public Bigplane(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
}
public class Sky extends FlyingObject{
public Sky(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
}
public class Bullet extends FlyingObject{
public Bullet(double x, double y, double width, double height, double step) {
//利用super()调用父类有参数构造器,复用了父类中构造器算法
super(x,y,width,height);
this.step = step;
}
/**
*重写继承与超类的move方法,作用就是修改了超类move行为
*超类是向下移动,修改为向上移动
*/
public void move(){
y-=step;
}
}
public class Hero extends FlyingObject{
public Hero(double x, double y, double height, double width) {
super(x,y,width,height);
}
/*
* 重写move方法,空方法,目的是不动
* 修改超类中规定的向下飞,改成不动
*/
public void move() {
}
/*
*将鼠标机移动到鼠标位置X,Y
*@param x鼠标位置x
*@param y鼠标位置y
*/
public void move(int x, int y) {
this.x=x;
this.y=y;
}
}
3.3 super访问父类属性和方法
public class Demo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Sub sub=new Sub();
sub.test();
}
}
class Super{
int a=7;
int c=10;
}
class Sub extends Super{
int a=8;
public void test() {
int a=9;
//局部变量,实例变量,继承的实例变量,存在名字冲突
System.out.println(a);//局部变量
System.out.println(this.a);//当前类中的实例变量a
System.out.println(super.a);//从父类中继承的实例变量a
System.out.println(c);//没有冲突时候,直接访问即可
}
}
访问父类中的方法:
public class Bee extends FlyingObject{
public Bee(double x, double y, double width, double height, double step){
super(x,y,width,height);
this.step=step;
}
/**
*重写父类型move方法修改为斜向飞行
*/
public void move(){
//调用父类型方法,复用父类型定义的算法
super.move();//向下飞行
x++;
}
}
每天对着镜子里的自己说一句:今天的你也很棒!