Home > other >  Go type inheritance
Go type inheritance

Time:10-14

How to achieve the following Java functions in the Go

1, define the type of inheritance
2, through the base class as a parameter, the function for processing

Seemingly can only be used in the Go struct inheritance, but it will not be able to use the parent class as a parameter to accept a subclass object
If you define an interface is not necessary, because this a few structure is the type definition, there is no way to implement

 
Public class Message {
Public string type.
}

Public class MessageA extends the Message {
Public string value.
}

Public class MessageB extends the Message {
Public string value.
}

Public class HelloWorld {
//parameter is the base class
Public string test Message (MSG) {
If (MSG) type)=='A' {
}
If (MSG) type)=='B' {
}
}

Public static void main (String [] args) {
The Message objA=new MessageA ();
ObjA. Type='A';
The test (objA);
The Message objB=new MessageB ();
ObjA. Type='B';
The test (objB);
}
}
  • Related