加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 19:58:34 所属栏目:百科 来源:网络整理
导读:public class PhotoList : ObservableCollectionImageFile{ public PhotoList() { } **//this is the line that I dont recognise!!!!!!!!!!** public PhotoList(string path) : this(new DirectoryInfo(path)) { } public PhotoList(DirectoryInfo director
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读