I have three classes here:
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);
}
}
And when I create some instances like below:
the sameType
method is down below:
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));
it will print false
and then nothing else, so it means the a.ide
and b.ide
here are null
; and they are supposed to be String bool
;
can anyone help me with it?
答案 0 :(得分:0)
I think that's because both ParentClass and Classone have an attribute called 'ide' and in the sameType method you are accessing to the parent's ide attribute which is not initialized.