Java: using polymorphism to avoid if-statements?(Java:使用多态性来避免 if 语句?)
问题描述
我正在尝试编写一个基于用户选择初始化某些布局的 java 程序.我想要做的是尽量避免编写一堆 if 语句,以便在需要添加更多布局时代码可以扩展以备将来使用.我听说实现这一点的最佳方法是使用多态,但我对多态的理解仍然有点模糊.
I'm attempting to write a java program that initializes certain layouts based on what the user selects. What I want to do is try to avoid writing a bunch of if-statements so the code can be scalable for future use if more layouts need to be added. I heard the best way to implement this is using polymorphism but my understanding of polymorphism is still a little fuzzy.
说我要实现这个案例:
if (user choose layoutA) { initialize layoutA }
if (user choose layoutB) { initialize layoutB }
if (user choose layoutC) {initialize layoutC }
我正在考虑为要实现的类创建一个接口.让我困惑的是它在 main() 中是如何工作的,我是否还需要条件 if 或 switch 语句来确定要实例化哪个类?
I was thinking of creating an interface for classes to implement. What confuses me is how it works in main(), won't I still need a conditional if or switch statement to figure out which class to instantiate?
interface LayoutHandler {
public void initializeLayout();
}
class layoutA implements LayoutHandler {
public void initialize Layout {initialize layout A}
}
class layoutB implements LayoutHandler {
public void initialize Layout {initialize layout B}
}
class layoutC implements LayoutHandler {
public void initialize Layout {initialize layout C}
}
然后在 main 的某个地方:
Then somewhere out in main:
public static void main() {
getlayoutselectionfromuser()
if (user choose layoutA) { LayoutHandler layout = new layoutA(); }
if (user choose layoutB) { LayoutHandler layout = new layoutB(); }
if (user choose layoutC) { LayoutHandler layout = new layoutC(); }
}
难道我还不需要在主程序中使用 switch 或 if 语句来确定用户在运行时选择的布局吗?
Wouldn't I still require a switch or if-statement in the main program to figure out which layout the user has chosen during runtime?
谢谢!
推荐答案
一般来说,在某些时候很难避免某种条件语句来创建适当类的实例.
Generally, it will be difficult to avoid some kind of conditional statement at some point to create an instance of the appropriate class.
当您在多个地方有多个 if-else 语句时,多态的好处就来了.多态性为您封装了条件逻辑.有关此主题的其他讨论,请参阅此问题.
The benefit of polymorphism comes when you have multiple if-else statements in multiple places. The polymorphism encapsulates the conditional logic for you. See this question for some other discussions on this topic.
这种散乱的逻辑:
void initLayout() {
if (user choose layoutA) { initialize layoutA }
if (user choose layoutB) { initialize layoutB }
if (user choose layoutC) {initialize layoutC }
}
void refreshLayout() {
if (user choose layoutA) { refresh layoutA }
if (user choose layoutB) { refresh layoutB }
if (user choose layoutC) { refresh layoutC }
}
void cleanupLayout() {
if (user choose layoutA) { cleanup layoutA }
if (user choose layoutB) { cleanup layoutB }
if (user choose layoutC) { cleanup layoutC }
}
用更简单的东西代替:
layout = getLayout(user choice);
layout.initLayout();
layout.refreshLayout();
layout.cleanupLayout();
这篇关于Java:使用多态性来避免 if 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java:使用多态性来避免 if 语句?
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01