会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3602楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3604楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3606楼

class Animal {
    public void shout() {
        System.out.println("叫了一声!");
    }
}
class Dog extends Animal {
    public void shout() {
        System.out.println("旺旺旺!");
    }
    public void seeDoor() {
        System.out.println("看门中....");
    }
}
class Cat extends Animal {
    public void shout() {
        System.out.println("喵喵喵喵!");
    }
}
public class TestPolym {
    public static void main(String[] args) {
        Animal a1 = new Cat(); // 向上可以自动转型
        //传的具体是哪一个类就调用哪一个类的方法。大大提高了程序的可扩展性。
        animalCry(a1);
        Animal a2 = new Dog();
        animalCry(a2);//a2为编译类型,Dog对象才是运行时类型。
         
        //编写程序时,如果想调用运行时类型的方法,只能进行强制类型转换。
        // 否则通不过编译器的检查。
        Dog dog = (Dog)a2;//向下需要强制类型转换
        dog.seeDoor();
    }
 
    // 有了多态,只需要让增加的这个类继承Animal类就可以了。
    static void animalCry(Animal a) {
        a.shout();
    }
 
    /* 如果没有多态,我们这里需要写很多重载的方法。
     * 每增加一种动物,就需要重载一种动物的喊叫方法。非常麻烦。
    static void animalCry(Dog d) {
        d.shout();
    }
    static void animalCry(Cat c) {
        c.shout();
    }*/
}

老师,这是学习材料里的代码,我可以理解它的意思但是我不理解为什么要引入多态,对于这个例子为什么不直接使用

        Dog d = new Dog();
        Cat c = new Cat();
        d.shout();
        c.shout();

这样的调用来实现,不需要重载动物的喊叫方法啊

JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3608楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3610楼




我用了数组来画炮弹,炮弹发射的角度是任意的,但运行了好几次却发现炮弹就成一条直线了,是哪里出了问题呢

image.png


Shell.java


package zzu.baizhan.Feb.Day20th;

import java.awt.*;
import java.util.Random;

public class Shell extends GameObject {
   double degree;

   public Shell() {
       x = 200;
       y = 200;
       width = 10;
       height = 10;
       speed = 3;

       degree = Math.random()*Math.PI*2;
   }

   public void draw(Graphics g) {
       Color c = g.getColor();
       g.setColor(Color.YELLOW);
       g.fillOval((int) x, (int) y, width, height);
       //炮弹沿着任意角度去飞
       x += speed * Math.cos(degree);
       y += speed * Math.sin(degree);

       if (x < 0 || x > Constant.GAME_WIDTH - width) {
           degree = Math.PI - degree;
       }
       if (y < 30 || y < Constant.GAME_HEIGHT - height) {
           degree = -degree;
       }

       g.setColor(c);
   }

}


package zzu.baizhan.Feb.Day20th;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Feb20thPlane extends JFrame {

    Image ball = Feb20thGameUtil.getImage("Images/ball.png");
    Image bg = Feb20thGameUtil.getImage("Images/bg.jpg");
    Image plane = Feb20thGameUtil.getImage("Images/plane.png");
    Plane p=new Plane(plane,250,250);

    Shell []shells=new Shell[50];

    int planeX=250;
    int planeY=250;

    @Override
    public void paint(Graphics g) { //自动被调用,相当于画笔

        g.drawImage(bg, 0, 0, null);
        g.drawImage(plane, planeX, planeY, null);
        p.drawSelf(g);
        for (int i = 0; i < shells.length; i++) {
            shells[i].draw(g);
        }
//        Color c=g.getColor();
//
//        Font f=g.getFont();
//        g.setColor(Color.BLUE);
//
//        g.drawLine(100,100,300,300);
//        g.drawRect(100,100,300,300);
//        g.drawOval(100,100,300,300);
////        g.drawArc(100,100,200,200,300,300);
//        g.fillRect(100,100,100,100);
//        g.setColor(Color.RED);
//        g.setFont(new Font("宋体",Font.BOLD,50));
//        g.drawString("我是谁?",200,200);
//
//        g.drawImage(ball,250,250,null);
//
//        g.setColor(c);
//        g.setFont(f);


    }
    class keyMonitor extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            p.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            p.minusDirection(e);
        }
    }

    public void launchFrame() {
        this.setTitle("Feb20th");
        this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
        this.setLocation(100, 100);
        setVisible(true);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();//启动重画窗口的线程
        addKeyListener(new keyMonitor());//给窗口增加键盘的监听

        //初始化50个炮弹
        for (int i = 0; i < shells.length; i++) {
            shells[i] = new Shell();
        }
    }


    public static void main(String[] args) {
        Feb20thPlane f = new Feb20thPlane();
        f.launchFrame();
    }


    class PaintThread extends Thread {
        @Override
        public void run() {
            while (true) {
                repaint();
                try {
                    Thread.sleep(40); //1s=1000ms
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3611楼

我用了数组来画炮弹,炮弹发射的角度是任意的,但运行了好几次却发现炮弹就成一条直线了,是哪里出了问题呢


image.png



Shell.java

zzu.baizhan.Feb.Day20th;

java.awt.*;
java.util.Random;

Shell GameObject {
    ;

    Shell() {
        = ;
        = ;
        = ;
        = ;
        = ;

        = Math.()*Math.*;
    }

    draw(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.);
        g.fillOval(() , () , , );
        += * Math.();
        += * Math.();

        (< || > Constant.- ) {
            = Math.- ;
        }
        (< || < Constant.- ) {
            = -;
        }

        g.setColor(c);
    }
}


main:

zzu.baizhan.Feb.Day20th;

javax.swing.*;
java.awt.*;
java.awt.event.KeyAdapter;
java.awt.event.KeyEvent;
java.awt.event.WindowAdapter;
java.awt.event.WindowEvent;

Feb20thPlane JFrame {

    Image = Feb20thGameUtil.();
    Image = Feb20thGameUtil.();
    Image = Feb20thGameUtil.();
    Plane =Plane(,,);

    Shell []=Shell[];

    =;
    =;

    paint(Graphics g) { g.drawImage(, , , );
        g.drawImage(, , , );
        .drawSelf(g);
        (i = ; i < .; i++) {
            [i].draw(g);
        }
}
    keyMonitor KeyAdapter {
        keyPressed(KeyEvent e) {
            .addDirection(e);
        }

        keyReleased(KeyEvent e) {
            .minusDirection(e);
        }
    }

    launchFrame() {
        .setTitle();
        .setSize(Constant., Constant.);
        .setLocation(, );
        setVisible();

        .addWindowListener(WindowAdapter() {
            windowClosing(WindowEvent e) {
                System.();
            }
        });
        PaintThread().start();addKeyListener(keyMonitor());(i = ; i < .; i++) {
            [i] = Shell();
        }
    }


    main(String[] args) {
        Feb20thPlane f = Feb20thPlane();
        f.launchFrame();
    }


    PaintThread Thread {
        run() {
            () {
                repaint();
                {
                    Thread.(); } (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}



JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3612楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3614楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637