Dziedziczenie - błąd

0

Witam , robiłem sobie własnie takie zadanko i wyskakuje mi błąd którego w ogole nie rozumiem. Proszę o pomoc

public class Zad_1 {

	public class Pies {
		int identyfikator = 0, wiek = 0;
		String imie = "", masc = "", imieWł = "", nazwWł = "";

		public Pies() {
		}

		public Pies(int identyfikator, String imie, String masc, int wiek,
				String imieWł, String nazWł) {
			this.identyfikator = identyfikator;
			this.imie = imie;
			this.masc = masc;
			this.wiek = wiek;
			this.imieWł = imieWł;
			this.nazwWł = nazWł;

		}

		public String toString() {
			String id = Integer.toString(identyfikator);
			String rok1 = "";
			String miesiac1 = "";
			int miesiac = 0;
			int rok = 0;
			while (id.length() < 8) {
				id = "0" + id;
			}
			if (wiek < 12) {
				miesiac1 = Integer.toString(wiek);
				miesiac1 += " miesiecy";
			} else {
				rok = wiek / 12;
				miesiac = wiek % 12;
			}
			if (rok == 1) {
				rok1 = Integer.toString(rok);
				rok1 += " rok i";
				miesiac1 = Integer.toString(miesiac);
				miesiac1 += " miesiecy";
			} else {
				rok1 = Integer.toString(rok);
				rok1 += " lat(a)";
				miesiac1 = Integer.toString(miesiac);
				miesiac1 += " miesiecy";
			}

			return "--------------------------------\n" + "identyfikator:\t\t"
					+ identyfikator + "\nWLASCICIEL: " + imieWł + " " + nazwWł
					+ "\n\nIMIE: " + imie + "\nmasc: " + masc + "\nwiek: "
					+ rok1 + "" + miesiac1
					+ "\n--------------------------------\n";
		}
	}

	public class RasowyPies extends Pies {
		int identyfikator = 0, wiek = 0;
		String rasa = "", imie = "", masc = "", imieWł = "", nazwWł = "",
				imieOj = "", imieMa = "";

		public RasowyPies() {
		}

		public RasowyPies(int identyfikator, String rasa, String imie,
				String masc, int wiek, String imieOj, String imieMa,
				String imieWł, String nazWł) {
			super(identyfikator, masc, imie, wiek, imieWł, nazWł);
			this.rasa = rasa;
			this.imieOj = imieOj;
			this.imieMa = imieMa;

		}

		public String toString() {
			return super.toString()
					+ "Pies rasowy\n--------------------------------\nRASA: "
					+ rasa + "\nMATKA: " + imieMa + "\nOJCIEC: " + imieOj
					+ "\nMIOT: " + "\n--------------------------------\n";
		}
	}

	public static void main(String[] args) {
	Pies sfora[]={new Pies(76589,"As","biało-czarny",18,  "Anna","Nowak"),
       new RasowyPies(45321,"Labrador","Elf","czarny",25, "Agus","Mati","Barbara","Klopotek"),
       new Pies(102467,"Kumpel","brazowy, podpalany",89,"Zennon","Kula"),
        new RasowyPies(9678,"York","Beta","brazowy",8,"Emka","Fidelus","Alicja","Kot")}; 	

for(int i=0; i < sfora.length; i++)
System.out.println(sfora[i]+"\n\n");

	}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type Zad_1 is accessible. Must qualify the allocation with an enclosing instance of type Zad_1 (e.g. x.new A() where x is an instance of Zad_1).

at Zad_1.main(Zad_1.java:84)
0

W jednym pliku możesz mieć tylko jedną klasę

public class
0

błąd nadal jest

0

To świadczy tylko i wyłącznie o tym że nie masz pojęcia co robisz. Gdybyś napisał to jak normalny człowiek, każdą klasę w osobnym pliku, to by działało. A skoro chcesz je wpychać w ten sposób to może warto poczytać jak się robi klasy zagnieżdżone? Że na przykład w tutaj musiałyby być statyczne żeby to się skompilowało? Ale nie, po co myśleć albo czytać, lepiej wrzucić pytanie na forum...

0

Poprawiłem trochę i działa

class Pies {
	int identyfikator = 0, wiek = 0;
	String imie = "", masc = "", imieWł = "", nazwWł = "";

	public Pies() {
	}

	public Pies(int identyfikator, String imie, String masc, int wiek,
			String imieWł, String nazWł) {
		this.identyfikator = identyfikator;
		this.imie = imie;
		this.masc = masc;
		this.wiek = wiek;
		this.imieWł = imieWł;
		this.nazwWł = nazWł;

	}

	public String toString() {
		String id = Integer.toString(identyfikator);
		String rok1 = "";
		String miesiac1 = "";
		int miesiac = 0;
		int rok = 0;
		while (id.length() < 8) {
			id = "0" + id;

		}

		if (wiek < 12) {
			miesiac1 = Integer.toString(wiek);
			miesiac1 += " miesiecy";

		} else {
			rok = wiek / 12;
			miesiac = wiek % 12;
		}
		if (rok == 1) {
			rok1 = Integer.toString(rok);
			rok1 += " rok i ";
			miesiac1 = Integer.toString(miesiac);
			miesiac1 += " miesiecy";
		} else if (rok > 1) {
			rok1 = Integer.toString(rok);
			rok1 += " lat(a) i ";
			miesiac1 = Integer.toString(miesiac);
			miesiac1 += " miesiecy";
		}

		return "--------------------------------\n" + "identyfikator:\t\t" + id
				+ "\nWLASCICIEL: " + imieWł + " " + nazwWł + "\n\nIMIE: "
				+ imie + "\nMASC: " + masc + "\nWIEK: " + rok1 + " " + miesiac1
				+ "\n--------------------------------\n";
	}
}

class RasowyPies extends Pies {

	private String rasa = "", imieOj = "", imieMa = "";

	public RasowyPies() {
	}

	public RasowyPies(int identyfikator, String rasa, String imie, String masc,
			int wiek, String imieOj, String imieMa, String imieWł, String nazWł) {
		super(identyfikator, masc, imie, wiek, imieWł, nazWł);
		this.rasa = rasa;
		this.imieOj = imieOj;
		this.imieMa = imieMa;

	}

	public String toString() {
		return super.toString()
				+ "\nPies rasowy\n--------------------------------\nRASA: "
				+ rasa + "\nMATKA: " + imieMa + "\nOJCIEC: " + imieOj
				+ "\n--------------------------------\n";
	}

	public static void main(String[] args) {
		Pies sfora[] = {
				new Pies(76589, "As", "biało-czarny", 18, "Anna", "Nowak"),
				new RasowyPies(45321, "Labrador", "Elf", "czarny", 25, "Agus",
						"Mati", "Barbara", "Klopotek"),
				new Pies(102467, "Kumpel", "brazowy, podpalany", 89, "Zennon",
						"Kula"),
				new RasowyPies(9678, "York", "Beta", "brazowy", 8, "Emka",
						"Fidelus", "Alicja", "Kot") };

		for (int i = 0; i < sfora.length; i++)
			System.out.println(sfora[i] + "\n\n");

	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1