C# 属性

C# 属性

访问器(Accessors)

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Student
    {
        private string code = "N.A";
        private string name = "not known";
        private int age = 0;
        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;
        }
    }
    class Test
    {
        public static void Main()
        {
            //创建一个新的Student对象
            Student s = new Student();
            //设置student的code、name、age
            s.Code = "001";
            s.Name = "Sofiya";
            s.Age = 1;
            Console.WriteLine("Student Info:{0}", s);
            //添加年龄
            s.Age += 8;
            Console.WriteLine("Student Info:{0}", s);
            Console.ReadKey();
        }
    }
}

运行结果

抽象属性(Abstract Properties)

抽象类可拥有抽象属性,这些属性应在派生类中被实现。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

    class Student:Person
    {
        private string code = "N.A";
        private string name = "not known";
        private int age = 0;
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }
        // 声明类型为 string 的 Name 属性
        public override string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        // 声明类型为 int 的 Age 属性
        public override int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }
        public override string ToString()
        {
            return "Code = " + Code + ", Name = " + Name + ", Age = " + Age;
        }
    }
    class Test
    {
        public static void Main()
        {
            //创建一个新的Student对象
            Student s = new Student();
            //设置student的code、name、age
            s.Code = "001";
            s.Name = "Sofiya";
            s.Age = 1;
            Console.WriteLine("Student Info:{0}", s);
            //添加年龄
            s.Age += 8;
            Console.WriteLine("Student Info:{0}", s);
            Console.ReadKey();
        }
    }
}

运行结果

相关推荐
ou.cs18 分钟前
WPF TreeView 自动展开所有节点:附加行为(Attached Behavior)保姆级实现教程
c#·.net·wpf
鱼蛋-Felix41 分钟前
C#浮点数在部分国家解析失效问题
开发语言·unity·c#
用户298698530141 小时前
C# Word文档页面操作:告别手动,高效掌控你的Word文档!
后端·c#·.net
flysh051 小时前
委托实战案例
开发语言·c#
一念春风2 小时前
可视化视频编辑(WPF C#)
开发语言·c#·wpf
阿蒙Amon13 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
i橡皮擦15 小时前
TheIsle恐龙岛读取游戏基址做插件(C#语言)
开发语言·游戏·c#·恐龙岛·theisle
用户219916797039121 小时前
C# 14 中的新增功能
c#
垂葛酒肝汤1 天前
放置挂机游戏的离线和在线收益unity实现
游戏·unity·c#