Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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
如何在java中生成动态全局变量_Java_Oop - Fatal编程技术网

如何在java中生成动态全局变量

如何在java中生成动态全局变量,java,oop,Java,Oop,您好,我是java新手,我想创建一个变量,它是基于字符串条件的不同类的对象。我不知道该怎么做 这就是我想解释的 public class Foo { Object obj; // I want this variable to be dynamic based on the condition in the constructor public Foo(String str){ // Constructor if(str.equals("bar")){

您好,我是java新手,我想创建一个变量,它是基于字符串条件的不同类的对象。我不知道该怎么做

这就是我想解释的

public class Foo {
    Object obj; // I want this variable to be dynamic based on the condition in the constructor
    public Foo(String str){ // Constructor
        if(str.equals("bar")){
            this.obj = new Bar();
        }
        else{
            this.obj = new Baz();
        }
    }

我希望这样做,因为稍后我将使用这个obj变量调用Bar或Baz中的方法,这两种方法都实现了相同名称但代码不同的方法。

请检查此java代码,您将了解如何使用接口来解决此问题

package com.company;

public class Main {
    public static void main(String[] args) {
        Foo foo = new Foo("bar");
        foo.obj.hello();
    }
}

class Foo {
    Ba obj;

    Foo(String str) { // Constructor
        if (str.equals("bar")) {
            this.obj = new Bar();
        } else {
            this.obj = new Baz();
        }
    }
}

interface Ba {
    void hello();
}

class Bar implements Ba {
    public void hello(){
        System.out.println(" Hello Bar");
    }
}

class Baz implements Ba {
    public void hello(){
        System.out.println(" Hello Baz");
    }
}

听起来像是战略模式,这里有一个粗略的解释:

public class Foo {
    Map<String, Common> strategyPattern = new HashMap<>();

    private Common obj;

    {
        // set this up how you want, this is a static code block common to all classes 
        // if you're using spring you can autowire these
        // Bar and Baz both implement Common interface
        strategyPattern.put("bar", new Bar());
        strategyPattern.put("bar", new Baz());
    }

    public Foo(String str) { // Constructor
        this.obj = strategyPattern.get(str);
    }
}
公共类Foo{
Map strategyPattern=newhashmap();
私人和公共obj;
{
//这是所有类通用的静态代码块
//如果您使用的是spring,则可以自动连接这些
//Bar和Baz都实现了公共接口
strategyPattern.put(“条”,新条());
strategyPattern.put(“bar”,新Baz());
}
公共Foo(字符串str){//构造函数
this.obj=strategyPattern.get(str);
}
}

最好的方法是创建接口而不是对象。 假设TestInterface是一个接口,其方法与Bar和Buz中的方法相同

接口测试接口{

        testMethod1();

        testMethod2();

        }

        class Bar implements TestInterface{
         testMethod1(){
          // Your  code
        }

        testMethod1(){
          // Your  code
        }

        }


        class Baz implements TestInterface{
         testMethod1(){
          // Your  code
        }

        testMethod2(){
          // Your  code
        }

        }

        public class Foo {
            TestInterface obj; // Here change object to interface 
            public Foo(String str){ // Constructor
                if(str.equals("bar")){
                    this.obj = new Bar();
                }
                else{
                    this.obj = new Baz();
                }
            }




        Foo foo = new Foo("Bar");

听起来Bar和Baz都应该实现一个公共接口……多亏了你们两位,我以前从未听说过这个接口,它解决了我的问题:)