Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSpring中的接口类问题_Java_Spring_Spring Mvc - Fatal编程技术网

JavaSpring中的接口类问题

JavaSpring中的接口类问题,java,spring,spring-mvc,Java,Spring,Spring Mvc,我尝试学习Spring框架。我有一些问题 我创建了一个控制器和几个类。这是控制器: @Controller @RequestMapping("/man") public class manController { private SwordImp Sword = new SwordImp(); private GunImp Gun = new GunImp(); private String mainWeapon; private String subWeapon

我尝试学习Spring框架。我有一些问题

  • 我创建了一个控制器和几个类。这是控制器:

    @Controller
    @RequestMapping("/man")
    public class manController {
        private SwordImp Sword = new SwordImp();
        private GunImp Gun = new GunImp();
        private String mainWeapon;
        private String subWeapon;
        @RequestMapping(value = "set/{weapon:[a-z A-Z 0-9]+}", method = RequestMethod.GET)
        public String setWeapon(@PathVariable String weapon, Model model){
            System.out.println(weapon);
            if(weapon.equals("gun")){
                Gun.set(weapon);
                mainWeapon = Gun.getWeapon();
                subWeapon = Gun.getSubWeapon();
            }else{
                if(weapon.equals("sword")){
                    Sword.set(weapon);
                    mainWeapon = Sword.getWeapon();
                    subWeapon = Sword.getSubWeapon();
                }else{
                    mainWeapon = "no weapon";
                    subWeapon = "no sub weapon";
                }
            }
            model.addAttribute("weapon_status", mainWeapon);
            model.addAttribute("sub_weapon_status", subWeapon);
            return "man/index";
        }
    }
    
  • 我还创建了一些类

    武器接口

    public interface Weapon {
        public void set(String weaponName);
        public String getWeapon();
        public String getSubWeapon();
    }
    
    public interface Weapon {
        private String weaponName = null;
        private String bullet = null;
        public void set(String weaponName);
        public String getWeapon();
        public String getSubWeapon();
    }
    
    剑类

    public class SwordImp implements Weapon {
        private String weaponName = null;   
        public void set(String weapon) {
            this.weaponName = "fire "+weapon;
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return "no sub weapon";
        }    
    }
    
    public class SwordImp {...}
    
    public class SwordImp implements Weapon {   
        public void set(String weapon) {
            this.weaponName = "fire "+weapon;
            this.bullet = "no sub weapon";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }    
    }
    
    枪类

    public class GunImp implements Weapon {
        private String weaponName = null;
        private String bullet = null;
        public void set(String weapon) {
            this.weaponName = "ice "+weapon;
            this.bullet = "need bullet";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }
    }
    
    public class GunImp  {...}
    
    public class GunImp implements Weapon {
        public void set(String weapon) {
            this.weaponName = "ice "+weapon;
            this.bullet = "need bullet";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }
    }
    
  • 我的问题是:

  • 如果我不在下面的枪类和剑类中实现武器类,看起来这个函数仍然可以工作。。。那么为什么我们需要使用接口呢

    剑类

    public class SwordImp implements Weapon {
        private String weaponName = null;   
        public void set(String weapon) {
            this.weaponName = "fire "+weapon;
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return "no sub weapon";
        }    
    }
    
    public class SwordImp {...}
    
    public class SwordImp implements Weapon {   
        public void set(String weapon) {
            this.weaponName = "fire "+weapon;
            this.bullet = "no sub weapon";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }    
    }
    
    枪类

    public class GunImp implements Weapon {
        private String weaponName = null;
        private String bullet = null;
        public void set(String weapon) {
            this.weaponName = "ice "+weapon;
            this.bullet = "need bullet";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }
    }
    
    public class GunImp  {...}
    
    public class GunImp implements Weapon {
        public void set(String weapon) {
            this.weaponName = "ice "+weapon;
            this.bullet = "need bullet";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }
    }
    
  • 我尝试将“所有”类放入“存储库”文件夹。这条路对吗?或者我需要将它们放入模型文件夹中吗

  • 首先,我尝试将Gun类和Swarm类中的weaponName变量和bullet变量放入武器接口,因此我不需要在每个类中声明它们,如下所示:

    武器接口

    public interface Weapon {
        public void set(String weaponName);
        public String getWeapon();
        public String getSubWeapon();
    }
    
    public interface Weapon {
        private String weaponName = null;
        private String bullet = null;
        public void set(String weaponName);
        public String getWeapon();
        public String getSubWeapon();
    }
    
    剑类

    public class SwordImp implements Weapon {
        private String weaponName = null;   
        public void set(String weapon) {
            this.weaponName = "fire "+weapon;
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return "no sub weapon";
        }    
    }
    
    public class SwordImp {...}
    
    public class SwordImp implements Weapon {   
        public void set(String weapon) {
            this.weaponName = "fire "+weapon;
            this.bullet = "no sub weapon";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }    
    }
    
    枪类

    public class GunImp implements Weapon {
        private String weaponName = null;
        private String bullet = null;
        public void set(String weapon) {
            this.weaponName = "ice "+weapon;
            this.bullet = "need bullet";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }
    }
    
    public class GunImp  {...}
    
    public class GunImp implements Weapon {
        public void set(String weapon) {
            this.weaponName = "ice "+weapon;
            this.bullet = "need bullet";
        }
        public String getWeapon() {
            return this.weaponName;
        }
        public String getSubWeapon() {
            return this.bullet;
        }
    }
    
  • 但这似乎是错误的。。。原因是什么

  • 您可以使用抽象工厂模式在运行时注入特定武器,而不是在控制器中硬编码它们
  • 考虑到您有100种不同的武器,很难将所有武器实现类添加到控制器中。您可以在google中找到此模式的示例和实现。这被认为是一种最佳做法

  • 尝试遵循最佳实践。它将帮助您更好地学习和编写整洁且可维护的代码。是的,移动到模型文件夹更好

  • 在OOP中,创建定义良好的对象非常重要。接口只能有常量字段和方法。无法更改接口中定义的状态


  • 请阅读一些OOP的理论。您对类和接口的理解似乎非常有限,因此关于设计模式的理论讨论毫无意义。我认为阅读一些OOP和控制反转IOC对您很有用,我认为理解接口使用的最好方法是学习一些设计模式