Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExampleClass {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Horse hr1 = new Horse();
- Horse hr2 = new Horse();
- Horse hr3 = new Horse();
- Horse hr4 = new Horse();
- Set hrSet = new HashSet();
- hrSet.add(hr1);
- hrSet.add(hr2);
- hrSet.add(hr3);
- hrSet.add(hr4);
- Horse hr;
- String hor = "sher_pkg.Horse";
- callHorse(hrSet,hor);
- }
- public static void callHorse(Set xSet,String clsName){
- try {
- Class hrt = Class.forName(clsName);
- Iterator hritr = xSet.iterator();
- while(hritr.hasNext()){
- exam(hrt.cast(hritr.next()));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static void exam(Object obj){ //I want to use exam(Horse hrr)
- System.out.println(obj);
- }
- }
- public class Test {
- public void test(Object obj) {
- if (obj instanceof Horse) {
- Horse c = (Horse) obj;
- noise(c);
- }
- if (obj instanceof Cow) {
- Cow c = (Cow) obj;
- noise(c);
- }
- }
- public void noise(Horse h) {
- System.out.println("Neigh");
- }
- public void noise(Cow c) {
- System.out.println("Moo");
- }
- public static void main(String[] args) {
- Object o1 = new Horse();
- Object o2 = new Cow();
- Test tester = new Test();
- tester.test(o1);
- tester.test(o2);
- }
- }
- class Horse {}
- class Cow {}
- if (obj instanceof Horse) {
- Horse c = (Horse) obj;
- noise(c);
- }
- if (obj instanceof Horse) {
- handleNoise(obj, Horse.class);
- }
- void handleNoise(Object obj, Class clazz) {
- noise(clazz.cast(obj));
- }
- public class Test {
- public void test(Animal obj) {
- obj.noise();
- }
- public static void main(String[] args) {
- Animal o1 = new Horse();
- Animal o2 = new Cow();
- Test tester = new Test();
- tester.test(o1);
- tester.test(o2);
- }
- }
- interface Animal {
- void noise();
- }
- class Horse implements Animal {
- public void noise() {
- System.out.println("Neigh");
- }
- }
- class Cow implements Animal {
- public void noise() {
- System.out.println("Moo");
- }
- }
- if (obj instanceof Cust) {
- loopOverSet(c.getCustPhonSet());
- } else if (obj instanceof Name) {
- loopOverSet(c.getCustNameSet());
- }
- // and so on for the rest...
- void loopOVerSet(Set cxSet) {
- if (cxSet != null && cxSet.size() > 0) {
- Iterator cxSetIterator = cxSet.iterator();
- while (cxSetIterator.hasNext())
- {
- ((StringProp)cxSetIterator.next()).stringProp();
- }
- }
- }
- public static void callHorse(Set<Horse> xSet) {
- Iterator<Horse> hritr = xSet.iterator();
- while (hritr.hasNext()) {
- exam(hritr.next());
- }
- }
- public static void exam(Horse obj) { //I want to use exam(Horse hrr)
- System.out.println(obj);
- }
- exam(Horse hrr)
- try {
- Class hrt = Class.forName(clsName);
- Iterator hritr = xSet.iterator();
- while(hritr.hasNext()){
- exam((Horse)hrt.cast(hritr.next()));
- }
- }
- (final Set xSet<Horse>, final String clsName){
- ...}
- public static void callHorse(Set<?> xSet, String clsName) {
- try {
- Class<?> hrt = Class.forName(clsName);
- for (Object x : xSet) {
- exam(hrt.cast(x));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static void exam(Object obj) {
- System.out.println("Object " + obj);
- }
- public static void exam(Horse obj) {
- System.out.println("Horse " + obj);
- }
- public static void main(String[] args) {
- Set<Horse> horses = new HashSet<Horse>();
- horses.add(new Horse());
- horses.add(new Horse());
- horses.add(new Horse());
- horses.add(new Horse());
- callHorse(horses);
- }
- public static void callHorse(Set<Horse> horses) {
- for (Horse horse : horses) {
- exam(horse);
- }
- }
- public static void exam(Horse horse) {
- System.out.println(horse);
- }
- public static void main(String[] args) {
- Set<Horse> horses = new HashSet<Horse>();
- horses.add(new Horse());
- horses.add(new Horse());
- horses.add(new Horse());
- horses.add(new Horse());
- examineHorses(horses);
- }
- public static void examineHorses(Set<Horse> horses) {
- for (Horse horse : horses) {
- horse.examine();
- }
- }
- // in Horse.java
- public class Horse {
- public void examine() {
- System.out.println(this);
- }
- ...
- }
- import java.lang.reflect.*;
- public class Test {
- public static void exam( Object o ) {
- System.out.println( "Object version called" );
- }
- public static void exam( Test t ) {
- System.out.println( "Test version called" );
- }
- public static void main (String[] args) {
- try {
- // Create an instance of Test but reference it as an Object
- Object untypedTest = new Test();
- // Calling exam directly will invoke the Object version
- exam( untypedTest );
- // But if we use reflection to select the version of exam
- // that takes the desired class name, we can invoke it without
- // even explicitly casting
- String className = "Test";
- Class[] examMethodParams = { Class.forName( className ) };
- Method examMethod = Test.class.getMethod( "exam", examMethodParams );
- Object[] actualParams = { untypedTest };
- examMethod.invoke( null, actualParams );
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- if (obj instanceof Cust)
- {
- Cust c = (Cust) obj;
- Set cxSet = c.getCustPhonSet();
- CustPhon cx;
- if (cxSet != null && cxSet.size() > 0)
- {
- Iterator cxSetIterator = cxSet.iterator();
- while (cxSetIterator.hasNext())
- {
- cx = (CustPhon) cxSetIterator.next();
- this.stringProp(cx);
- }
- }
- //....pattern continues here... CustPhon is replaced by various classes like CustNam etc... Also getCustPhonSet by getCustNamSet etc...
- }
- public void dynamicIteration(Set xlSet, String clsName)
- {
- if (xSet != null && xSet.size() > 0)
- {
- try{
- Class clsinstance = Class.forName(clsName);
- Iterator itr = generalSet.iterator();
- while(itr.hasNext())
- {
- this.stringProp(clsinstance.cast(itr.next()));// See this is wrong.. thats y i posted here by using a simple Horse example
- }
- }catch(ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- }
- //process customer email address
- Set cxSet = c.getCustPhonSet();
- className = "pkg.CustPhon";
- dynamicIteration(cxSet,className);
- // Similarly for other patterns
Add Comment
Please, Sign In to add comment