public static int ChangeVoltage(HomeVoltage homeVoltage, UsbLine usbLine) { if (homeVoltage == null || usbLine == null) { throw new ArgumentNullException("输入参数为空"); } if (homeVoltage.Voltage >= 100 && homeVoltage.Voltage <= 220) { return(usbLine.Voltage); } return(-1); }
static void Main(string[] args) { var homeVoltage = new HomeVoltage(220); var usbLine = new UsbLine(); var result = PhoneAdapter.ChangeVoltage(homeVoltage, usbLine); Console.WriteLine($"手机电源适配器可正常冲电: {result== usbLine.Voltage}"); homeVoltage = new HomeVoltage(80); result = PhoneAdapter.ChangeVoltage(homeVoltage, usbLine); Console.WriteLine($"手机电源适配器可正常冲电: {result == usbLine.Voltage}"); Console.ReadKey(); }