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

查看:386
本文介绍了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(); }

}

我是否仍然需要转换或如果 - 主程序中的语句,以确定用户在运行时选择的布局?

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天全站免登陆