Modifying parameter values before sending to Base constructor

前端 未结 7 555
借酒劲吻你
借酒劲吻你 2021-01-07 16:26

The title may be a bit ambiguous, but I couldn\'t think of a better way to word this.

I realize that I can not call a derived constructor prior to calling a base con

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 17:09

    You can create a static method on derived class and put your logic there:

    public enum InputType {
        Number = 1,
        String = 2,
        Date = 3
    }
    
    public class BaseClass {
        public BaseClass(InputType t) {
            // Logic
        }
    }
    
    public class DerivedClass : BaseClass {
        public DerivedClass(int i)
            : base(GetInputType(i)) {
            // Is it possible to set "value" here?
            // Logic
        }
    
        private static InputType GetInputType(Int32 parameter) {
            // Do something with parameter
            // and return an InputType
    
            return (InputType)Enum.Parse(typeof(InputType), parameter);
        }
    }
    

提交回复
热议问题