Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

java - How can i use/inherit the same context as the parent class i am extending?

The code uses a library to create IO for a java driven PLC. In the Io interface class i create IO for the plc. All properties etc is stored in a bComponent. You can see this as something like context normally is.

I need this bComponent/context to be the same in the mainClass so i can't use static methods/class. How can i do this without having to create an instance. i just want to share the bcomponent between the classes. i.e. inherit the parents bComponent/context.

any help would be really appreciated !

public class IoInterface extends BComponent implements Runnable {
public final BComponent getComponent() {return this;}

/**Create an Action*/
public static final Action TestAction_1 = newAction(Flags.HIDDEN, null);
public void TestAction_1() {invoke(TestAction_1, null, null);}

/**Create in IO*/
public static final Property bBool_1 = newProperty(Flags.EXECUTE_ON_CHANGE | Flags.OPERATOR Flags.SUMMARY, ((BBoolean) ((BValue) BBoolean.TYPE.getInstance())).getBoolean(), BFacets.tryMake(null));
public boolean getBBool_1() {return getBoolean(bBool_1);}
public void setBBool_1(boolean v) {setBoolean(bBool_1, v, null);}

public void run() {}

//This method is called when triggering "TestAction_1"
public void doTestAction_1(){}
}

In this class i want to interact with the IO and action created in the IoInterface class*/

public class mainClass extends IoInterface {

    //bcomponent needs to bee the same as IoInterface
public mainClass(BComponent b) {
    super(b);
}

public void main(){
    triggerAction("TestAction");
}

//for this to work properly getComponent needs to return the same bComponent as in IoInterface
public void triggerAction(String nameOfAction) {
        this.getComponent().invoke(this.getComponent().getAction(nameOfAction), null);
        }
    }
question from:https://stackoverflow.com/questions/65951814/how-can-i-use-inherit-the-same-context-as-the-parent-class-i-am-extending

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...