Java:使用多态性来避免 if 语句? [英] Java: using polymorphism to avoid if-statements?

查看:38
本文介绍了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 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆