A.3.2创建简单的类(人类),包含的概念(字段,构造,封装字段,创建方法,创建对象,赋值,调用方法)

ylbtech- .NET-Basic: A.3.2-创建简单的类(人类),包含的概念(字段,构造,封装字段,创建方法,创建对象,赋值,调用方法)

A.3.2-创建简单的类(人类),包含的概念(字段,构造,封装字段,创建方法,创建对象,赋值,调用方法)

1.A,源代码返回顶部
1.A.1,Person.cs
using System;

namespace Test2
{
    class Person
    {
        //字段
        int id;//编号
        string name;//姓名
        int age;//年龄

        //两参构造
        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
        //全参构造
        public Person(int id, string name, int age)
        {
            this.id = id;
            this.name = name;
            this.age = age;
        }
        //方法
        public void Show()
        {
            Console.WriteLine("编号:{0},姓名:{1},年龄:{2}", id,name,age);
        }

        //封装字段
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
}
1.A.2,Program.cs
using System;

namespace Test2
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person(10000, "jik", 4);

        //调用方法
            p.Show();

            Console.Read();
        }
    }
}
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ylbtech/p/2980462.html