Calling a base class constructor in derived class after some code block in derived constructor
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class bar { public bar(list<int> id, String x, int size, byte[] bytes) { ... } } public class Foo: Bar { public Foo(list<int> id, String x, someEnumType y): base(id, x, sizeof(someEnumType), y) { //some functionality } } |
正如您在上面的代码中看到的,在调用基类构造函数之前,我需要将somenumtype转换为byte数组类型。有办法吗?类似:
1 2 3 4 5 6 7 8 9 | public class Foo: Bar { public Foo(list<int> id, String x, someEnumType y) { //someEnumType to byte array base(...) } } |
只需在派生类中创建一个方法并调用它….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class bar { public bar(list<int> id, String x, int size, byte[] bytes) { ... } } public class Foo: Bar { public Foo(list<int> id, String x, someEnumType y): base(id, x, sizeof(someEnumType), Convert(y)) { //some functionality } public static byte[] Convert(SomeEnumType type) { // do convert } } |