于梦想齐行
于梦想齐行

C#属性(Property)

C#属性(Property)
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //C#属性(Property)
            /*
             * 属性(Property)是类(class)、结构(structure)和接口(interface)的命名(named)成员。类或结构中的成员变量或方法称为域(Field)。属性(Property)是域(Field)的扩展,且可使用相同的语法来访问。它们使用访问器(accessors)让私有域的值可被读写或操作。
             * 属性(Property)不会确定存储位置。相反,它们具有可读写或计算它们的值的访问器(accessors)。
             * 例如,有一个名为Student的类,带有age、name和code的私有域。我们不能在类的范围以外直接访问这些域,但是我们可以拥有访问这些私有域的属性。
             * 
             */
            //访问器(Accessors)
            /*
             * 属性(Property)的访问器(accessors)包含有助于获取(读取或计算)或设置(写入)属性的可执行语句。访问器声明可包含一个get访问器、一个set访问器或者同时包含二者。
             * 
             */
            //实例:见class Student
            Student s = new Student();
            s.Code = "001";
            s.Name = "Zara";
            s.Age = 9;
            Console.WriteLine("Student Info: {0}", s);
            s.Age += 1;
            Console.WriteLine("Student Info: {0}", s);
            //抽象属性(Abstract Properties)
            /*
             * 抽象类可拥有抽象属性,这些属性应在派生类中被实现。
             */
            var s2 = new Student()
            {
                Code = "002",
                Name = "Zara",
                Age = 09
            };
            Console.WriteLine($"Student Info:={s2}");
            s2.Age++;
            Console.WriteLine($"Student Info:={s2}");

            Console.ReadKey();
        }
    }

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

    public class Student2 : Person
    {
        public string Code { get; set; } = "N.A";
        public override string Name { get; set; } = "NA";
        public override int Age { get; set; } = 0;
        public override string ToString()
        {
            return $"Code: {Code}, Name: {Name}, Age: {Age}";
        }
    }

    class Student
    {
        private string code="N.A";
        private string name="not known";
        private int age=0;

        //声明类型为string的Code属性
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }
        //声明类型为string的Name属性
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        //声明类型为int的Age属性
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
        public override string ToString()
        {
            return "Code="+Code+", Name="+Name+", Age="+Age;
        }
    }
}
没有标签
首页      CSharp      C#属性(Property)

于梦想齐行

C#属性(Property)
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //C#属性(Property) /* …
扫描二维码继续阅读
2022-06-29
近期文章
近期评论