Skip to content

Commit bc1d789

Browse files
author
Andrew Longosz
committed
Klasy i ich dziedziczenie
0 parents  commit bc1d789

17 files changed

+118
-0
lines changed

Animal.class

662 Bytes
Binary file not shown.

Animal.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public abstract class Animal extends Organism {
2+
private final int legs;
3+
4+
public Animal(int legs) {
5+
super();
6+
this.legs = legs;
7+
System.out.println("Creating animal...");
8+
}
9+
10+
public void eat(Plant plant) {
11+
System.out.printf("%s is eating %s\n", this, plant);
12+
}
13+
14+
public int getLegs() {
15+
return legs;
16+
}
17+
}

Grass.class

343 Bytes
Binary file not shown.

Grass.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Grass extends Plant {
2+
public Grass() {
3+
super();
4+
System.out.println("Creating grass...");
5+
}
6+
}

Hay.class

337 Bytes
Binary file not shown.

Hay.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Hay extends Grass {
2+
public Hay() {
3+
super();
4+
System.out.println("Creating hay...");
5+
}
6+
}

Horse.class

354 Bytes
Binary file not shown.

Horse.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Horse extends Animal {
2+
public Horse() {
3+
super(4);
4+
System.out.println("Creating a horse...");
5+
}
6+
}

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
World: *.java
2+
javac *.java
3+
4+
clean:
5+
rm -f *.class
6+
7+
IDE:
8+
vim World.java -c 'sp Animal.java' -c 'vsp Spider.java'

Organism.class

1.36 KB
Binary file not shown.

Organism.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Map;
2+
import java.util.HashMap;
3+
4+
public abstract class Organism {
5+
private static int counter = 0;
6+
private static Map<String, Integer> specificCounter = new HashMap<String, Integer>();
7+
8+
public Organism() {
9+
System.out.println("Creating an organism...");
10+
++counter;
11+
String specificName = toString();
12+
if (!specificCounter.containsKey(specificName)) {
13+
specificCounter.put(specificName, 0);
14+
}
15+
specificCounter.put(specificName, specificCounter.get(specificName) + 1);
16+
}
17+
18+
public static int getCounter() {
19+
return counter;
20+
}
21+
22+
public static int getSpecificCounter(Organism o) {
23+
return specificCounter.get(o.toString());
24+
}
25+
26+
public String toString() {
27+
return getClass().getName();
28+
}
29+
}

Plant.class

348 Bytes
Binary file not shown.

Plant.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public abstract class Plant extends Organism {
2+
public Plant() {
3+
super();
4+
System.out.println("Creating a plant...");
5+
}
6+
}

Spider.class

358 Bytes
Binary file not shown.

Spider.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Spider extends Animal {
2+
public Spider() {
3+
super(8);
4+
System.out.println("Creating a spider...");
5+
}
6+
}

World.class

1.24 KB
Binary file not shown.

World.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class World {
2+
public static void main(String[] args) {
3+
Spider spider = new Spider();
4+
Grass grass = new Grass();
5+
Hay hay = new Hay();
6+
final int herdCount = 10;
7+
Horse[] herd = new Horse[herdCount];
8+
9+
spider.eat(grass);
10+
11+
for (int i = 0; i < herdCount; ++i) {
12+
herd[i] = new Horse();
13+
}
14+
15+
Horse myHorse = herd[4];
16+
17+
for (Horse horse : herd) {
18+
horse.eat(hay);
19+
}
20+
21+
22+
int c = Organism.getCounter();
23+
if (c == 1) {
24+
System.out.printf("There is 1 organism\n");
25+
} else {
26+
System.out.printf("There are %d ogranisms\n", c);
27+
}
28+
29+
int hc = Horse.getSpecificCounter(myHorse);
30+
System.out.printf("There are %d horses\n", hc);
31+
System.out.printf("A %s has %d legs\n", spider, spider.getLegs());
32+
System.out.printf("A %s has %d legs\n", myHorse, myHorse.getLegs());
33+
}
34+
}

0 commit comments

Comments
 (0)