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();
        }
    }
}

运行结果

相关推荐
正运动技术44 分钟前
PC强实时运动控制(一):C#的EtherCAT总线初始化(上)
c#·运动控制·正运动技术·运动控制器·ethercat·正运动·运动控制内核
d111111111d1 小时前
在STM32中,中断服务函数的命名有什么要求?
笔记·stm32·单片机·嵌入式硬件·学习·c#
极客智造3 小时前
深入解析 C# Type 类:解锁反射与动态编程的核心
c#·反射
SmoothSailingT3 小时前
C#——textBox控件(1)
开发语言·c#
superman超哥3 小时前
仓颉语言中并发集合的实现深度剖析与高性能实践
开发语言·后端·python·c#·仓颉
工程师0074 小时前
C#中的服务注册剖析
c#·服务注册
张人玉4 小时前
c#DataTable类
数据库·c#
缺点内向4 小时前
如何在 C# .NET 中将 Markdown 转换为 PDF 和 Excel:完整指南
pdf·c#·.net·excel
CodeCraft Studio4 小时前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建旭日图
c#·excel·aspose·excel旭日图·excel库·excel开发控件·excel api库
民乐团扒谱机4 小时前
【微实验】仿AU音频编辑器开发实践:从零构建音频可视化工具
算法·c#·仿真·audio·fft·频谱