C# 类 - 基本示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14627180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:28:34  来源:igfitidea点击:

C# classes - basic example

c#class

提问by Jeyekomon

This is basically my first attempt to understand the classes in C#. I've went through several tutorials on the internet, but the thing I'm missing the most and what I haven't found anywhere yet, is a simple good example.

这基本上是我第一次尝试理解 C# 中的类。我已经在互联网上浏览了几个教程,但我最想念的东西和我还没有在任何地方找到的东西,是一个简单的好例子。

I have some idea how my basic program should look like and I would appreciate your help:

我有一些想法我的基本程序应该是什么样子,我很感激你的帮助:

using System;

namespace Introduction_to_classes
{
    class Person
    {
        int Age;
        string Name;

        int DateOfBirth()
        {
            return 2013 - Age;
        }
    }

    class Program
    {
        public static void Main()
        {
            Person Mother = new Person(35, Alice);
            Person Son = new Person(12, Johny);

            Mother.Name = "Lucy";  // Just changing the value afterwards

            if(Mother.Age > Son.Age)
            {
                int year = Mother.DateOfBirth();
                Console.WriteLine("Mom was born in {0}.", year);
            }

            Console.ReadLine();
        }
    }
}

It's just an idea, it definitely doesn't work. But more than anything else it would help me if you can correct it to the working example...

这只是一个想法,它绝对行不通。但最重要的是,如果您可以将其纠正为工作示例,它将对我有所帮助......

采纳答案by laszlokiss88

class Person
{
    public int Age { get; set; }
    public string Name { get; set; }

    public Person(int age, string name)
    {
        Age = age;
        Name = name;
    }

    public int DateOfBirth()
    {
        return 2013 - Age;
    }
}

        class Program
        {
            public static void Main()
            {
                Person Mother = new Person(35, "Alice");
                Person Son = new Person(12, "Johny");

                Mother.Name = "Lucy";  // Just changing the value afterwards

                if (Mother.Age > Son.Age)
                {
                    int year = Mother.DateOfBirth();
                    Console.WriteLine("Mom was born in {0}.", year);
                }
            }
        }

Some useful links: properties, constructor

一些有用的链接:属性构造函数

回答by Ken Kin

using System;

namespace Introduction_to_classes {
    class Person {
        public int Age;
        public string Name;

        public int DateOfBirth() {
            return 2013-Age;
        }
    }

    class Program {
        public static void Main() {
            Person Mother=new Person {
                Age=35,
                Name="Alice"
            };

            Person Son=new Person {
                Age=12,
                Name="Johny"
            };

            Mother.Name="Lucy";  // Just changing the value afterwards

            if(Mother.Age>Son.Age) {
                int year=Mother.DateOfBirth();
                Console.WriteLine("Mom was born in {0}.", year);
            }

            Console.ReadLine();
        }
    }
}

回答by Theodoros Chatzigiannakis

The problem is that you're referring to a constructor that doesn't exist:

问题是你指的是一个不存在的构造函数:

Person Mother = new Person(35, Alice);

The first argument here is an int, the second should be a stringas far as I understand. But a string literal should be marked with double quotes, so that line should be:

这里的第一个参数是 an intstring据我所知,第二个参数应该是 a 。但是字符串文字应该用双引号标记,所以该行应该是:

Person Mother = new Person(35, "Alice");

Same for the following line.

下一行也一样。

Now you probably want a constructor that takes types of these arguments and you want to save these values to the new object, I assume. So, add this in your Personclass:

我假设现在您可能想要一个接受这些参数类型的构造函数,并且您想将这些值保存到新对象中。因此,在您的Person课程中添加以下内容:

public Person(int a, string n)
{
    this.Age = a;
    this.Name = n;
}

And, finally, you should make your Ageand Namefields accessible to the other class, by marking them internalor public:

最后,您应该通过标记它们或使您的AgeName字段可供其他类访问:internalpublic

    public int Age;
    public string Name;

After that, you should be good to go.

在那之后,你应该很高兴。

回答by Nolonar

First of all: new Person(35, "Alice")implies that class Persondefines a constructor public Person(int age, string name). Alternatively, you'll have to call new Person() { Age = 35, Name = "Alice" }which only works as long as you have not defined a constructor, or have defined a constructor that takes 0 arguments, such as public Person()(notice how I put "Alice" within quotation marks? That's because you didn't define a string called Alice, so Aliceis an unknown object)

首先:new Person(35, "Alice")意味着class Person定义了一个构造函数public Person(int age, string name)。或者,您必须调用new Person() { Age = 35, Name = "Alice" }which 仅在您未定义构造函数或已定义接受 0 个参数的构造函数时才有效,例如public Person()(注意我如何将“Alice”放在引号中?那是因为您没有' t 定义一个名为 的字符串Alice,因此Alice是一个未知对象)

Next we have Mother.Name = "Lucy", which won't work, because Nameis not discoverable. The class Persondoes define a Name, but since you didn't specify an access modifier, such as publicor private, class Programdoesn't even know it exists and thus cannot access it. So you have to use public string Nameinstead of string Name. It is also considered to be good style to alwaysspecify your access modifier. The same applies to public int Ageand public int DateOfBirth()as well.

接下来我们有Mother.Name = "Lucy",这将不起作用,因为Name无法发现。在class Person做定义Name,但因为你没有指定访问修饰符,如publicprivateclass Program甚至不知道它的存在,因此无法对其进行访问。所以你必须使用public string Name而不是string Name. 始终指定访问修饰符也被认为是一种很好的风格。这同样适用于public int Agepublic int DateOfBirth()

To know more about access modifiers refer to http://msdn.microsoft.com/en-us/library/ms173121.aspx

要了解有关访问修饰符的更多信息,请参阅http://msdn.microsoft.com/en-us/library/ms173121.aspx