c++/Java mehrere klassen in eine datei

Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.

c++/Java mehrere klassen in eine datei
hey,
ist es möglich - so wie in java mehrere Klassen in eine Datei zu schreiben?

ich würde gerne meine mainfunction haben - und dann danach mir eine Objektklasse in der gleichen Datei definieren, mit der ich in der mainfunction arbeite.


jo, in dem fall muss die Objektklasse halt “vor” der main methode - zumindest deklariert werden


Im Gegensatz zu Java ist es sogar ohne Krampf möglich :wink:


In Java ist sowas möglich?


lol :rolleyes:


Ich glaub mit irgendwelchen Verrenkungen mit inner classes oder so… Also nicht so richtig. Aber mal nen Jaffa-Experten fragen. Absurd, cree!


Naja soweit ich weiss, will der javac nur eine Klasse mit dem selben Namen wie dem Dateinamen finden, und ist zufrieden…
Wenn du im Inneren wie arw schon bemerkt hat noch ne class baust, is das legitim(nicht schoen aber legitim) afaik.

also sowas

public static class foo{
    public class bar{
        
    }
    public static void main(String[] args){
    
    }
}

[quote=mediadon]public static class foo[/quote]Aua! Java-Klassennamen schreibt man groß!

Es geht btw auch ohne innere Klassen: Java erwartet nur, dass Klassen, die public sind in einer Datei liegen, die so heißt, wie der Klassenname. Neben der public-Klasse in dieser Datei kann man noch beliebig viele Klassen mit anderen Sichbarkeitsmodifizierern anlegen.


static gibts aber bei Klassen nur, falls es innere sind, wenn ich mich nicht irre.


Korrekt.

Btw. anonyme Klassen kann man nicht static machen.


In Java dachte ich bis jetzt immer es funktioniert so… tut’s aber irgendwie auch nicht… hiiiilfeeeeee

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class zk2 {
	
	public static void main(String args[]) throws NumberFormatException,
			/*
...
*/
		for (int i = 0; i < sets; i++) {
			parse(all.get(i));
		}
	}

	static void parse(ArrayList<String> set) {
		Alphabet a = null;
		/*
...
*/
			a = match();
			/*
...
*/		

	}

	static Alphabet match() {
		if (!(workaround.charAt(3) == ' '
				&& workaround.charAt(9) == workaround.charAt(3)
				&& workaround.charAt(9) == workaround.charAt(15)
				&& workaround.charAt(9) == workaround.charAt(19)
				&& workaround.charAt(9) == workaround.charAt(25)
				&& workaround.charAt(9) == workaround.charAt(30)
				&& workaround.charAt(9) == workaround.charAt(34) && workaround
				.charAt(9) == workaround.charAt(39))) {
			return null;
		}
		
		Alphabet a = new Alphabet(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); // Hier ist das Problem! Egal ob mit default-Construtctor oder so umständlich wie es jetzt ist. Der Debugger sagt mir als Fehler: java.lang.ClassNotFoundException 
/*
...
*/
		return a;

	}

	
}
// Hier ist die neue Klasse in der gleichen Datei, welch mir als Objekt dienen soll...
class Alphabet {
	public Alphabet(char q, char i, char c, char k, char b, char w, char n,
			char f, char x, char j, char u, char m, char p, char s, char v,
			char r, char t, char h, char e, char l, char a, char z, char y,
			char d, char o, char g) {
		this.q = q;
		this.i = i;
		this.c = c;
		this.k = k;
		this.b = b;
		this.w = w;
		this.n = n;
		this.f = f;
		this.x = x;
		this.j = j;
		this.u = u;
		this.m = m;
		this.p = p;
		this.s = s;
		this.v = v;
		this.r = r;
		this.t = t;
		this.h = h;
		this.e = e;
		this.l = l;
		this.a = a;
		this.z = z;
		this.y = y;
		this.d = d;
		this.o = o;
		this.g = g;
	}

	char q = ' ';
	char i = ' ';
	char c = ' ';
	char k = ' ';
	char b = ' ';
	char w = ' ';
	char n = ' ';
	char f = ' ';
	char x = ' ';
	char j = ' ';
	char u = ' ';
	char m = ' ';
	char p = ' ';
	char s = ' ';
	char v = ' ';
	char r = ' ';
	char t = ' ';
	char h = ' ';
	char e = ' ';
	char l = ' ';
	char a = ' ';
	char z = ' ';
	char y = ' ';
	char d = ' ';
	char o = ' ';
	char g = ' ';
}

Sollte eigentlich tun… Zumindest die Klassen sind Korrekt angelegt.


Jo sollte prinzipiell funktionieren


Wie schaut die Umgebungsvariable für den CLASSPATH auf deinem PC aus? Vermutlich fehlt da ein “.”


Meinst du das hier, bzw den Punkt am Anfang der Zeile?

.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip


häng mal den ganzen source an


Hm, das sollte eigentlich passen.


der ist sau hässlich, weiß ich selber :wink: wollt ich nachm debuggen noch optimieren :stuck_out_tongue:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class zk2 {
	/*
	 * static final String ref = "the quick brown fox jumps over the lazy dog";
	 * static final int reflen = ref.length(); static final int spaces[] = { 3,
	 * 9, 15, 19, 25, 30, 34, 39, 100 }; static final int spacelen =
	 * spaces.length;
	 */
	static String workaround;

	public static void main(String args[]) throws NumberFormatException,
			IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<ArrayList> all = new ArrayList<ArrayList>();
		int sets = Integer.parseInt(br.readLine());
		br.readLine(); // leere Zeile verwerfen
		for (int i = 0; i < sets; i++) { // Eingaben gleich zu seperaten Sets
											// zusammenfassen
			ArrayList<String> set = new ArrayList<String>();
			while (true) {
				String str = br.readLine();
				if (str.length() < 1) { // Ende eines Sets
					all.add(set);
					break;
				} else {
					set.add(str);
				}
			}
		}
		for (int i = 0; i < sets; i++) {
			parse(all.get(i));
		}
	}

	static void parse(ArrayList<String> set) {
		Alphabet a = null;
		for (int i = 0; i < set.size(); i++) {
			workaround = set.get(i);
			if (workaround.length() != 43) {
				continue;
			}
			a = match();
			if (a != null) {
				System.out.println("Zugewiesenes Zeug:");

				System.out.println(a.t + a.h + a.e + ' ' + a.q + a.u + a.i
						+ a.c + a.k + ' ' + a.f + a.o + a.x + ' ' + a.j + a.u
						+ a.m + a.p + a.s + ' ' + a.o + a.v + a.e + a.r + ' '
						+ a.t + a.h + a.e + ' ' + a.l + a.a + a.z + a.y + ' '
						+ a.f + a.o + a.x);
				break;
			}
		}

		for (int i = 0; i < set.size(); i++) {
			String str = set.get(i);
			for (int j = 0; j < str.length(); j++) {
				if (str.charAt(i) == ' ') {
					System.out.print(' ');
					continue;
				}
				if (str.charAt(i) == 'a') {
					System.out.print(a.a);
					continue;
				}
				if (str.charAt(i) == 'b') {
					System.out.print(a.b);
					continue;
				}
				if (str.charAt(i) == 'c') {
					System.out.print(a.c);
					continue;
				}
				if (str.charAt(i) == 'd') {
					System.out.print(a.d);
					continue;
				}
				if (str.charAt(i) == 'e') {
					System.out.print(a.e);
					continue;
				}
				if (str.charAt(i) == 'f') {
					System.out.print(a.f);
					continue;
				}
				if (str.charAt(i) == 'g') {
					System.out.print(a.g);
					continue;
				}
				if (str.charAt(i) == 'h') {
					System.out.print(a.h);
					continue;
				}
				if (str.charAt(i) == 'i') {
					System.out.print(a.i);
					continue;
				}
				if (str.charAt(i) == 'j') {
					System.out.print(a.j);
					continue;
				}
				if (str.charAt(i) == 'k') {
					System.out.print(a.k);
					continue;
				}
				if (str.charAt(i) == 'l') {
					System.out.print(a.l);
					continue;
				}
				if (str.charAt(i) == 'm') {
					System.out.print(a.m);
					continue;
				}
				if (str.charAt(i) == 'n') {
					System.out.print(a.n);
					continue;
				}
				if (str.charAt(i) == 'o') {
					System.out.print(a.o);
					continue;
				}
				if (str.charAt(i) == 'p') {
					System.out.print(a.p);
					continue;
				}
				if (str.charAt(i) == 'q') {
					System.out.print(a.q);
					continue;
				}
				if (str.charAt(i) == 'r') {
					System.out.print(a.r);
					continue;
				}
				if (str.charAt(i) == 's') {
					System.out.print(a.s);
					continue;
				}
				if (str.charAt(i) == 't') {
					System.out.print(a.t);
					continue;
				}
				if (str.charAt(i) == 'u') {
					System.out.print(a.u);
					continue;
				}
				if (str.charAt(i) == 'v') {
					System.out.print(a.v);
					continue;
				}
				if (str.charAt(i) == 'w') {
					System.out.print(a.w);
					continue;
				}
				if (str.charAt(i) == 'x') {
					System.out.print(a.x);
					continue;
				}
				if (str.charAt(i) == 'y') {
					System.out.print(a.y);
					continue;
				}
				if (str.charAt(i) == 'z') {
					System.out.print(a.z);
					continue;
				}
				System.out.println();
			}
		}
	}

	static Alphabet match() {
		if (!(workaround.charAt(3) == ' '
				&& workaround.charAt(9) == workaround.charAt(3)
				&& workaround.charAt(9) == workaround.charAt(15)
				&& workaround.charAt(9) == workaround.charAt(19)
				&& workaround.charAt(9) == workaround.charAt(25)
				&& workaround.charAt(9) == workaround.charAt(30)
				&& workaround.charAt(9) == workaround.charAt(34) && workaround
				.charAt(9) == workaround.charAt(39))) {
			return null;
		}
		
		Alphabet a = new Alphabet(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
		if (!(ungleich('a', 36))) {
			return null;
		}
		a.a = workaround.charAt(36);
		if (!(ungleich('b', 10))) {
			return null;
		}

		if (!(ungleich('c', 7))) {
			return null;
		}
		a.c = workaround.charAt(7);

		if (!(ungleich('d', 40))) {
			return null;
		}
		a.d = workaround.charAt(40);

		if (!(ungleich('e', 2, 28, 33))) {
			return null;
		}
		a.e = workaround.charAt(2);

		if (!(ungleich('f', 16))) {
			return null;
		}
		a.f = workaround.charAt(16);

		if (!(ungleich('g', 42))) {
			return null;
		}
		a.g = workaround.charAt(42);

		if (!(ungleich('h', 1, 32))) {
			return null;
		}
		a.h = workaround.charAt(1);

		if (!(ungleich('i', 6))) {
			return null;
		}
		a.i = workaround.charAt(6);

		if (!(ungleich('j', 20))) {
			return null;
		}
		a.j = workaround.charAt(20);

		if (!(ungleich('k', 8))) {
			return null;
		}
		a.k = workaround.charAt(8);

		if (!(ungleich('l', 35))) {
			return null;
		}
		a.l = workaround.charAt(35);

		if (!(ungleich('m', 22))) {
			return null;
		}
		a.m = workaround.charAt(22);

		if (!(ungleich('n', 14))) {
			return null;
		}
		a.n = workaround.charAt(14);

		if (!(ungleich('o', 12, 17, 26, 41))) {
			return null;
		}
		a.o = workaround.charAt(12);

		if (!(ungleich('p', 23))) {
			return null;
		}
		a.p = workaround.charAt(23);

		if (!(ungleich('q', 4))) {
			return null;
		}
		a.q = workaround.charAt(4);

		if (!(ungleich('r', 11, 29))) {
			return null;
		}
		a.r = workaround.charAt(11);

		if (!(ungleich('s', 24))) {
			return null;
		}
		a.s = workaround.charAt(24);
		System.out.println("t zuweisen");
		if (!(ungleich('t', 0, 31))) {
			return null;
		}
		System.out.println("erledigt mit: " + a.t);
		a.t = workaround.charAt(31);

		if (!(ungleich('u', 5, 21))) {
			return null;
		}
		a.u = workaround.charAt(5);

		if (!(ungleich('v', 27))) {
			return null;
		}
		System.out.println("a.v soll werden: " + workaround.charAt(27));

		a.v = workaround.charAt(27);

		if (!(ungleich('w', 13))) {
			return null;
		}
		a.w = workaround.charAt(13);

		if (!(ungleich('x', 18))) {
			return null;
		}
		a.x = workaround.charAt(18);

		if (!(ungleich('y', 38))) {
			return null;
		}
		a.y = workaround.charAt(38);

		if (!(ungleich('z', 37))) {
			return null;
		}
		a.z = workaround.charAt(37);

		return a;

	}

	static boolean ungleich(char x, int pos) {
		for (int y = pos + 1; y < 43; y++) {
			if (workaround.charAt(y) != x) {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}

	static boolean ungleich(char x, int pos, int pos2) {
		for (int y = pos + 1; y < 43; y++) {
			if (workaround.charAt(y) != x) {
				continue;
			}
			if (y == pos2) {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}

	static boolean ungleich(char x, int pos, int pos2, int pos3) {
		for (int y = pos + 1; y < 43; y++) {
			if (workaround.charAt(y) != x) {
				continue;
			}
			if (y == pos2 || y == pos3) {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}

	static boolean ungleich(char x, int pos, int pos2, int pos3, int pos4) {

		for (int y = pos + 1; y < 43; y++) {
			if (workaround.charAt(y) != x) {
				continue;
			}
			if (y == pos2 || y == pos3 || y == pos4) {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}
}

class Alphabet {
	public Alphabet(char q, char i, char c, char k, char b, char w, char n,
			char f, char x, char j, char u, char m, char p, char s, char v,
			char r, char t, char h, char e, char l, char a, char z, char y,
			char d, char o, char g) {
		this.q = q;
		this.i = i;
		this.c = c;
		this.k = k;
		this.b = b;
		this.w = w;
		this.n = n;
		this.f = f;
		this.x = x;
		this.j = j;
		this.u = u;
		this.m = m;
		this.p = p;
		this.s = s;
		this.v = v;
		this.r = r;
		this.t = t;
		this.h = h;
		this.e = e;
		this.l = l;
		this.a = a;
		this.z = z;
		this.y = y;
		this.d = d;
		this.o = o;
		this.g = g;
	}

	char q = ' ';
	char i = ' ';
	char c = ' ';
	char k = ' ';
	char b = ' ';
	char w = ' ';
	char n = ' ';
	char f = ' ';
	char x = ' ';
	char j = ' ';
	char u = ' ';
	char m = ' ';
	char p = ' ';
	char s = ' ';
	char v = ' ';
	char r = ' ';
	char t = ' ';
	char h = ' ';
	char e = ' ';
	char l = ' ';
	char a = ' ';
	char z = ' ';
	char y = ' ';
	char d = ' ';
	char o = ' ';
	char g = ' ';
}
/*
	private boolean a_lock = false;
	private boolean b_lock = false;
	private boolean c_lock = false;
	private boolean d_lock = false;
	private boolean e_lock = false;
	private boolean f_lock = false;
	private boolean g_lock = false;
	private boolean h_lock = false;
	private boolean i_lock = false;
	private boolean j_lock = false;
	private boolean k_lock = false;
	private boolean l_lock = false;
	private boolean m_lock = false;
	private boolean n_lock = false;
	private boolean o_lock = false;
	private boolean p_lock = false;
	private boolean q_lock = false;
	private boolean r_lock = false;
	private boolean s_lock = false;
	private boolean t_lock = false;
	private boolean u_lock = false;
	private boolean v_lock = false;
	private boolean w_lock = false;
	private boolean x_lock = false;
	private boolean y_lock = false;
	private boolean z_lock = false;

	public boolean setA(char a) {
		if (a_lock == false) {
			this.a = a;
			a_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setB(char b) {
		if (b_lock == false) {
			this.b = b;
			b_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setC(char c) {
		if (c_lock == false) {
			this.c = c;
			c_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setD(char d) {
		if (d_lock == false) {
			this.d = d;
			d_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setE(char e) {
		if (e_lock == false) {
			this.e = e;
			e_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setF(char f) {
		if (f_lock == false) {
			this.f = f;
			f_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setG(char g) {
		if (g_lock == false) {
			this.g = g;
			g_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setH(char h) {
		if (h_lock == false) {
			this.h = h;
			h_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setI(char i) {
		if (i_lock == false) {
			this.i = i;
			i_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setJ(char j) {
		if (j_lock == false) {
			this.j = j;
			j_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setK(char k) {
		if (k_lock == false) {
			this.k = k;
			k_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setL(char l) {
		if (l_lock == false) {
			this.l = l;
			l_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setM(char m) {
		if (m_lock == false) {
			this.m = m;
			m_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setN(char n) {
		if (n_lock == false) {
			this.n = n;
			n_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setO(char o) {
		if (o_lock == false) {
			this.o = o;
			o_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setP(char p) {
		if (p_lock == false) {
			this.p = p;
			p_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setQ(char q) {
		if (q_lock == false) {
			this.q = q;
			q_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setR(char r) {
		if (r_lock == false) {
			this.r = r;
			r_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setS(char s) {
		if (s_lock == false) {
			this.s = s;
			s_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setT(char t) {
		if (t_lock == false) {
			this.t = t;
			t_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setU(char u) {
		if (u_lock == false) {
			this.u = u;
			u_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setV(char v) {
		if (v_lock == false) {
			this.v = v;
			v_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setW(char w) {
		if (w_lock == false) {
			this.w = w;
			w_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setX(char x) {
		if (x_lock == false) {
			this.x = x;
			x_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setY(char y) {
		if (y_lock == false) {
			this.y = y;
			y_lock = true;
			return true;
		} else {
			return false;
		}
	}

	public boolean setZ(char z) {
		if (z_lock == false) {
			this.z = z;
			z_lock = true;
			return true;
		} else {
			return false;
		}
	}

	void free() {
		a_lock = false;
		b_lock = false;
		c_lock = false;
		d_lock = false;
		e_lock = false;
		e_lock = false;
		f_lock = false;
		f_lock = false;
		g_lock = false;
		h_lock = false;
		i_lock = false;
		j_lock = false;
		k_lock = false;
		l_lock = false;
		m_lock = false;
		n_lock = false;
		o_lock = false;
		p_lock = false;
		q_lock = false;
		r_lock = false;
		s_lock = false;
		t_lock = false;
		u_lock = false;
		v_lock = false;
		w_lock = false;
		x_lock = false;
		y_lock = false;
		z_lock = false;
	}
*/

Du solltest dich mal mit Reflection befassen :smiley: