Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SandBox;
- public interface IDish {
- }
- 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;
- }
- }
- package SandBox;
- public class Chips implements IDish{
- double potatoes;
- double salt;
- public Chips(double potatoes, double salt) {
- this.potatoes = potatoes;
- this.salt = salt;
- }
- }
- 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));
- }
- public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
- double[] ingredSet = {20, 30, 50};
- Coctail cock = (Coctail) getMyFuckingDish(ingredSet, Coctail.class);
- }
- }
Add Comment
Please, Sign In to add comment