Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - How to call methods from other class without instantiating the class that contains the methods

i am using a third party library to create software for a java controlled PLC.

the 3th party library requires you to define all needed Inputs and outputs etc. Then you click generate and it will generate a massive load of getters/setters and properties that use the library.

the problem is that everything is generated in one single java class. and it looks very messy.

for example (it looks something like this, tried to keep it very simple)

TestClass.Java ->

public class TestClass extends BComponent implements Runnable {

/**AUTO GENERATED IO's*/
public static final Property bBool_1 = newProperty(//settings)
public boolean Get_bool_1(){//return value }    
public boolean Set_bool_1(){//set value}

public static final Property bBool_2 = newProperty(//settings)
public boolean Get_bool_2(){//return value }    
public boolean Set_bool_2(){//set value}

/**END OF AUTO GENERATED CODE*/

/**The above will repeats a lot depending on how many in/outputs you defined*/

public void run(){
//below all these IO's the code is written (which looks like a mess)
    }
}

What i would like to do is to split up all IO (getters and setters etc) and put them in a different class. I tried to do it with a Interface but i got a bit lost as i am still new to java.

example:

IoInterface.java -> (where all auto generated IO needs to go)

public class IoInterface extends BComponent implements Runnable {

/**AUTO GENERATED IO's*/
public static final Property bBool_1 = newProperty(//settings)
public boolean Get_bool_1(){//return value }    
public boolean Set_bool_1(){//set value}

public static final Property bBool_2 = newProperty(//settings)
public boolean Get_bool_2(){//return value }    
public boolean Set_bool_2(){//set value}

/**END OF AUTO GENERATED CODE*/
}

TestClass.Java -> (where all logic will be implemented)

public class TestClass extends BComponent implements Runnable{

public void run(){

if (getBool_xx){ 
//doSomething
        }
    }
}

i cant explain it better then that the code has to think its on the came class so i dont have to make an instance and call each individual like this : "instanceOfIoInterface.getBool_xx"

In the end i want to acces both classes IoInterface -> testclass and testClass->IoInterface

any help would be appreciated!

question from:https://stackoverflow.com/questions/65938609/how-to-call-methods-from-other-class-without-instantiating-the-class-that-contai

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...