c#构造函数中的if语句 [英] c# if statement in constructor

查看:159
本文介绍了c#构造函数中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是一个简单的问题。



构造函数是否仅用于将数据分配给数据字段?



我可以在构造函数中使用if语句还是应该避免使用?

我仍​​然可以在我的应用程序主体中使用它们我不确定在这种情况下会有什么更好。



谢谢。









示例:



I just have a quick question.

Are constructors used only for assigning data to data fields?

Could I use if statements in constructor or should it be avoided?
I could still use them in the main body of my application I am just not sure what would be better in this situation.

Thank you.




EXAMPLE:

class Calculation
    {
        // every time you use a comboBox you have to create a load event in order to populate it
        public static char[] actors = { '+', '-', '*', '/' };

        decimal x;
        decimal y;
        char actor;
        decimal result;

        public Calculation(decimal _x, decimal _y, char _actor)
        {
            x = _x;
            y = _y;
            actor = _actor;

            // if actor this then result = add() etc...
        }

        public override string ToString()
        {
            return String.Format("{0} {1} {2} = {3}", x, actor, y, result);
        }

        public decimal add()
        {
            return result = x + y;
        }

        public decimal subtract()
        {
            return result = x - y;
        }

        public decimal multiply()
        {
            return result = x * y;
        }

        public decimal divide()
        {
            return result = x / y;
        }
    }

推荐答案

当然可以!如果需要在构造函数中完成工作,以便在构造对象后数据准备就绪并且一致,那么在构造函数中执行它。



如果没有不需要做 - 然后不要 - 保持你的构造函数尽可能短!
Of course you can! If work needs to be done in the constructor so that data is ready and consistent as soon as an object is constructed, then do it in the constructor.

If it doesn't need to be done, then don't - keep your constructor(s) as short as possible!


是的。做你需要做的事。



OOP最大的特色之一就是封装,特定方法(甚至是构造函数)中发生的事情都停留在方法中调用者不需要担心它。

如果在构造函数中放置 if 允许你有一个构造函数而不是让调用者选择从几个构造函数中,我会说这是一件好事。
Yes. Do what you need to do.

One of the greatest features of OOP is encapsulation, what happens in a particular method (even a constructor) stays in the method and the caller doesn't need to worry about it.
If putting an if in the constructor allows you to have one constructor rather than have the caller select from several constructors, I'd say that's a good thing.


你可以这样做但是在构造函数中编写业务逻辑并不是一个好主意。



构造函数的主要用途是在创建类的新实例时初始化局部变量。



//如果是演员,那么结果= add()等......



对于这行代码,您可以将逻辑实现到主类中。或者您可以创建单独的方法,您可以接受三个参数:

1.演员

2. Number1

3. Number2



问候,

CodeBlack
You can do this but It is not a good idea to write your business logic in constructor.

The main purpose of the constructor is to initialize local variables when new instance of your class is created.

// if actor this then result = add() etc...

For this line of code you can implement your logic into main class. Or you can create separate method where you accept three parameters :
1. Actor
2. Number1
3. Number2

Regards,
CodeBlack


这篇关于c#构造函数中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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