Commit 77ded6b9 by Patryk Czarnik

dodatkowe przykłady dnia 1

parent 228dad10
...@@ -9,7 +9,7 @@ public class Osoba { ...@@ -9,7 +9,7 @@ public class Osoba {
String imie, nazwisko; String imie, nazwisko;
int wiek; int wiek;
// Klasa może posiadać wiele konstruktorów, które różnią się liczbą lub typem parametrów // Klasa może posiadać wiele konstruktorów, które różnią się liczbą lub typem parametrów
Osoba() { Osoba() {
} }
......
...@@ -14,7 +14,7 @@ public class Polimorfizm1 { ...@@ -14,7 +14,7 @@ public class Polimorfizm1 {
System.out.println(); System.out.println();
// Nawet, gdy zmienna jest typu Osoba, ale w tej zmiennej jest referencja do obiektu klasy Student, // Nawet, gdy zmienna jest typu Osoba, ale w tej zmiennej jest referencja do obiektu klasy Student,
// to przy wywołaniu metody wykona się wersja z klasy Student. // to przy wywołaniu metody wykona się wersja z klasy Student.
// W mechanizmie override decydujące znaczenie ma typ obiektu w czasie wykonania. // W mechanizmie override decydujące znaczenie ma typ obiektu w czasie wykonania.
Osoba ktos; Osoba ktos;
......
...@@ -45,7 +45,7 @@ public class Student extends Osoba { ...@@ -45,7 +45,7 @@ public class Student extends Osoba {
// czyli dla istniejących metoda podać inną treść. // czyli dla istniejących metoda podać inną treść.
void przedstawSie() { void przedstawSie() {
System.out.println("Hej tu " + imie + " " + nazwisko + ", studiuję " + kierunek + ", jestem na " + rok + " roku"); System.out.println("Hej tu " + imie + " " + nazwisko + ", studiuję " + kierunek + ", jestem na " + rok + " roku");
} }
// Od Javy 5 przed definicją metody, która nadpisuje definicję z nadklasy, można umieścić adnotację @Override // Od Javy 5 przed definicją metody, która nadpisuje definicję z nadklasy, można umieścić adnotację @Override
......
...@@ -26,7 +26,6 @@ public class Usuwanie3_gc { ...@@ -26,7 +26,6 @@ public class Usuwanie3_gc {
} }
System.out.println("Koniec programu"); System.out.println("Koniec programu");
} }
} }
...@@ -21,10 +21,12 @@ public class Porownywanie1 { ...@@ -21,10 +21,12 @@ public class Porownywanie1 {
A a2 = new A(10, 20); A a2 = new A(10, 20);
A a3 = new A(10, 30); A a3 = new A(10, 30);
System.out.println(a1 == a1);
System.out.println(a1 == a2); System.out.println(a1 == a2);
System.out.println(a1 == a3); System.out.println(a1 == a3);
System.out.println(); System.out.println();
System.out.println(a1.equals(a1));
System.out.println(a1.equals(a2)); System.out.println(a1.equals(a2));
System.out.println(a1.equals(a3)); System.out.println(a1.equals(a3));
......
...@@ -37,11 +37,13 @@ public class Porownywanie2 { ...@@ -37,11 +37,13 @@ public class Porownywanie2 {
System.out.println(b2.toString()); System.out.println(b2.toString());
System.out.println(b3.toString()); System.out.println(b3.toString());
System.out.println(b1 == b1);
System.out.println(b1 == b2); System.out.println(b1 == b2);
System.out.println(b1 == b3); System.out.println(b1 == b3);
System.out.println(); System.out.println();
// chociaż nieprawidłowo zaimplementowałem equals w klasie B, to ten test działa // chociaż nieprawidłowo zaimplementowałem equals w klasie B, to ten test działa
System.out.println(b1.equals(b1));
System.out.println(b1.equals(b2)); System.out.println(b1.equals(b2));
System.out.println(b1.equals(b3)); System.out.println(b1.equals(b3));
System.out.println(); System.out.println();
......
package gotowe.p21_metody_klasy_object.porownywanie; package gotowe.p21_metody_klasy_object.porownywanie;
import java.util.HashSet; import java.util.HashSet;
import java.util.concurrent.ThreadLocalRandom;
class C { class C {
int x, y; int x, y;
...@@ -29,6 +30,7 @@ class C { ...@@ -29,6 +30,7 @@ class C {
public int hashCode() { public int hashCode() {
// taki hashCode jest poprawny, w tym sensie, że spełnia minimalne wymagania kontraktu // taki hashCode jest poprawny, w tym sensie, że spełnia minimalne wymagania kontraktu
// zazwyczaj taka implementacja jest niewydajna i w praktyce tak się nie robi // zazwyczaj taka implementacja jest niewydajna i w praktyce tak się nie robi
// return ThreadLocalRandom.current().nextInt(2);
return 0; return 0;
} }
} }
...@@ -44,11 +46,13 @@ public class Porownywanie3 { ...@@ -44,11 +46,13 @@ public class Porownywanie3 {
System.out.println(c2.toString()); System.out.println(c2.toString());
System.out.println(c3.toString()); System.out.println(c3.toString());
System.out.println(c1 == c1);
System.out.println(c1 == c2); System.out.println(c1 == c2);
System.out.println(c1 == c3); System.out.println(c1 == c3);
System.out.println(); System.out.println();
// chociaż nieprawidłowo zaimplementowałem equals w klasie B, to ten test działa // chociaż nieprawidłowo zaimplementowałem equals w klasie B, to ten test działa
System.out.println(c1.equals(c1));
System.out.println(c1.equals(c2)); System.out.println(c1.equals(c2));
System.out.println(c1.equals(c3)); System.out.println(c1.equals(c3));
System.out.println(); System.out.println();
......
package wydajnosc; package na_zywo;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
......
package na_zywo;
import java.util.concurrent.atomic.AtomicInteger;
public class Mutowalnosc1 {
static void test1(int x) {
System.out.println("test1 x = " + x);
x += 1;
System.out.println("test1 x = " + x);
}
static void test2(Integer y) {
System.out.println("test2 y = " + y);
y += 1;
// y = y+1 , a tak naprawdę y = new Integer(y.intValue()+1);
// y wskazuje na nowy obiekt; w praktyce: zmiana y jest lokalna, a main jej nie widzi
System.out.println("test2 y = " + y);
}
static void test3(AtomicInteger z) {
System.out.println("test3 z = " + z);
z.incrementAndGet();
System.out.println("test3 z = " + z);
}
static void test4(String s) {
System.out.println(s);
s += " ma kota";
System.out.println(s);
}
static void test5(StringBuilder s) {
System.out.println(s);
s.append(" ma psa");
System.out.println(s);
}
public static void main(String[] args) {
int x = 100;
test1(x);
System.out.println("za test1: " + x);
System.out.println();
Integer y = 200;
test2(y);
System.out.println("za test2: " + y);
System.out.println();
AtomicInteger z = new AtomicInteger(300);
test3(z);
System.out.println("za test3: " + z);
System.out.println();
String napis = "Ala";
System.out.println(napis);
test4(napis);
System.out.println(napis);
System.out.println();
StringBuilder builder = new StringBuilder("Ola");
System.out.println(builder);
test5(builder);
System.out.println(builder);
}
}
// Znane klasy niemutowalne:
// String, Integer, Long, Double itd., BigInteger i BigDecimal
// LocalDate, LocalTime itd
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