简体   繁体   English

在C#中创建一个类

[英]Creating a class in c#

I have read the docs and everything but I'm very confused. 我已经阅读了文档和所有内容,但我很困惑。 I never needed to create a class before and now I do. 我从来不需要现在就创建一个类。

I want to have something like: 我想要类似的东西:

TextDocument.Save("filepath", "contents of file to save");

and stuff like: 和类似的东西:

Application.Create("filepath", "text/code to save");

and: 和:

Stylesheet.Save("filepath", "contents");

and have these in a class and create methods for them but I'm very confused as to how to go about doing it can somebody please help me with this? 并把它们放在一个类中并为它们创建方法,但是我对如何去做感到非常困惑,有人可以帮我吗?

thank you, jase 谢谢,杰瑟

Impossible to say without more code, but those look like static methods, ie create a new class (cs) file, and add something like: 无需更多代码就可以说,但它们看起来像静态方法,即创建一个新的类(cs)文件,并添加以下内容:

using System.IO;
public class TextDocument {
   public static void Save(string path, string contents) {
       File.WriteAllText(path, contents);
   }
}

If TextDocument is actually an instance, take away the word static . 如果TextDocument实际上是一个实例,请TextDocument单词static

Note that to be callable, you also need to know about namespaces. 请注意,要被调用,您还需要了解名称空间。 The above is in the default namespace, but that is a bit vulgar. 上面是默认名称空间中的内容,但是有点粗俗。 It should really be more like: 实际上应该更像是:

using System.IO;
namespace FooCorp.MagicApp {
    public class TextDocument {
        public static void Save(string path, string contents) {
            File.WriteAllText(path, contents);
        }
    }
}

Then only code with using FooCorp.MagicApp will see your class (this is a good thing for retaining sanity; there are lots of classes in the .NET framework) 然后只有using FooCorp.MagicApp代码才能看到您的类(这对保持理智是一件好事; .NET框架中有很多类)

public class Foo
{
   public static Foo Create()
   {
     //do stuff
   }

   public void Save()
   {
     //do stuff
   }
}

You call them differently, because of the static keyword 由于使用static关键字,因此您使用不同的名称

Foo f = Foo.Create();
f.Save();

and I recommend this book - http://oreilly.com/catalog/9780596003760 我推荐这本书-http://oreilly.com/catalog/9780596003760

Note that create is really not the best static method in the world because of things called constructors, but for the sake of examples... 请注意,由于被称为构造函数的原因,create实际上并不是世界上最好的静态方法,而是为了示例...

If you're confused to how classes and objects work, try to read a book (as Russell Steen points out ). 如果您对类和对象的工作方式感到困惑,请尝试读一本书(正如Russell Steen指出的那样 )。

The terminology in tutorials and programming books may confuse beginners, if this is the case for you then try to fetch a programmer who does know and watch him code a class while asking as many questions you can until you get it . 教程和编程书籍中的术语可能会使初学者感到困惑,如果您遇到这种情况,请尝试获取确实了解并观看他编写代码的程序员,同时问尽可能多的问题,直到获得它为止。 Programmers are generally a nice bunch of people. 程序员通常是一群好人。

If you really want to learn more about OOP (Object Oriented Programming), watch someone do some code kata or TDD. 如果您真的想了解有关OOP(面向对象编程)的更多信息,请观看某人做一些代码kata或TDD。

Good luck writing your classes! 祝您写出好运!

Yay! 好极了! I created my first Class! 我创建了第一堂课! :D hehehehe... :D呵呵呵呵...

Here's the complete code. 这是完整的代码。 I don't know if it's the "best" way, or the most "efficient" way of doing this. 我不知道这是“最佳”方式还是最“高效”的方式。 But it works. 但这有效。 I hope it can help others too: 我希望它也可以帮助其他人:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MyApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public String FilePath,
            FileContents = null;

        public class TextDocument
        {
            public void Save(string FilePath, String FileContents)
            {
                File.WriteAllText(FilePath, FileContents);
            }
        }

        private void toolStripButton337_Click(object sender, EventArgs e)
        {
            FilePath = "this.txt";
            FileContents = "testing";

            TextDocument Document = new TextDocument();
            Document.Save(FilePath, FileContents);
        }
    }
}

Thank you Russell Steen and Marc Gravell for your help! 感谢Russell Steen和Marc Gravell的帮助! :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM