解决 从另一个类的主程序继承是否不好?

袜子1992

知名会员
已加入
2020年5月20日
留言内容
58
编程经验
Beginner
在我的一个班上,我继承了主程序。我只是想征询其他人的意见。那是不好的做法还是可以吗?
 
Last edited:

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,571
地点
弗吉尼亚州切萨皮克
编程经验
10+
欢迎来到软件工程和API /类设计的奇art。

许多年前,当人们仍在尝试找出面向对象的编程时,人们主要使用继承来避免重复代码,但是在程序编程时代,重复代码是一种已知的不良习惯。随着人们越来越多地使用面向对象的编程,人们发现仅使用继承来节省代码实际上是一种糟糕的做法,因为它会导致紧密耦合且脆弱的类层次结构。如果您修改了祖先类中的某些内容,则需要确保您没有破坏后代中的任何内容。如果您需要重用代码,现代的做法是始终首选聚合而不是继承。

一般来说,花点时间学习 SOLID面向对象的编程原理。这些将有助于您做出设计决策。
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,545
地点
悉尼,澳大利亚
编程经验
10+
In this specific case, you should absolutely not be inheriting the Program class that VS adds by default. That class should be the entry point for the application. If your application is very simple then you might put all the code in that one class. As your application gets more complex, you can move functionality out to other classes and use them in the Program class. For such an application, you might have the initialisation code in the Program class in one or more methods, then put the meat of the application functionality in other classes. Your Main method would then call the initialisation method first and then create and use the other class(es) as required. In your case, you might have an ExceptionNotification class with a Format method. Anywhere in your application, you can then call ExceptionNotification.Format. That's how it's supposed to work.

EDIT: Actually, I realise that I misinterpreted that method name. Maybe you would have a class named ExceptionNotifier and a method named NotifyFormat. You could then have other methods named NotifyX, where X 是您要通知的异常类型的名称。

That said, I hope you're not really getting an exception thrown because the user didn't enter a number. If you're working with numeric user input then you should call TryParse on the appropriate numeric type to validate and convert in one step. In that case, invalid input would not generate an exception so a type or member name that implies that it does would be inappropriate.
 
Last edited:

袜子1992

知名会员
已加入
2020年5月20日
留言内容
58
编程经验
Beginner
In this specific case, you should absolutely not be inheriting the Program class that VS adds by default. That class should be the entry point for the application. If your application is very simple then you might put all the code in that one class. As your application gets more complex, you can move functionality out to other classes and use them in the Program class. For such an application, you might have the initialisation code in the Program class in one or more methods, then put the meat of the application functionality in other classes. Your Main method would then call the initialisation method first and then create and use the other class(es) as required. In your case, you might have an ExceptionNotification class with a Format method. Anywhere in your application, you can then call ExceptionNotification.Format. That's how it's supposed to work.

EDIT: Actually, I realise that I misinterpreted that method name. Maybe you would have a class named ExceptionNotifier and a method named NotifyFormat. You could then have other methods named NotifyX, where X 是您要通知的异常类型的名称。

That said, I hope you're not really getting an exception thrown because the user didn't enter a number. If you're working with numeric user input then you should call TryParse on the appropriate numeric type to validate and convert in one step. In that case, invalid input would not generate an exception so a type or member name that implies that it does would be inappropriate.
感谢您的答复。因此,如果我试图从ExceptionNotification类中访问主程序中的方法(例如TurnNumberintoInt),则可以执行Program.TurnNumberToInt,这可以吗?

是的,我已经做到了 :) 这些通知消息与用户输入无关。 exceptionNotification类包含其他异常的所有通知消息,例如文件读取错误等。
 

袜子1992

知名会员
已加入
2020年5月20日
留言内容
58
编程经验
Beginner
欢迎来到软件工程和API /类设计的奇art。

许多年前,当人们仍在尝试找出面向对象的编程时,人们主要使用继承来避免重复代码,但是在程序编程时代,重复代码是一种已知的不良习惯。随着人们越来越多地使用面向对象的编程,人们发现仅使用继承来节省代码实际上是一种糟糕的做法,因为它会导致紧密耦合且脆弱的类层次结构。如果您修改了祖先类中的某些内容,则需要确保您没有破坏后代中的任何内容。如果您需要重用代码,现代的做法是始终首选聚合而不是继承。

一般来说,花点时间学习 SOLID面向对象的编程原理。这些将有助于您做出设计决策。
嗯,好吧!我会研究一下。感谢你的回复 :)
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,545
地点
悉尼,澳大利亚
编程经验
10+
因此,如果我试图从ExceptionNotification类中访问主程序中的方法(例如TurnNumberintoInt),则可以执行Program.TurnNumberToInt,这可以吗?
No, not OK. If that method is needed in places other than the Program class then it should be somewhere other than the Programclass that is accessible to the Program class and any other class that needs to use it.
 
最佳 底部