索千 2023-03-19 15:41 采纳率: 74.4%
浏览 27
已结题

抽象的类方法该如何调用?

想要化简一下代码,该如何实现?希望能具体点

import java.util.Scanner;

abstract class Plane {

    int jy;
    int js;
    float wy;
    int hc;
    int psg;
//    如果要用以下的构造方法,在主方法中如何调用输入?
//    public Plane(int jy, int js, float wy, int hc, int psg) {
//        super();
//        this.jy = jy;
//        this.js = js;
//        this.wy = wy;
//        this.hc = hc;
//        this.psg = psg;
//    }

    public abstract void display();
}

class Fighter extends Plane {

    
    public void display() {

        System.out.println("This is a fighter!");
    }

    private int dd;

    public void setDd(int dd) {

        this.dd = dd;
    }

    public int getDd() {

        return dd;
    }

    @Override
    public String toString() {
        return jy + " " + js + " " + wy + " " + hc + " " + psg + " " + dd;
    }

}

class Bomber extends Plane {

    public void display() {

        System.out.println("This is a bomber!");
    }

    private int zd;

    public void setZd(int zd) {

        this.zd = zd;
    }

    public int getZd() {

        return zd;
    }

    public String toString() {

        return jy + " " + js + " " + wy + " " + hc + " " + psg + " " + zd;
    }
}

public class oj1417 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        Fighter fit = new Fighter();
        Bomber bmb = new Bomber();
        while (sc.hasNext()) {

            char c = sc.next().charAt(0);
            if (c == 'f') {

//                fit.jy = sc.nextInt();
//                fit.js = sc.nextInt();
//                fit.wy = sc.nextFloat();
//                fit.hc = sc.nextInt();
//                fit.psg = sc.nextInt();
//                fit.setDd(sc.nextInt());
//               想要省去上面繁琐的fit.对象,下面的形式该如何实现?
                Plane p2 = new Fighter();
                p2.Plane(sc.nextInt(),sc.nextInt(),sc.nextFloat(),sc.nextInt(),sc.nextInt());
                fit.display();
                System.out.println(fit);
            }
            if (c == 'b') {

                bmb.jy = sc.nextInt();
                bmb.js = sc.nextInt();
                bmb.wy = sc.nextFloat();
                bmb.hc = sc.nextInt();
                bmb.psg = sc.nextInt();
                bmb.setZd(sc.nextInt());
                bmb.display();
                System.out.println(bmb);

            }
        }
        sc.next();
    }
}


  • 写回答

4条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-03-19 16:04
    关注
    • 子类 要 有对应的构造方法,并在子类的构造方法中 调用 super 先初始化父类【即调用父类的构造方法】
    • 参考如下:
    import java.util.Scanner;
    
    abstract class Plane {
        int jy;
        int js;
        float wy;
        int hc;
        int psg;
    
        public Plane(int jy, int js, float wy, int hc, int psg) {
            this.jy = jy;
            this.js = js;
            this.wy = wy;
            this.hc = hc;
            this.psg = psg;
        }
    
        public abstract void display();
    }
    
    class Fighter extends Plane {
    
    
        public Fighter(int jy, int js, float wy, int hc, int psg) {
            super(jy, js, wy, hc, psg);
        }
    
        public void display() {
    
            System.out.println("This is a fighter!");
        }
    
        private int dd;
    
        public void setDd(int dd) {
    
            this.dd = dd;
        }
    
        public int getDd() {
    
            return dd;
        }
    
        @Override
        public String toString() {
            return jy + " " + js + " " + wy + " " + hc + " " + psg + " " + dd;
        }
    
    }
    
    class Bomber extends Plane {
    
        public Bomber(int jy, int js, float wy, int hc, int psg) {
            super(jy, js, wy, hc, psg);
        }
    
        public void display() {
    
            System.out.println("This is a bomber!");
        }
    
        private int zd;
    
        public void setZd(int zd) {
    
            this.zd = zd;
        }
    
        public int getZd() {
    
            return zd;
        }
    
        public String toString() {
    
            return jy + " " + js + " " + wy + " " + hc + " " + psg + " " + zd;
        }
    }
    
    public class oj1417 {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                char c = sc.next().charAt(0);
                if (c == 'f') {
                    Plane fighter = new Fighter(sc.nextInt(), sc.nextInt(), sc.nextFloat(), sc.nextInt(), sc.nextInt());
                    fighter.display();
                    System.out.println(fighter);
                }
                if (c == 'b') {
                    Plane fighter = new Bomber(sc.nextInt(), sc.nextInt(), sc.nextFloat(), sc.nextInt(), sc.nextInt());
                    fighter.display();
                    System.out.println(fighter);
                }
            }
            sc.next();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 已采纳回答 3月20日
  • 创建了问题 3月19日

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存