替代写多个if语句? [英] Alternative to writing many if statements?

查看:145
本文介绍了替代写多个if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的方法:

  private void ConvertValues()
{
txtResult.Text = angle [cmbUnits1.SelectedIndex];
double value1 = Convert.ToDouble(txtValue1.Text);
double value2 = Convert.ToDouble(txtValue2.Text);
int unit1 = cmbUnits1.SelectedIndex;
}

我想要的方法是获取所选的组合框测试值。但我想知道是否有一个替代方案:

  if(angles [cmbUnits1.SelectedIndex] .Equals(Degrees )&& 
angles [cmbUnits2.SelectedIndex] .Equals(Radians)){
...
}
pre>




顺便说一句,我正在做一种单位转换器,所以我会有除角度以外的部分。所以我想要一些枚举,接口,抽象类或类,我可以实现。也许一个类名为Unit?所以我可以创建新的对象,例如 Unit degrees = new Unit(Units.Angle),单位为枚举。或者只是 Unit sqrMillimeters = new Unit(Area);

解决方案

p>当然,多态性。每当你看到长开关语句或如果/ then / else结构,它总是可以使用多态和工厂来处理它们。在你的情况下,我想象一个ITemperatureConverter界面与具体实现为华氏,Celcius,开尔文和兰金会做得很好。



更新:



如果你发现你有这个逻辑重复,为什么不能有一个更好的抽象角度比双?



如果你写的是面向对象的语言,最好是提升上面的原语(例如double和是的,字符串)封装行为到对象。



我想到一个Angle类,它会挂在值上,并返回它作为你想要的任何测量。



这里有一种在Java中做的方法。

 包角度; 

public class Angle
{
private double value;
private AngleUnit units;

public Angle()
{
this(0.0,AngleUnit.RADIANS);
}

public Angle(double value)
{
this(value,AngleUnit.RADIANS);
}

public Angle(double value,AngleUnit units)
{
this.value = value;
this.units = units;
}

public double getValue()
{
返回值;
}

public AngleUnit getUnits()
{
return units;
}

public Angle convert(AngleUnit newUnits)
{
Angle newAngle = null;

if(this.units.equals(newUnits))
{
return this;
}

return newAngle;
}
}

包角度;

public interface AngleConverter
{
角度转换(Angle angle,AngleUnit to);
}

包角度;

public enum AngleUnit
{
DEGREES,RADIANS,GRADIANS;
}

包角度;

import java.util.HashMap;
import java.util.Map;

public class DegreeConverter实现AngleConverter
{
private final Map< AngleUnit,Double>因素;

public DegreeConverter(Map< AngleUnit,Double> factors)
{
this.factors = new HashMap< AngleUnit,Double>
this.factors.put(AngleUnit.DEGREES,1.0);
this.factors.put(AngleUnit.RADIANS,Math.PI / 180.0);
this.factors.put(AngleUnit.GRADIANS,100.0 / 90。);
}

public angle convert(Angle angle,AngleUnit to)
{
assert angle!= null&& to!= null;

return new Angle(angle.getValue()* this.factors.get(to),to);
}
}


Here is my method:

private void ConvertValues()  
{  
    txtResult.Text = angles[cmbUnits1.SelectedIndex];  
    double value1 = Convert.ToDouble(txtValue1.Text);  
    double value2 = Convert.ToDouble(txtValue2.Text);  
    int unit1 = cmbUnits1.SelectedIndex;  
}

What I want the method to do is to get the selected of the ComboBoxes and test the values. But I want to know if there is an alternative to this:

if( angles[cmbUnits1.SelectedIndex].Equals("Degrees") && 
    angles[cmbUnits2.SelectedIndex].Equals("Radians")) {  
    ...  
}


By the way, I'm making a kind of unit converter so I will have sections other than angles. So I would like some enum, interface, abstract class, or class that I can implement. Maybe a class with the name Unit? So I can create new objects like Unit degrees = new Unit(Units.Angle) with Units as an enum. Or just Unit sqrMillimeters = new Unit("Area");?

解决方案

Sure, polymorphism. Anytime you see long switch statements or if/then/else constructs it's always possible to handle them using polymorphism and factories. In your case I'd imagine an ITemperatureConverter interface with concrete implementations for Fahrenheit, Celcius, Kelvin, and Rankine would do nicely.

UPDATE:

If you find you have this logic repeated, why not have a better abstraction for an Angle than a double?

If you're writing in an object-oriented language, it's a good idea to rise above primitives (e.g, double and, yes, string) to encapsulate behavior into objects.

I'd think about an Angle class that would hang onto the value and return it as whatever measure you wanted.

Here's one way to do it in Java. I'll leave the translation to C# and the rest for you.

package angles;

public class Angle
{
    private double value;
    private AngleUnit units;

    public Angle()
    {
        this(0.0, AngleUnit.RADIANS);
    }

    public Angle(double value)
    {
        this(value, AngleUnit.RADIANS);
    }

    public Angle(double value, AngleUnit units)
    {
        this.value = value;
        this.units = units;
    }

    public double getValue()
    {
        return value;
    }

    public AngleUnit getUnits()
    {
        return units;
    }

    public Angle convert(AngleUnit newUnits)
    {
        Angle newAngle = null;

        if (this.units.equals(newUnits))
        {
            return this;
        }

        return newAngle;
    }
}

package angles;

public interface AngleConverter
{
    Angle convert(Angle angle, AngleUnit to);
}

package angles;

public enum AngleUnit
{
    DEGREES, RADIANS, GRADIANS;
}

package angles;

import java.util.HashMap;
import java.util.Map;

public class DegreeConverter implements AngleConverter
{
    private final Map<AngleUnit, Double> factors;

    public DegreeConverter(Map<AngleUnit, Double> factors)
    {
        this.factors = new HashMap<AngleUnit, Double>();
        this.factors.put(AngleUnit.DEGREES, 1.0);
        this.factors.put(AngleUnit.RADIANS, Math.PI/180.0);
        this.factors.put(AngleUnit.GRADIANS, 100.0/90.);
    }

    public Angle convert(Angle angle, AngleUnit to)
    {
        assert angle != null && to != null;

        return new Angle(angle.getValue()*this.factors.get(to), to);
    }
}

这篇关于替代写多个if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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