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

运行结果

相关推荐
zdr尽职尽责1 天前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
BruceNeter3 天前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
何以解忧唯有撸码4 天前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
国家一级摸鱼选手5 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库5 天前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc5 天前
C# 发展历史
开发语言·c#
kyle~5 天前
C# --- 反射
开发语言·c#
暴走约伯6 天前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316347 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船7 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua