Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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
C# 继承递归_C#_Wpf - Fatal编程技术网

C# 继承递归

C# 继承递归,c#,wpf,C#,Wpf,所以我尝试在代码中实现一些继承,因为我有许多类需要其他类的相同实例,我将这些实例作为参数传递,但现在我遇到了一个递归问题,我不太明白为什么 它在ParentScreen类中中断(这只是一个普通类,没有XAML)-也许这不是解决我的简单问题的最佳方法 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ParentScreen

所以我尝试在代码中实现一些继承,因为我有许多类需要其他类的相同实例,我将这些实例作为参数传递,但现在我遇到了一个递归问题,我不太明白为什么

它在ParentScreen类中中断(这只是一个普通类,没有XAML)-也许这不是解决我的简单问题的最佳方法

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ParentScreen ps = new ParentScreen();
        container.Children.Add(ps.ms);
    }
}

public class ParentScreen : UserControl
{
    public MainScreen ms;

    public ParentScreen()
    {
        ms = new MainScreen(); // breaks here
    }
}

public partial class MainScreen : ParentScreen
{
    public MainScreen()
    {
        InitializeComponent();
    }
}

<runtime:ParentScreen x:Class="WpfApplication1.MainScreen"
        xmlns:runtime="clr-namespace:WpfApplication1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        Height="100" Width="200">
    <Grid>
        <TextBox Height="23" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" />
    </Grid>
</runtime:ParentScreen>
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
ParentScreen ps=新的ParentScreen();
容器。子对象。添加(ps.ms);
}
}
公共类ParentScreen:UserControl
{
公共主屏幕ms;
公共屏幕()
{
ms=new MainScreen();//此处中断
}
}
公共部分类主屏幕:父屏幕
{
公共主屏幕()
{
初始化组件();
}
}

我认为有两个问题:

  • 你混合了“实例”和“类”的概念 当A继承B并实例化新A时,则只有A的一个实例,而不是2个实例(A&B)

  • 继承并放置默认的构造函数时,将自动调用基本构造函数。这意味着这个代码:

    公共部分类主屏幕:父屏幕 { 公共主屏幕() { 初始化组件(); } }

与以下内容相同:

public partial class MainScreen : ParentScreen
{
    public MainScreen() : base()
    {
        InitializeComponent();
    }
}
所以当你打电话的时候

ParentScreen ps = new ParentScreen();
发生了什么事

  • ParentScreen.CTor=>ms=newmainscreen()
  • MainScreen.CTor=>ParentScreen.CTor(因为MainScreen继承自 父屏幕)=>ms=新主屏幕()
  • MainScreen.CTor=>ParentScreen.CTor
  • ParentScreen.CTor=>ms=newmainscreen()

etc(无限loo)

它应该是stackoverflowException

创建子类时,首先它将调用其父类的构造函数

using System;

class Parent
{
  private Client m_client;
  public Parent()
  {
    m_client = new Client();
    Console.WriteLine("client inited");
  }
}

class Client: Parent
{
  public Client()
  {
    //..
  }
}

class Test
{
  public static void Main(string[] args)
  {
    Client c = new Client();
    Console.WriteLine("End");
  }
}
运行上面的代码,在MacOS上运行Mono时会导致un段错误

检查windows中发生的情况


:)

创建parentScreen对象时,代码会调用创建mainScreen对象的构造函数。由于MainScreen对象继承自ParentScreen-您只需创建一个新的ParentScreen对象,然后再次调用ParentScreen构造函数!->StackOverflowException?!哦,我真的。。。哎呀!谢谢你指出这一点!您需要稍微“重新表述”解决方案(和问题)——也就是说,这不是处理WPF用户控件重用的最令人愉快的方式,出于这样或那样的原因,您最终会遇到问题。这只是一个明显的错误:),但您可能会遇到更多的麻烦,更难发现。继承并不是GUI/WPF组件的最佳选择,在WPF的情况下,继承也不是最好的选择。围绕WPF,您最好尝试通过MVVM和各种控件的数据模板来实现一些东西,并将定制编码控件作为最后手段,这将帮助您,引导您走向.best