Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions JavaProject.iml
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>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions src/ru/gb/FamilyTree/FamilyTree.java
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поля класса почти всегда приватны


@Override
public String toString() {
return
"FamilyTree{" +
"list=" + list +
'}';
}
}

5 changes: 5 additions & 0 deletions src/ru/gb/FamilyTree/Gender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.gb.FamilyTree;

public enum Gender {
Male, Female;
}
94 changes: 94 additions & 0 deletions src/ru/gb/FamilyTree/Human.java
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 +
'}';
}
}


44 changes: 44 additions & 0 deletions src/ru/gb/FamilyTree/Main.java
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<>();
Copy link
Owner

Choose a reason for hiding this comment

The 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);



}
}