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

运行结果

相关推荐
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹1 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
kybs19911 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
Polevne2 天前
C# SFTP客户端实现文件传输
c#·ssh
samble2 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
cjp5602 天前
024 . WPF MVVM类封装优化
c#
淡海水2 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
asdzx672 天前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
2501_930707782 天前
使用C#代码使用条件格式为 Excel 隔行设置颜色
c#·excel
何以解忧唯有撸码2 天前
【开源】c#造个轮子-日程安排工具
c#·自定义控件