Created
May 18, 2017 08:37
-
-
Save fermopili/b876d636e353581e5d4a093bf346480e to your computer and use it in GitHub Desktop.
com.javarush.task.task35.task3512 Генератор объектов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3512; | |
public class Event { | |
private static int nextId = 1; | |
private int id; | |
public Event() { | |
id = nextId++; | |
} | |
public int getId() { | |
return id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3512; | |
public class Generator<T> | |
{ | |
Class<T> clazz; | |
public Generator(Class<T> clazz) | |
{ | |
this.clazz = clazz; | |
} | |
T newInstance() throws InstantiationException, IllegalAccessException, IllegalArgumentException | |
{ | |
return (T) clazz.newInstance ( ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.task.task35.task3512; | |
/* | |
Генератор объектов | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws InstantiationException, IllegalAccessException { | |
Generator<Event> eventGenerator = new Generator<>(Event.class); | |
System.out.println(eventGenerator.newInstance().getId()); | |
System.out.println(eventGenerator.newInstance().getId()); | |
System.out.println(eventGenerator.newInstance().getId()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taskKey="com.javarush.task.task35.task3512" | |
Генератор объектов | |
Разберись с текущими классами. | |
Метод newInstance класса Generator должен создавать Event для каждого нового вызова, однако сейчас компилятор ругается на синтаксис. | |
Переделай класс Generator так, что бы конструкция стала рабочей. | |
Подсказка: Для создания объекта потребуется использовать метод Class<T>.newInstance(). | |
Требования: | |
1. Класс Generator должен быть параметризован типом Т. | |
2. Класс Generator должен иметь поле типа Class, которое параметризовано типом Т. | |
3. Класс Generator должен иметь конструктор, который инициализирует поле типа Class. | |
4. Метод newInstance в классе Generator должен создавать и возвращать объект типа Т. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment