C# - 在一些计算之后从另一个构造函数调用构造函数

时间:2016-04-06 09:15:32

标签: c# constructor

我有2个构造函数,接受不同类型的参数:

public someClass(String s) {

    // the string is parsed to an int array.
    int[] array = doSomething(s);

    this(array);
}

public someClass(int[] array) {
    doSomethingElse(array);
}

然而,在第一个构造函数中,我得到"方法名称是预期的"。有没有办法让构造函数在执行其他操作后调用另一个,或者它只是C#的限制?

3 个答案:

答案 0 :(得分:8)

除非display:none是静态的。

$(function () {
var nav = $('#gads300x600');
var footer = $('#copyright');
var navHomeY = nav.offset().top;
var navFooterY = footer.offset().top;
var isFixed = false;
var $w = $(window);
$w.scroll(function () {
    var scrollTop = $w.scrollTop();
    var topSidebar = navHomeY;
    var topFooter = navFooterY;
    var bottomSidebar = nav.offsetHeight + topSidebar;
    var footerIsOnSight = (scrollTop + $window.innerHeight) >= topFooter;
    var shouldBeFixed = (scrollTop + $window.innerHeight) >= bottomSidebar ;
    var maxY = navFooterY - nav.outerHeight();

    if (!isFixed && shouldBeFixed) {
        nav.css({
            position: 'fixed',
            top: '90px',
            left: nav.offset().left,
            width: nav.width()
        });
        nav.css('z-index', 1000);
        isFixed = true;
    }

    if ((isFixed && (scrollTop <= 0))) {
        nav.css({
            position: 'absolute',
            top: '0px',
            left: nav.offset().left,
            width: nav.width()
        });
        nav.css('z-index', 1000);
        isSticked = false;
    } else {
        if (isSticked && footerIsOnSight) {
            nav.css({
                position: 'static'
            });
            isFixed = false;
        }
    }
});
});

答案 1 :(得分:2)

你做不到。但你可以写

public SomeClass(string s) : this(doSomething(s)){}

完全没问题,只要int[] doSomething(string)static

答案 2 :(得分:2)

根据 Call one constructor from another

public class SomeClass
{
    public SomeClass(string s) : this(dosomething(s))
    {

    }

    public SomeClass(int[] something)
    {
    }


    private static int[] dosomething(string)
    {
        return new int[] { };
    }
}

我会使用静态方法来实现你想要的东西