/* * A Konstruktor2 osztaly modositasa, ami fajlbol vagy billentyuzetrol veszi a konstruktor parameteret. * */ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; class NagySzulo { static int i; NagySzulo() { System.out.println("NagySzulo().i=" + i); i = 3; System.out.println("NagySzulo().i=" + i); } // NagySzulo() NagySzulo(int l) { i = l; System.out.println("NagySzulo(" + l + ").i=" + i); } // NagySzulo(int) } // class NagySzulo class Szulo extends NagySzulo { { i = 4; } Szulo(int l) { this(l, i); System.out.println("Szulo(" + l + ").i=" + i); i = 2; System.out.println("Szulo(" + l + ").i=" + i); } // Szulo(int) Szulo(int l, int w) { // super() - ha nincs, a fordító beteszi System.out.println("Szulo(" + l + "," + w + ").i=" + i); i = 5; System.out.println("Szulo(" + l + "," + w + ").i=" + i); } // Szulo(int, int) } // class Szulo class Gyerek extends Szulo { int i; { i = 8; } Gyerek(int l) { super(l); // ha nincs Szulo(), akkor ez kotelezo System.out.println("Gyerek(" + l + ").i=" + i); System.out.println("super.i=" + super.i); } // Gyerek(int) } // class Gyerek public class Konstruktor2Modositott { public static void main(String args[]) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int i = 7; System.out.println("Billenytyuzetrol, vagy fajlbol olvassunk be?"); while (true){ String parancs = null; try{ parancs = br.readLine(); }catch (IOException f){ f.printStackTrace(); } if (parancs.equalsIgnoreCase("billentyuzet")){ try{ System.out.println("Adj meg egy számot!"); i = Integer.parseInt(br.readLine()); System.out.println("Köszönöm! A megadott szám a(z) " + i + " volt."); // }catch (Exception e){ // System.err.println("Ha nem lenne kikommentelve fordítási hibát kapnánk"); // mivel az exception az összes többi beépített exception őse, bármilyen exception bekövetkezte esetén // az első ág lefutna, a több catch értelmét vesztené ebben a sorrenben, és ezt a fordító nagyon okosan hibával honorálja }catch (NumberFormatException e){ System.err.println("A begepelt string nem ertelmezheto egesz szamkent!"); e.printStackTrace(); }catch (IOException e){ System.err.println("Problema a billentyuzetrol valo beolvasas soran!"); e.printStackTrace(); }finally{ // abban az esetben fut le, ha nem keletkezett egy kivetel sem System.out.println("Sikeres volt a try blokk!"); break; } }else if (parancs.equalsIgnoreCase("fajl")){ try{ br = new BufferedReader(new FileReader("be.txt")); i = Integer.parseInt(br.readLine()); }catch (NumberFormatException e){ System.err.println("A be.txt elso soraban talalhato string nem ertelmezheto egesz szamkent!"); e.printStackTrace(); }catch (FileNotFoundException e){ System.err.println("Nem talalom a be.txt fajlt!"); e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } break; }else{ System.err.println("Ilyen utasitast nem ismerek!\nbillenytuzet/fajl utasitasok leteznek"); } } Gyerek g1 = new Gyerek(i); System.out.println("g1.i=" + g1.i); } // main() } // Konstruktor3