首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类似于头文件,我可以在C#中分离一些定义吗?

类似于头文件,我可以在C#中分离一些定义吗?
EN

Stack Overflow用户
提问于 2020-02-29 04:57:30
回答 2查看 73关注 0票数 0

简而言之,我希望在一个地方实现一种基本结构,在另一个地方实现/定义。我想要更好地“看到”互连性,而不是所有的功能都遮蔽了它,主要是为了设计讨论,解释等。我可以用继承来做到这一点,但我真的不想为了实现这一点而改变所有东西的名称。这是某种意义上的东西吗?

代码语言:javascript
复制
// Simple File for seeing relationships between classes
public class AllMyObjectTypes // A class because it will be its own object with functionality below all this structural stuff
{
    public class Thing1
    {
        public Thing2[] things2;
        public Thing3[] things3;
    }

    public class Thing2[]
    {
        public int version;
        public Thing1[] thing1Utilizers;
    }

    public class Thing3[]
    {
        public string Title;
    }
}


// Complicated file for doing all the hard work for Thing1 with all the internal variables to make it happen.
public class Thing1 : Thing1 // Implement itself somehow?
{
    // Stuff I want to use and define but not cloud the structure above
    private int[] internalStuff; 
    private string moreInternalStuff;

    public void UsefulFunctionButWantSeparated()
    {
        // Hundreds of lines of code clouding junk up
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-02 21:39:35

分部类正是我要找的,但它确实要求我不能嵌套在另一个类中。除非我把那部分做得太过...?但不管怎样,这让我离我的目标最近

代码语言:javascript
复制
// Simple File for seeing relationships between classes
//public class AllMyObjectTypes // A class because it will be its own object with functionality below all this structural stuff
//{
    public partial class Thing1
    {
        public Thing2[] things2;
        public Thing3[] things3;
    }

    public partial class Thing2[]
    {
        public int version;
        public Thing1[] thing1Utilizers;
    }

    public partial class Thing3[]
    {
        public string Title;
    }
//}


// Complicated file for doing all the hard work for Thing1 with all the internal variables to make it happen.
public partial class Thing1 // More implementation
{
    // Stuff I want to use and define but not cloud the structure above
    private int[] internalStuff; 
    private string moreInternalStuff;

    public void UsefulFunctionButWantSeparated()
    {
        // Hundreds of lines of code [no longer] clouding junk up
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-02-29 05:14:03

接口和类声明

代码语言:javascript
复制
public interface IThing
{
    IThing2[] Thing2s();
    string DoSomething();
}

public class Thing : IThing
{
    private readonly IThing2[] _thing2s = new IThing2[1] { new Thing2() };
    public IThing2[] Thing2s() => _thing2s;

    public string DoSomething()
    {
        return "MyText";
    }
}

public interface IThing2
{

}

public class Thing2 : IThing2
{

}

使用

代码语言:javascript
复制
IThing thing;
thing = new Thing();
var thing2s = thing.Thing2s();
var txt = thing.DoSomething();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60459141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档