Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写Python';在C#中的uu init_uuu块末尾调用s super()?_C#_Python_Inheritance - Fatal编程技术网

如何编写Python';在C#中的uu init_uuu块末尾调用s super()?

如何编写Python';在C#中的uu init_uuu块末尾调用s super()?,c#,python,inheritance,C#,Python,Inheritance,我一直在将大量Python代码移植到C#,并经常在Python中的\uu init\uuuu块末尾遇到super()。当派生类\uuu init\uu块中的某些python代码在调用基构造函数之前执行某些操作时,就会出现问题。在C#中,假定派生构造函数需要基本构造函数才能工作,并且在派生构造函数中完成“stuff”之前,首先调用它们。我通常的解决方法是编写一个静态方法并在base内部调用它(例如,public:base(DoStuff(someVariable))。当我有一个参数基构造函数时,这

我一直在将大量Python代码移植到C#,并经常在Python中的
\uu init\uuuu
块末尾遇到
super()。当派生类
\uuu init\uu
块中的某些python代码在调用基构造函数之前执行某些操作时,就会出现问题。在C#中,假定派生构造函数需要基本构造函数才能工作,并且在派生构造函数中完成“stuff”之前,首先调用它们。我通常的解决方法是编写一个静态方法并在base内部调用它(例如,
public:base(DoStuff(someVariable))
。当我有一个参数基构造函数时,这很好。但是如果我有三个参数呢?我不想使用三个不同的静态方法将
\uuuu init\uuuuu>块代码重复三次,所以我编写了一个方法,返回第一个参数,并在局部变量中设置其他参数

在将结果传递给C#中的基构造函数之前,有没有更好的方法来完成对派生构造函数中两个+变量的处理

以下是我编写的一些测试代码,用于观察我的变通方法的行为:

using System;

namespace BaseSuperTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("In Main...");
            var B = new B(0,0,"zero");

            Console.WriteLine();

            var C = new C();
        }
    }

    class A
    {
        public A(int first, int second, string third)
        {
            Console.WriteLine("\tIn A's constructor...");
            Console.WriteLine("\t{0} {1} {2}", first, second, third);
        }
    }

    class B : A
    {
        public static int Second { get; set; }

        public static string Third { get; set; }

        private static int DoStuff(int first, int second, string third)
        {
            Console.WriteLine("\t\tIn DoStuff()...");
            Second = second + 1;
            Third = "changed";
            return first + 1;
        }

        public B(int first, int second, string third) : base(DoStuff(first, second, third), Second, Third)
        {
            Console.WriteLine("\t\tIn B's constructor...");
        }
    }

    class C : B
    {
        public C() : base(0,0,"zero")
        {
            Console.WriteLine("\t\t\tIn C's constructor...");
        }
    }
}

如果您必须在初始化基之前初始化派生中的字段,这听起来像是设计问题。您实际上想实现什么?@YuvalItzchakov我完全同意设计离理想还很远。部分问题是我从如此庞大的代码库移植,现在我只是试图与您复制行为不要改变太多的设计。所以,现在,我需要一个派生类,以便能够在调用基类构造函数之前,对传入它的参数进行处理。我知道,大型代码库可能会对端口造成问题。但是,如果您没有考虑高级别的设计,您只需要做一个补丁呃,另一个,我不确定是否会产生更好的代码。我不知道您为什么要移植代码,但我认为您不希望最终得到比现有代码更少的可维护代码。@YuvalItzchakov我将与我的团队讨论这个问题,因为我同意我们不需要更少的可维护代码。目标是拥有更多可管理的代码。@YuvalItzchakov具有更高测试覆盖率的e代码。同时,我希望至少确保我的变通方法有效。我还将与我的团队一起研究更好的设计模式。