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

发布于 2024-07-19 19:50:46 字数 908 浏览 2 评论 0原文

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;
}
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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

却一份温柔 2024-07-26 19:50:46

这称为构造函数链接 - 构造函数可以使用此语法调用同一类型中的其他构造函数(对同级构造函数使用 this ,对同级构造函数使用 < code>base 用于基本构造函数)。

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

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);
    }
}

输出:

你好
世界

This is called constructor chaining - constructors can call other constructors within the same type with this syntax (using this for sibling constructors and base for base constructors).

Here is a simple example that shows how it works:

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);
    }
}

Output:

hello
world

世态炎凉 2024-07-26 19:50:46

它调用类中以 DirectoryInfo 作为参数的另一个构造函数。

让我们看看如何使用这个类的调用者

//The empty ctor()
PhotoList list = new PhotoList();

//The ctor that takes a DirectoryInfo
PhotoList list2 = new PhotoList(new DirectoryInfo("directory")); 

//Would do the same as the code above since this constructor calls another constructor via the this() keyword
PhotoList list3 = new PhotoList("directory");

It calls the other constructor in the class that takes a DirectoryInfo as argument.

Lets see how the caller of this class can be used

//The empty ctor()
PhotoList list = new PhotoList();

//The ctor that takes a DirectoryInfo
PhotoList list2 = new PhotoList(new DirectoryInfo("directory")); 

//Would do the same as the code above since this constructor calls another constructor via the this() keyword
PhotoList list3 = new PhotoList("directory");
浮生面具三千个 2024-07-26 19:50:46

它使采用字符串参数的构造函数调用采用 DirectoryInfo 参数的构造函数,并将新的 DirectoryInfo 对象(该对象又使用字符串作为参数)传递给它。

我经常使用这种方法为复杂的类提供更简单的构造函数,让类本身使用默认值初始化属性,而不必重复初始化代码。

It makes the constructor that takes a string parameter invoke the constructor that takes a DirectoryInfo parameter, passing a new DirectoryInfo object (which in turn is using the string as parameter) to it.

I often use this approach to provide simpler constructors to complex classes, letting the class itself initializse properties with default values without having to duplicate intitiallization code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文