C# 接口属性的定义&get、set访问器的简单应用

复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 

namespace 接口属性
{
//定义接口
interface ISeries //interface接口关键字,ISeries接口名称
{
 //定义接口属性
 int next
 {
 get;
 set;
 }
}

//创建一个Numble类,实现ISeries接口
class Numble : ISeries
{
 int val;
 public Numble()
 {
 val = 0;
}

//实现属性
public int next
{
 get
 {
 val += 2;
 return val;
}
set
{
 val = value;
}
}
}
class Program
{
 public void Run()
 {
 Numble num = new Numble();
 //访问接口属性
 for(int i=0;i<5;i++)
 Console.WriteLine("Next Value is "+num.next);
}
static void Main(string[] args)
{
 Program p=new Program();
 Numble num = new Numble();
 p.Run(); //这里展示了如何调用类自身的函数,先new一个Program(),然后再调用

 Console.WriteLine("Starting at 21");
 num.next = 21;
 for (int i = 0; i < 5; i++)
 Console.WriteLine("Next Value is " + num.next);

 Console.ReadLine();
 }
}
}

复制代码
posted @   盛开的雨季  阅读(10226)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示