简体   繁体   中英

How to write new instance of subclass

can somebody help me please properly extend my method USBtoUSART in Java? I managed to extend it, but I have problem, to create a new instance of subclass.

public class USBtoUSART extends DesktopApplication1View implements SerialPortEventListener{
   public USBtoUSART(SingleFrameApplication app){
        super(app);    
   }
}


public class DesktopApplication1View extends FrameView {
    SingleFrameApplication ap;
    USBtoUSART serial = new USBtoUSART(ap);

    public DesktopApplication1View(SingleFrameApplication app) {      
        super(app);
    }
}

I wanted USBtoUSART to be a subclass of DesktopApplication1View, but I get error message of the app will crash. I think because the USBtoUSART serial = new USBtoUSART(ap); sequence.

Thanks for any help, it's eating me alive ...

As @MadProgrammer already said: you cannot create a new object of a class that holds a subclass as a field and initializes it. This will create a StackOverflowError because of the circular dependency that you introduce.

Consider following example:

public class A {
    C c;
    B b = new B(c);

    public A(C c) {
        System.out.println("A constructor");
    }
} 

public class B extends A {
    public B(C c) {
        super(c);
    }
}

public class C {

}

public class Test {
    public static void main(String[] args) {
        new A(new C());
//      new B(new C());
    }
}

Both the creation of a new A and a new B will return a StackOverflowError because they both will construct class A that creates an instance of class B that creates an instance of class A , etc.

Edit to use your example:

public class USBtoUSART extends DesktopApplication1View implements SerialPortEventListener{
   public USBtoUSART(SingleFrameApplication app){
        super(app);    
   }
}


public class DesktopApplication1View extends FrameView {
    SingleFrameApplication ap;

    public DesktopApplication1View(SingleFrameApplication app) {      
        super(app);
    }
}

public static void Main(string[] args){
 USBtoUSART serial = new USBtoUSART(new SingleFrameApplication());
}

This will happen:

  1. Define serial as an object of USBtoUSART
  2. Instantiate a new USBtoUSART object. This will perform the following actions:
    1. Enter the constructor of USBtoUSART
    2. Enter the constructor of its superclass, DesktopApplication1View
    3. Enter the constructor of its superclass, FrameView

Meanwhile, the parameter SingleFrameApplication will be passed to every constructor.

It sounds as if your general architecture setup is wrong. Do you perhaps want to extend SingleFrameApplication ? You'll have to use a parameter in your constructor if you want USBtoUSART to be instantiated.

Alternatively, do something like this:

public class USBtoUSART extends DesktopApplication1View implements SerialPortEventListener{
       public USBtoUSART(){
            super(new SingleFrameApplication());    
       }
}

public static void Main(string[] args){
 USBtoUSART serial = new USBtoUSART();
}

But I don't know if this is applicable in your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM