我如何在C#中实现类 [英] How Can I Implement Class in C#

查看:93
本文介绍了我如何在C#中实现类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中实现类

How Can I Implement Class in C#
and how can I call it and do all operation?

推荐答案

我很想删除它,但是我希望您能认真地问这个问题.您要问的内容太基本了,我什么都不知道",对于简短的回答就足够了.在这里问问题之前,您应该先买书并仔细阅读.
I am tempted to delete this, but I''m going to hope you''re asking this seriously. What you''re asking is too basic, too ''I have no idea about anything'', for a brief answer to suffice. You should buy a book and work through it before you ask questions here.


为什么您不以Google的价格购买?
:)
Why don''t you Google for "Object Oriented Programming in C# Tutorial"?
:)


让我们从最基本的层次开始.通常,您会创建一个以.cs扩展名结尾的文件.在此文件中,您将添加一些using语句(取决于您要执行的操作),名称空间和类.这是一个简单的示例:
Let''s start at the most basic level. Normally you''d create a file that ends in the .cs extension. In this file, you''ll add some using statements (depending on what you want to do), a namespace and a class. Here''s a simple sample:
using System;
namespace MyNamespace
{
  public class MyClass
  {
    public void DoSomething()
    {
      DoSomethingElse();
    }
    private void DoSomethingElse()
    {
      Console.WriteLine("Hello world");
    }
  }
}

在另一个类中,您可以通过使用new关键字实例化该类来调用此方法(假设您不使用反射).

In a different class, you would call this by instantiating the class using the new keyword (assuming you aren''t using reflection). It would look like this:

MyClass myClass = new MyClass();
myClass.DoSomething();

请注意,您可以直接调用DoSomething,因为它是公共的,但是您不能调用DoSomethingElse,因为它已被private关键字隐藏.这使您可以编写隐藏应用程序内部逻辑的代码,同时仍然允许定义明确的入口点.

我建议此时,您购买许多优秀的C#初学者书籍之一,例如
学习C# [^ ]作者:Jesse Liberty.

Note that you can directly call DoSomething because it is public, but you cannot call DoSomethingElse because it is hidden by the private keyword. This allows you to write code that hides the internal logic of your application while still allowing well defined entry points.

I would suggest at this point that you purchase one of the many excellent beginners C# books, such as Learning C#[^] by Jesse Liberty.


这篇关于我如何在C#中实现类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆