環境是 vistual studio 2019 ,net.core 3.0 在控制臺應用環境里 練習封裝和屬性 不知道帶條件的set如何寫 ,原程式如下 可以執行
namespace ConsoleApp2
{
public class Student
{
private int _AGE;
public int AGE
{ get => _AGE; set => _AGE = value; }
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.AGE = 18; //設定屬性值,使該物件的Age值為18
Console.WriteLine($"小明的年齡是{student.AGE}歲"); //獲得student.Age屬性值
Console.ReadKey();
}
}
}
現在想加上條件 年齡賦值在 0到100歲之間 就直接賦值,如果 不在這個范圍 就直接賦值20 .
uj5u.com熱心網友回復:
set => {if(value>0 && value<50)
_AGE = value;
else
_AGE=20;
}
uj5u.com熱心網友回復:
按照你給的格式 不行 見附圖 能否幫我再看看 { 有錯誤 紅色
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/224654.html
標籤:C#
上一篇:通過ffmpeg管道直播,ffmpeg啟動完成后消失了。
下一篇:百度地圖如何離線下載省份地圖。
