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?:))
- Internet Rádió építése (hardver), és programozása
- TCL LCD és LED TV-k
- Samsung Galaxy Felhasználók OFF topicja
- Turbo fokozatra kapcsolt a OnePlus
- Futás, futópályák
- ubyegon2: Airfryer XL XXL forrólevegős sütő gyakorlati tanácsok, ötletek, receptek
- iPhone topik
- AliExpress tapasztalatok
- One mobilszolgáltatások
- AMD K6-III, és minden ami RETRO - Oldschool tuning
- További aktív témák...
- Dell Precision 5570 4K+ OLED Touch / i7-12800H 14C / 16-32G / 512G / RTX A2000 8G / IR FPR WS
- Dell Precision 7560 FHD / i7-11850H / 32G / 512G G4 / RTX A4000 8G / SCR WS tervező
- Alienware 16 Aurora - 16" 2560x1600 120Hz, Intel Core 7 240H, RTX 5060, beszámítás!
- Alpenföhn Ben Nevis processzorhűtő
- Eladó konfig! I9-14900K 2TB SSD 32GB DDR5 RTX5070 12GB!
- Lenovo L13 Core I3-10110U / 4GB DDR4 / LAPTOP 1
- ÚJ HP Omen 16 - 16,1" FHD 144Hz - i5 13420H - 16GB - 512GB - RTX 4050 - Win11 - 3 év garancia
- Apple iPhone 15 Pro 256GB,Újszerű,Dobozaval,12 hónap garanciával
- BESZÁMÍTÁS! ASRock H510M i5 11400F 16GB DDR4 512GB SSD ASUS ROG RX VEGA64 8GB CM Masterbox 5 700W
- AKCIÓ! DELL PowerEdge R630 rack szerver - 2xE5-2660v4 (28c/56t, 2.0/3.2GHz), 96GB RAM, 4x1G, áfás
Állásajánlatok
Cég: Laptopszaki Kft.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest


