Ich habe hier drei Klassen:Warum erhalte ich Nullwerte für meine Instanz einer Klasse, wenn sie nicht null sein sollen?
abstract class ParentClass{
String ide;
ArrayList args;
ParentClass from_type;
ParentClass to_type;
}
class Classone extends ParentClass{
String ide;
ArrayList<ParentClass> args;
public Classone(ParentClass from_type, ParentClass to_type){
this.from_type = from_type;
this.to_type = to_type;
}
public Classone(String ide, ArrayList<ParentClass> args){
this.ide = ide;
this.args = args;
}
@Override
public String toString() {
String result = "";
if (ide == null) {
return from_type.toString() + " -> " + to_type.toString();
}
else {
if (args.size() == 0) {
return ide;
}
else if (args.size() == 2) {
result = args.get(0).toString() + " " + ide + " " + args.get(1).toString();
return result;
}
else {
return "Wrong number";
}
}
}
}
class Classtwo extends Classone{
ParentClass from_type;
ParentClass to_type;
public Classtwo(ParentClass from_type, ParentClass to_type){
super(from_type, to_type);
}
}
Und wenn ich wie unten einige Beispiele zu erstellen:
die sameType
Methode ist unten:
public static boolean sameType(ParentClass t1, ParentClass t2){
if (t1 instanceof Classone && t2 instanceof Classone) {
if (t1.ide != null && t2.ide != null) {
System.out.println(t1.ide);
System.out.println(t2.ide);
return t1.ide == t2.ide;
}
else {
return false;
}
}
else {
return false;
}
}
Classone a = new Classone("bool", new ArrayList<ParentClass>());
Classone b = new Classone("bool", new ArrayList<ParentClass>());
System.out.println(sameType(a,b));
es false
und anderes dann nichts gedruckt wird , so bedeutet es die a.ide
und b.ide
hier sind null
; und sie sollen String sein bool
; kann mir jemand dabei helfen?
Es hilft nicht, dass Sie uns 'sameType' nicht gezeigt haben. Bitte geben Sie eine [MCVE] an. –
Testen Sie auf Gleichheit von 'a.ide' und' b.ide' oder auf Objektidentität? Das heißt, testen Sie mit '==' (Objektidentität) oder '.equals()'? Siehe http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java –
hi, sameType jetzt angehängt –