如何创建两个类和标记方法的接口,我可以使用哪些?

时间:2014-12-03 09:09:17

标签: java interface extends implements

class Rifle {
    void shoot() {
        System.out.println("rifle shoot");
    }

    void reload() {
        System.out.println("rifle reload");
    }

    void onlyRifle() {
        System.out.println("rifle!");
    }
}

class Shotgun {

    void shoot() {
        System.out.println("shotgun shoot");
    }

    void reload() {
        System.out.println("shotgun reload");
    }

    void onlyShotgun() {
        System.out.println("shotgun!");
    }
}

我需要类武器,它将使用来自Rifle的方法shoot()和来自Shotgun的方法reload() 我怎么能用界面呢? 想象一下,我可以修改类步枪和霰弹枪类

我的错误认识是

class Weapon {
    Rifle myRifle;
    Shotgun myShotgun;

    Weapon() {
        myRifle = new Rifle();
        myShotgun = new Shotgun();
    }

    void shoot() {
        myRifle.shoot();
    }

    void reload() {
        myShotgun.reload();
    }

    void onlyShotgun() {
        myShotgun.onlyShotgun();
    }

    void onlyRifle() {
        myRifle.onlyRifle();
    }
}

...main(){
    Weapon myWeapon = new Weapon();
    myWeapon.shoot();
    myWeapon.reload();
    myWeapon.onlyRifle();
    myWeapon.onlyShotgun();
}

我想要的那项工作,但我怎样才能更简单地使用界面?

4 个答案:

答案 0 :(得分:1)

Weapon应该是界面

interface Weapon
{
     void shoot();
     void reload();
}

然后在具体实例中,实现该接口:

class Rifle implements Weapon
{
    /*ToDo - implement shoot and reload*/

    void onlyRifle() {
        /*ToDo - specific code here*/
    }
}

然后,假设您rRifle的实例,则可以将r添加到Weapon的集合中。您不太可能需要onlyRifleRifle - 具体的内容只需要在构建时传递。

答案 1 :(得分:1)

尝试使用类似于使用的合成并使用如下界面:

interface Weapon {
 void shoot();
 void reload();
}

class ModifiedRifle implements Weapon {
     Rifle myRifle;
     Shotgun myShotgun;
     public ModifiedRifle (Rifle rifle, Shotgun shotgun) {
          this.myRifle = rifle();
          this.myShotgun = shotgun;
     }

     public void shoot() {
         myRifle.shoot();
     }

     public void reload() {
         myShotgun.reload();
     }
}

答案 2 :(得分:0)

一种方法是定义界面武器并拥有2个新类,让我们称之为MyRifle,它扩展了Rifle并实现了Weapon和MySHotgun,它扩展了Shotgun并实现了武器

要求您扩展现有类,因为您提到您无法修改现有类

所以

interface Weapon
{
     void shoot();
     void reload();
}

class MyRifle extends Rifle implements Weapon{
// actual implementations of your methods
}

答案 3 :(得分:0)

你有一个超级武器,它采用了WeaponType。你的WeaponType是一个具有射击和重载方法的界面。

public class Weapon {

WeaponType weaponType;

    public Weapon(WeaponType weaponType) {

        this.weaponType = weaponType;
    }

    public void shoot() {

        this.weaponType.shoot();
    }
    public void reload() {

        this.weaponType.reload();
    }
}

您的界面:

public interface WeaponType {

    public void shoot();
    public void reload();
}

你的霰弹枪课程:

    public class Shotgun implements WeaponType {

    @Override
    public void shoot() {

        System.out.println("Shotgun shoot!");
    }

    @Override
    public void reload() {

        System.out.println("Shotgun reload!");
    }
}

使用示例:

Weapon myShotgun = new Weapon(new Shotgun());
myShotgun.shoot();
myShotgun.reload();