c# – 具有空主体和类似继承语法的构造函数有什么作用?

c# – 具有空主体和类似继承语法的构造函数有什么作用?,第1张

概述public class PhotoList : ObservableCollection<ImageFile>{ public PhotoList() { } **//this is the line that I dont recognise!!!!!!!!!!** public PhotoList(string path) : this(new Direct
public class PhotoList : ObservableCollection<Imagefile>{    public PhotoList() { }    **//this is the line that I  dont recognise!!!!!!!!!!**    public PhotoList(string path) : this(new DirectoryInfo(path)) { }    public PhotoList(DirectoryInfo directory)    {        _directory = directory;        Update();    }    public string Path    {        set        {            _directory = new DirectoryInfo(value);            Update();        }        get { return _directory.Fullname; }    }    public DirectoryInfo Directory    {        set        {            _directory = value;            Update();        }        get { return _directory; }    }    private voID Update()    {        foreach (fileInfo f in _directory.Getfiles("*.jpg"))        {            Add(new Imagefile(f.Fullname));        }    }    DirectoryInfo _directory;}
解决方法 这称为构造函数链接 – 构造函数可以使用此语法调用同一类型中的其他构造函数(对于兄弟构造函数和基础构造函数的基础使用此构造函数).

这是一个简单的例子,展示了它的工作原理:

using System;class Program{    static voID Main()    {        Foo foo = new Foo();    }}class Foo{    public Foo() : this("hello")    {        Console.Writeline("world");    }    public Foo(String s)    {        Console.Writeline(s);    }}

输出:

hello
world

总结

以上是内存溢出为你收集整理的c# – 具有空主体和类似继承语法的构造函数有什么作用?全部内容,希望文章能够帮你解决c# – 具有空主体和类似继承语法的构造函数有什么作用?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1225523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存