Commit f135c443 by Patryk Czarnik

unmodifiableList

parent 804288b8
...@@ -26,7 +26,7 @@ public class Program { ...@@ -26,7 +26,7 @@ public class Program {
// próba modyfikacji listy za pośrednictwem gettera // próba modyfikacji listy za pośrednictwem gettera
List<Integer> lista = student.getOceny(); List<Integer> lista = student.getOceny();
System.out.println(lista); System.out.println(lista);
lista.removeIf(x -> x < 4); // usuń wszystkie oceny gorsze niż 4 lista.removeIf(x -> x < 4); // usuń wszystkie oceny gorsze niż 4 - w wersji unmodifiable wyrzuca wyjątek
System.out.println(lista); System.out.println(lista);
System.out.println("średnia: " + student.sredniaOcen()); System.out.println("średnia: " + student.sredniaOcen());
} }
......
...@@ -2,6 +2,7 @@ package p11_klasy.enkapsulacja; ...@@ -2,6 +2,7 @@ package p11_klasy.enkapsulacja;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class Student extends Osoba { public class Student extends Osoba {
...@@ -41,7 +42,8 @@ public class Student extends Osoba { ...@@ -41,7 +42,8 @@ public class Student extends Osoba {
} }
public List<Integer> getOceny() { public List<Integer> getOceny() {
return oceny; // zwracamy obiekt, poprzez który nie jest możliwa modyfikacja
return Collections.unmodifiableList(oceny);
} }
public double sredniaOcen() { public double sredniaOcen() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment