这个封装代码有什么问题 [英] What is wrong with this encapsulation code
本文介绍了这个封装代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello Friends,
我无法在此代码中发现问题。
我的输出应该是 - 当我将Mark的值传递为1时应该返回0,当我作为20传递时,它应该返回20(通过属性'Mark')。但目前它在所有情况下仅返回0,0。
请帮助。
Hello Friends,
I am not able find out the problem in this code.
My output should be - when i pass the value of Mark as 1 it should return 0 and when i pass as 20 , it should return 20 (via the property 'Mark'). But currently its returning only 0,0 in all cases.
Please help.
class Program
{
static void Main(string[] args)
{
Encapsulation encapsulate = new Encapsulation();
encapsulate.Mark = 1;
encapsulate.Display();
encapsulate.Mark = 20;
encapsulate.Display();
Console.ReadKey();
}
}
public class Encapsulation
{
private int _mark;
public int Mark
{
get { return _mark; }
set
{
if (_mark < 5)
_mark = 0;
else
_mark = value;
}
}
public void Display()
{
Console.WriteLine("Mark: " + Mark);
}
}
推荐答案
我怀疑if(_mark < 5)
应为if(value< 5)
这篇关于这个封装代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文