Wednesday, 15 August 2012

java - Generic method that accepts two types of List -


I want to write a function that can accept two identical types. But not the same

  MyClassA {abc () a2b ()} MyClassB {abc () a3b ()}   

One of two methods is the same The function should accept any of these two types and I want to invite ABC () on the object in the list.

It does not help:

  Private & LT; T & gt; Set up & lt; MyclassX & gt; CreateObject (list & lt; T & gt; class) {set & lt; MyclassX & gt; X; If (t instanceof MyClassA) {for (MyClassA a: classes) {if (a.abc (). Equals ("somethig")} {x.add (t.abc ()); }}} Return x; }   

I do not even need to check instanceof . I just need to iterate through the list and compare the values. If a match occurs, then the method abc ()

call one Create a general interface and apply it to MyClassA and MyClassB:

  Interface MyClass {void abc (); }    

No comments:

Post a Comment