Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SandBox;
- public class Coctail implements IDish {
- double water;
- double alcohol;
- double topping;
- public Coctail(double water, double alcohol, double topping) {
- this.water = water;
- this.alcohol = alcohol;
- this.topping = topping;
- }
- @Override
- public String toString() {
- return "Coctail with " + alcohol + " ml buxla";
- }
- }
- package SandBox;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- public class DishFactory {
- public static IDish getMyFuckingDish(double[] ingredients, Class<? extends IDish> dishType) throws IllegalAccessException, InvocationTargetException, InstantiationException {
- Constructor<?>[] cons = dishType.getConstructors();
- //return dishType.cast(cons[0].newInstance((Object) ingredients));
- return dishType.cast(cons[0].newInstance(ingredients[0], ingredients[1], ingredients[2]));
- }
- public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
- double[] ingredSet = {20, 30, 50};
- Coctail cock = (Coctail) getMyFuckingDish(ingredSet, Coctail.class);
- System.out.println(cock);
- }
- }
Add Comment
Please, Sign In to add comment