下面是一个简单的 C# 对象实例:

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

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

    public void Introduce()
    {
        Console.WriteLine('Hi, my name is ' + Name + '. I am ' + Age + ' years old and work as a ' + Occupation + '.');
    }
}

// 创建一个 Person 实例
Person john = new Person('John', 32, 'Software Developer');

// 调用 Introduce 方法
john.Introduce();

输出结果:

Hi, my name is John. I am 32 years old and work as a Software Developer.

这段代码展示了如何定义一个名为 'Person' 的类,它包含 'Name'、'Age' 和 'Occupation' 属性。此外,它还包含一个名为 'Introduce' 的方法,该方法输出关于 Person 对象的信息。最后,我们创建了一个名为 'john' 的 'Person' 实例,并调用了它的 'Introduce' 方法,从而显示相关信息。

C# 对象实例:创建和使用 Person 类

原文地址: https://www.cveoy.top/t/topic/m5GW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录