我在Java中如何使用继承有什么问题?

时间:2015-03-16 13:41:23

标签: java inheritance super redundancy system.out

很抱歉,我知道此代码可以在这里和那里显着缩短,我真的没有真正了解继承是如何工作的。

我的问题是,我该如何制作,以便我的机器人和机器人课程中的方法不会打印出那些多余的统计数据。

public class FP {
    public void string() {
        System.out.println("A Standard fighting pokemon's stats are:");
    }

    public double weight() {
        double fWeight = 65;
        System.out.println("Weight: " + fWeight);
        return fWeight;
    }

    public double arm() {
        double fArm = 21;
        System.out.println("Arm Reach: " + fArm);
        return fArm;
    }

    public double leg() {
        double fLeg = 22;
        System.out.println("Leg Reach: " +fLeg);
        return fLeg;
    }

    public double punch() {
        double fPunch = 40;
        System.out.println("Punch Damage: " + fPunch);
        return fPunch;
    }

    public double kick() {
        double fKick = 35;
        System.out.println("Kick Damage: " + fKick);
        return fKick;
    }

    public double bodySlam() {
        double fBodySlam = 150;
        System.out.println("Body Slam damage: " + fBodySlam);
        return fBodySlam;
    }
}



public class Machop extends FP {
    public double DMG_MULTI = 1.5;

    public void string() {
        System.out.println("Machop's stats are:");
    }

    public double bodySlam(){
        double fBodySlam = super.bodySlam()*DMG_MULTI;
        System.out.println("Body Slam damage: "+fBodySlam);
        System.out.println();   
        return fBodySlam;
    }
}


public class Machoke extends Machop {
    public void string() {
        System.out.println("Machoke's stats are:");
    }

    public double weight() {
        double fWeight = super.weight()*1.75;
        System.out.println("Weight: " + fWeight);
        return 0;
    }

    public double arm() {
        double fArm = super.arm()*1.75;
        System.out.println("Arm Reach: " + fArm);
        return fArm;
    }

    public double leg() {
        double fLeg = super.leg()*1.75;
        System.out.println("Leg Reach: " +fLeg);
        return fLeg;
    }

    public double punch() {
        double fPunch = super.punch()*1.75;
        System.out.println("Punch Damage: " + fPunch);
        return fPunch;
    }

    public double kick() {
        double fKick = super.kick()*1.75;
        System.out.println("Kick Damage: " + fKick);
        return fKick;
    }

    public double bodySlam() {
        double fBodySlam = super.bodySlam()*2;
        System.out.println("Body Slam damage: " + fBodySlam);
        return fBodySlam;
    }

    public double submission(){
        double fSubmission = super.bodySlam()*1.5;
        System.out.println("Submission damage: " + fSubmission);
        System.out.println();   
        return 0;
    }
}


public class Call {
    public static void main(String args[]) {
        FP fp = new FP();
        fp.string();
        fp.weight();
        fp.arm();
        fp.leg();
        fp.punch();
        fp.kick();
        fp.bodySlam();
        System.out.println();

        Machop a = new Machop();
        a.string();
        a.weight();
        a.arm();
        a.leg();
        a.punch();
        a.kick();
        a.bodySlam();

        Machoke b = new Machoke();
        b.string();
        b.weight();
        b.arm();
        b.leg();
        b.punch();
        b.kick();
        b.bodySlam();
        b.submission();
    }
}

输出:

  

标准格斗小精灵的统计数据是:
  重量:65.0
  Arm Reach:21.0
  腿伸手可及的距离:22.0
  打孔伤害:40.0
  踢伤:35.0
  身体猛击伤害:150.0

     

Machop的统计数据是:
  重量:65.0
  Arm Reach:21.0
  腿伸手可及的距离:22.0
  打孔伤害:40.0
  踢伤:35.0
  身体猛击伤害:150.0
  身体猛击伤害:225.0

     

Machoke的统计数据是:
  重量:65.0
  重量:113.75
  Arm Reach:21.0
  Arm Reach:36.75
  腿伸手可及的距离:22.0
  腿伸手可及的距离:38.5
  打孔伤害:40.0
  冲床损坏:70.0
  踢伤:35.0
  踢伤:61.25
  身体猛击伤害:150.0
  身体猛击伤害:225.0
  身体猛击伤害:450.0
  身体猛击伤害:150.0
  身体猛击伤害:225.0
  提交损坏:337.5

2 个答案:

答案 0 :(得分:1)

你的大多数(如果不是全部)函数都会调用super。因为每个实现都有System.out.println(...)语句,所以输出被阻塞。

通常,在类中调用overriden方法'除非它包含也是覆盖代码一部分的特定逻辑,否则不需要super。

您可以采取一些措施来清理代码。对于初学者,请创建类FP abstract。 (为了便于阅读,您还应该将类重命名为FightingPokemon。)使类抽象将阻止您实例化它。你不想制作FightingPokemon个实例,因为这只是一个包含所有口袋妖怪逻辑的超类。您只需要实际的口袋妖怪实例,例如Machop和Machoke。

其次,您应该删除所有System.out.println代码。在每个类中通过toString的实现替换它,它一次打印所有信息。这也将允许您将这些统计数据与调用函数分开打印。

答案 1 :(得分:0)

删除所有println语句并使用类似

printStats例程
void printStats() {
    System.out.println("A Standard fighting pokemon's stats are:\n" + 
        "weight: " + weight() + "\n" + ...