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

运行结果

相关推荐
向上的车轮12 小时前
为什么.NET(C#)转 Java 开发时常常在“吐槽”Java:checked exception
java·c#·.net
心疼你的一切14 小时前
Unity异步编程神器:Unitask库深度解析(功能+实战案例+API全指南)
深度学习·unity·c#·游戏引擎·unitask
.房东的猫1 天前
ERP(金蝶云星空)开发【安装篇】
c#
fie88891 天前
基于C#的推箱子小游戏实现
开发语言·c#
.房东的猫1 天前
ERP(金蝶云星空)开发【业务数据中心创建和注册】
c#
bugcome_com1 天前
C# 进阶核心知识点汇总|多项目开发 + 委托 + Lambda + 事件一次吃透
c#
SunflowerCoder1 天前
基于插件化 + Scriban 模板引擎的高效 HTTP 协议中心设计
http·c#
青云计划2 天前
知光项目用户关系模块
c#·linq
m5655bj2 天前
使用 C# 修改 PDF 页面尺寸
java·pdf·c#
专注VB编程开发20年2 天前
c#模仿内置 Socket.Receive(无需 out/ref,直接写回数据)
开发语言·c#