How to call a class of a class as a parameter in java

for example, I have the following method in the BugReportFactory class:

public static void addFactoryImpl(Class impl) { }

I want to call this method from another class in the following ways:

BugReportFactory.addFactoryImpl(new BugReportFactoryAndroid());

It states that the following argument is not applicable to the Class class.

Can someone tell me my mistake?

ONE MORE QUESTION:

private static IBugReportFactory INSTANCE = null; 
public static void addFactoryImpl(Class impl) { 
INSTANCE = (IBugReportFactory)impl; 
} 

But does it show errors indicating that you cannot expose a class to an object?

+4
source share
8 answers

Try the following, the Objectclass has a getClass()method

BugReportFactory.addFactoryImpl(new BugReportFactoryAndroid().getClass());

or

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

Performs this work.

ONE MORE QUESTION:

private static IBugReportFactory INSTANCE = null; 
public static void addFactoryImpl(Class impl) { 
     INSTANCE = (IBugReportFactory)impl; 
}

But it shows errors indicating that you cannot use

The class is different from another instance. Change your INSTANCEvariable type as Class.

private static Class INSTANCE = null;
public static void addFactoryImpl(Class impl) { 
     INSTANCE = impl; 
}

- . . .

+5
BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);
+4
+1

:

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

:

BugReportFactory.addFactoryImpl((new BugReportFactoryAndroid()).getClass());

.

+1

(ObjectInstance).getClass() ( ).class

+1

, :

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

, :

public static void addFactoryImpl(Object impl) { }

public static <T> void addFactoryImpl(T impl) { }
+1

, , Class . , .

Factory , , , API factory, :

public interface BugReportFactory {
  public A createA();
  public B createB();
}

public class BugReportFactoryAndroid implements BugReportFactory {..}

public class BugReportFactoryIOS implements BugReportFactory {..}

, factory, :

public class Foo {
  private List<BugReportFactory> factories = new ArrayList<BugReportFactory>(); 

  public void addFactoryImpl(BugReportFactory factory) { 
    factories.add(factory);
  }

  public void createAll() {
    for (BugReportFactory f : factories) {
      A a = f.createA(); 
      B b = f.createB();
      ... 
    }
  }
}
+1

, : :

private static IBugReportFactory INSTANCE = null;
public static void addFactoryImpl(Class<?> impl) {
    Object factoryImpl = null;
    try {
        factoryImpl = impl.newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    INSTANCE = (IBugReportFactory) factoryImpl;
}

, :

BugReportFactory.addFactoryImpl(BugReportFactoryAndroid.class);

.

+1

Source: https://habr.com/ru/post/1535312/


All Articles