Hirdetés
Új hozzászólás Aktív témák
-
Davs
tag
PetCreator:
//: typeinfo/pets/PetCreator.java
// Creates random sequences of Pets.
package typeinfo.pets;
406
Thinking in Java
Bruce Eckel
import java.util.*;
public abstract class PetCreator {
private Random rand = new Random(47);
// The List of the different types of Pet to create:
public abstract List<Class<? extends Pet>> types();
public Pet randomPet() { // Create one random Pet
int n = rand.nextInt(types().size());
try {
return types().get(n).newInstance();
} catch(InstantiationException e) {
throw new RuntimeException(e);
} catch(IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public Pet[] createArray(int size) {
Pet[] result = new Pet[size];
for(int i = 0; i < size; i++)
result[i] = randomPet();
return result;
}
public ArrayList<Pet> arrayList(int size) {
ArrayList<Pet> result = new ArrayList<Pet>();
Collections.addAll(result, createArray(size));
return result;
}
} ///:~ForNameCreator:
//: typeinfo/pets/ForNameCreator.java
package typeinfo.pets;
import java.util.*;
public class ForNameCreator extends PetCreator {
private static List<Class<? extends Pet>> types =
new ArrayList<Class<? extends Pet>>();
// Types that you want to be randomly created:
private static String[] typeNames = {
"typeinfo.pets.Mutt",
"typeinfo.pets.Pug",
"typeinfo.pets.EgyptianMau",
"typeinfo.pets.Manx",
"typeinfo.pets.Cymric",
Type Information
407
"typeinfo.pets.Rat",
"typeinfo.pets.Mouse",
"typeinfo.pets.Hamster"
};
@SuppressWarnings("unchecked")
private static void loader() {
try {
for(String name : typeNames)
types.add(
(Class<? extends Pet>)Class.forName(name));
} catch(ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
static { loader(); }
public List<Class<? extends Pet>> types() {return types;}
} ///:~
Új hozzászólás Aktív témák
A topicot kiemeltem. Valaki nem akar egy nyitó hsz-t írni?:))
- BESZÁMÍTÁS! Apple Macbook Pro16 M4 Pro 24GB RAM 512GB SSD notebook garanciával hibátlan működéssel
- BESZÁMÍTÁS! Apple Macbook Air 15 2025 M4 24GB RAM 512GB SSD notebook garanciával hibátlan működéssel
- BESZÁMÍTÁS! Apple iPad Pro 11 M4 2024 256GB Wifi tablet extrákkal garanciával hibátlan működéssel
- BESZÁMÍTÁS! Gigabyte M32U Arm Edition 31,5 144Hz IPS 1ms monitor garanciával hibátlan működéssel
- BESZÁMÍTÁS! Lian Li Galahad II 360ARGB white bontattlan vízhűtés garanciával hibátlan működéssel
- ÁRGARANCIA!Épített KomPhone i5 10400F 16/32/64GB RAM RTX 3050 6GB GAMER PC termékbeszámítással
- Keresünk iPhone 15/15 Plus/15 Pro/15 Pro Max
- Dell Precision 3571 i7-12700H 32GB 1000GB FHD RTX T600 4GB 1 év teljeskörű garancia
- GAMER PC! Intel Ultra 245 / RTX 5070 / H810M-X WIFI / 32GB DDR5 / SSD 512GB / 700w! BeszámítOK!
- 238 - Lenovo Legion Pro 7 (16IRX8) - Intel Core i9-13900HX, RTX 4070
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest

