From ffdb027ab73ada4fd8b976db470fa9ae9e5ff194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E4=BD=B3=E6=9C=A8?= Date: Thu, 7 Jul 2022 22:43:27 +0800 Subject: [PATCH] =?UTF-8?q?CSharp=5F=E5=8D=9C=E4=BD=B3=E6=9C=A8=5F?= =?UTF-8?q?=E6=99=BA10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw1/hw1/Counting.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hw1/hw1/Counting.cs b/hw1/hw1/Counting.cs index b8e76f4..2944500 100644 --- a/hw1/hw1/Counting.cs +++ b/hw1/hw1/Counting.cs @@ -44,6 +44,31 @@ public interface ICountableVariable public class Counting : ICountableVariable { + private int variable = -1; + private int readTimes; + private int writeTimes; + public int Variable + { + set + { + value = value >= 0 ? value : 0; + writeTimes += (value != variable) ? 1 : 0; + variable = value; + } + get + { + readTimes++; + return variable; + } + } + public int ReadTimes + { + get => readTimes; + } + public int WriteTimes + { + get => writeTimes; + } // 需要实现:分别统计变量Variable被读取、被修改的次数,只允许修改Counting类中的代码 // 要求: // 1. Variable 不能被外部程序赋值为负数。若被赋为负数,则将它代表的值置为0。