开发者

Java extends classes - Share the extended class fields within the super class

EDITED:

Straight to the point... I have a class

public class P_Gen{  
    protected String s;
    protected Object oP_Gen;
    protected Object oP_Gen2;
    public P_Gen(String str){
       s = str;
       oP_Gen = new MyClass(this);
       oP_Gen2 = new MyClass2(this);
    }
}  

Extended class:

public class P extends P_Gen{  
    protected Object oP;  
    public P(String str){    
       super(str);  
       oP = new MyClass(this);
    }
} 

MyCla开发者_运维问答ss:

public class MyClass{  
    protected Object oMC;
    public MyClass(P extendedObject){
       this.oMC = extendedObject.oP;
    }
}

MyClass2:

public class MyClass2{  
    protected Object oMC2;
    public MyClass(P_Gen thisObject){
       this.oMC2 = thisObject.oP;
    }
}   

The Class P_Gen shown above gives me an error for line :

oP_Gen = new MyClass(this);  

stating "cannot find symbol constructor MyClass(P_Gen)."

What i want to achieve is to make P.oP available from MyClass2.

My initial thought were that P.this === P_Gen.this. In other word, P_Gen disappear when called from super() and what is left is only P the extended class.


ok,

  1. In the class MyClass2, the constructor should be named 'MyClass2'
  2. Why dont you tray to define the constructors like this:
public MyClass(P_Gen thisObject){...}
public MyClass2(P_Gen thisObject){...}

Doing this, you are able to do something like:

new MyClass(new P());

Also you need to cast thisObject to P inside MyClass and MyClass2 contructors, so then you are able to get the oP variable. My recommendation 4you is to reed a little bit more about java inheritance.

JP


I'm a bit confused by your question (and your code, which doesn't have a doSomething() method anywhere), but you might benefit from P_Gen being an abstract class with an abstract method doSomething(). Then P can implement that method, and anything with a reference to a P_Gen can call it (and get the implementation in P, if that's what instance it is).


After a good night sleep.... It is not what I wanted to do that is wrong but my approach altogether. I went back to my beginning of OOP. Shame on me. Anyway, i gave the answer to @juanp because he said "My recommendation 4you is to reed a little bit more about java inheritance.".

For those who are interested, here is what it should look like:

The generated P_Gen class:

public abstract class P_Gen{
    protected String s;
    protected Object oP;
    public P_Gen(String str){
        this.s = str;
        init(str);
    }

    protected void init(String str){
        if(this instanceof P){
            oP = new MyClass((P)this);
        }
    }
}

The extended P class:

public class P extends P_Gen{  
    public String sSTR;
    public P(String str){
       super(str);
    }

    @Override
    protected void init(String str){
        this.sSTR = str;
        this.oP = new MyClass(this);
    }
}

The MyClass class:

public class MyClass{
    protected Object oMC;
    public MyClass(P extendedObject){
       this.oMC = extendedObject.sSTR;
    }
}

What I did was to move the MyClass instantiation from the constructor to an init function. That init function is then overridden in the extended class..... very basic stuff i know.

Even more basic, added abstract to my P_Gen class to prevent it from being instantiated directly. I could also declare an abstract init method in P_Gen.

All this makes me think that maybe .... :-) ...another day. Thanks for helping me get back on track.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜