开发者

Using an interface as a constructor parameter in Java?

How would I be able to accomplish the following:

public class testClass implements Interface {
     public testClass(Interface[] args) {
     }
}

So that I could declare

Interface testObject = new testClass(new class1(4), new class2(5));

Where c开发者_运维知识库lass1 and class2 are also classes that implement Interface.

Also, once I accomplish this, how would I be able to refer to each individual parameter taken in to be used in testClass?

Thanks :)


So that I could declare

Interface testObject = new testClass(new class1(4), new class2(5));

You need to use varargs in testClass constructor:

public testClass (Interface ... args) {
   for (Interface i : args) {
      doSmthWithInterface (i);
   }
}


You can use varargs, which are treated as arrays. For example:

public testClass(Interface... args) {
    System.out.println(args[0]);
}


Like this (save the whole sample into a file, say testClass.java):

interface Interface{}

public class testClass implements Interface 
{
     public testClass(Interface ... args) 
     {
        System.out.println("\nargs count = " + args.length);
        for( Interface i : args )
        {
            System.out.println( i.toString() );
        }
     }

     public static void main(String[] args)
     {
         new testClass(
          new Interface(){}, // has no toString() method, so it will print gibberish
          new Interface(){ public String toString(){return "I'm alive!"; } }, 
          new Interface(){ public String toString(){return "me, too"; } }
         );

         new testClass(); // the compiler will create a zero-length array as argument
     }
}

The output will be as follows:

C:\temp>javac testClass.java

C:\temp>java testClass

args count = 3
testClass$1@1f6a7b9
I'm alive!
me, too

args count = 0


You don't have to use varargs, you can use an array as an input parameter, varargs is basically just a fancy new syntax for an array parameter, but it will help prevent you from having to construct your own array in the calling class.
i.e. varargs allow (parm1, parm2) to be received into an array structure

You cannot use an interface to enforce a Constructor, you should probably use a common abstract super class with the desired constructor.

public abstract class Supa {

    private Supa[] components = null;

    public Supa(Supa... args) {
        components = args;
    }

}
public class TestClass extends Supa {

    public TestClass(Supa... args) {
        super(args);
    }

    public static void main(String[] args) {
        Supa supa = new TestClass(new Class1(4), new Class2(5));
            // Class1 & Class2 similarly extend Supa
    }
}

Also see the composite design pattern http://en.wikipedia.org/wiki/Composite_pattern

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜