-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Застрял на добавлении детей в список экземпляра класса. #8
base: master
Are you sure you want to change the base?
Changes from 2 commits
d4be8b6
11300d3
8585730
0101d21
daeb550
c9a1e6c
8441ef1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.gb.FamilyTree; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class FamilyTree { | ||
List<Human> list; | ||
|
||
@Override | ||
public String toString() { | ||
return | ||
"FamilyTree{" + | ||
"list=" + list + | ||
'}'; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ru.gb.FamilyTree; | ||
|
||
public enum Gender { | ||
Male, Female; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ru.gb.FamilyTree; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Human { | ||
private String name; | ||
private LocalDate dob, dod; | ||
private Gender gender; | ||
private List<Human> children; | ||
private Human mother, father; | ||
|
||
public Human(String name, LocalDate dob, LocalDate dod, Gender gender, List<Human> children, Human mother, Human father) { | ||
this.name = name; | ||
this.dob = dob; | ||
this.dod = dod; | ||
this.gender = gender; | ||
this.children = children; | ||
this.mother = mother; | ||
this.father = father; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public LocalDate getDob() { | ||
return dob; | ||
} | ||
|
||
public void setDob(LocalDate dob) { | ||
this.dob = dob; | ||
} | ||
|
||
public LocalDate getDod() { | ||
return dod; | ||
} | ||
|
||
public void setDod(LocalDate dod) { | ||
this.dod = dod; | ||
} | ||
|
||
public Gender getGender() { | ||
return gender; | ||
} | ||
|
||
public void setGender(Gender gender) { | ||
this.gender = gender; | ||
} | ||
|
||
public List<Human> getChildren() { | ||
return children; | ||
} | ||
|
||
public void setChildren(List<Human> children) { | ||
this.children = children; | ||
} | ||
|
||
public Human getMother() { | ||
return mother; | ||
} | ||
|
||
public void setMother(Human mother) { | ||
this.mother = mother; | ||
} | ||
|
||
public Human getFather() { | ||
return father; | ||
} | ||
|
||
public void setFather(Human father) { | ||
this.father = father; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Human{" + | ||
"name='" + name + '\'' + | ||
", dob=" + dob + | ||
", dod=" + dod + | ||
", gender=" + gender + | ||
", children=" + children + | ||
", mother=" + mother + | ||
", father=" + father + | ||
'}'; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ru.gb.FamilyTree; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
Human human1 = new Human("Дмитрий", LocalDate.of(1981,3,17), | ||
null, Gender.Male, null, null,null); | ||
Human human2 = new Human("Валерия", LocalDate.of(2005, 10, 5), | ||
null,Gender.Female,null, null, human1); | ||
Human human3 = new Human("Алина", LocalDate.of(1980,6,4), | ||
null,Gender.Female, null,null,null); | ||
Human human4 = new Human("Наталья", LocalDate.of(1961,6,1), | ||
null, Gender.Female, null, null, null); | ||
Human human5 = new Human("Александр", LocalDate.of(1956,12,9), | ||
null, Gender.Male, null, null, null); | ||
Human human6 = new Human("Маргарита", LocalDate.of(1932,2,23), | ||
null, Gender.Female, null, null, null); | ||
Human human7 = new Human("Борис", LocalDate.of(1931,8,12), | ||
LocalDate.of(2008,8,16), Gender.Male, null, null, null); | ||
|
||
human1.setChildren(List.of(human2)); | ||
|
||
|
||
FamilyTree familyTree = new FamilyTree(); | ||
familyTree.list = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А было бы поле приватно и такая строчка была бы невозможной. В данном случае вы нарушаете инкапсуляцию. Только класс должен решать как ему использовать свои поле. Извне это должно быть невозможно |
||
familyTree.list.add(human1); | ||
familyTree.list.add(human2); | ||
familyTree.list.add(human3); | ||
familyTree.list.add(human4); | ||
familyTree.list.add(human5); | ||
familyTree.list.add(human6); | ||
familyTree.list.add(human7); | ||
|
||
System.out.println(human1); | ||
|
||
|
||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
поля класса почти всегда приватны