Created
February 14, 2017 07:31
-
-
Save f3ath/de2a50b2320f3c1b8a162ef8670f2ed5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
Баг в initializeIdAndName | |
1. Подумать, что в программе неправильно. | |
2. Вынести реализацию метода initializeIdAndName в класс User. | |
3. initializeIdAndName в классе User должен возвращать тип User. | |
4. Поправить программу, чтобы компилировалась и работала. | |
Такой код изначальный. | |
*/ | |
public class Solution { | |
public static void main(String[] args) throws Exception { | |
System.out.println(Matrix.NEO); | |
System.out.println(Matrix.TRINITY); | |
} | |
static class Matrix { | |
public static DBObject NEO = new User().initializeIdAndName(1, "Neo"); | |
public static DBObject TRINITY = new User().initializeIdAndName(2, "Trinity"); | |
} | |
interface DBObject { | |
DBObject initializeIdAndName(long id, String name) { | |
this.id = id; | |
this.name = name; | |
return this; | |
} | |
} | |
static class User implements DBObject { | |
long id; | |
String name; | |
@Override | |
public String toString() { | |
return String.format("User has name %s, id = %d", name, id); | |
} | |
} | |
} | |
Я понимаю, что в интерфейсе надо метод DBObject initializeIdAndName сделать void, убрать из него тело метода, поставить там ; сам метод инициализировать в теле класса, но вот что должен возвращать return я никак не могу понять. | |
Что-то типа такого. Но если честно, я не слишком понимаю логику того, что там происходит. Есть кто знающий? | |
public class Solution { | |
public static void main(String[] args) throws Exception { | |
System.out.println(Matrix.NEO); | |
System.out.println(Matrix.TRINITY); | |
} | |
static class Matrix { | |
public static DBObject NEO = new User().initializeIdAndName(1, "Neo"); | |
public static DBObject TRINITY = new User().initializeIdAndName(2, "Trinity"); | |
} | |
interface DBObject { | |
DBObject initializeIdAndName(long id, String name); | |
} | |
static class User implements DBObject { | |
long id; | |
String name; | |
DBObject initializeIdAndName(long id, String name) { | |
this.id = id; | |
this.name = name; | |
return ; | |
} | |
@Override | |
public String toString() { | |
return String.format("User has name %s, id = %d", name, id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment