using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lei
{
class Program
{
static void Main(string[] args)
{
Shu x = new Shu();
//判定标准一
x.Ss = -9;
Console.WriteLine(x.Ss);
//判定标准二
x.Kk = -9;
Console.WriteLine(x.Kk);
}
}
}
class Shu
{
private int a;
public int Ss//判定标准一
{
get
{
return a;
}
set
{
if (a < 0) a = 0;
else a = value;
}
}
public int Kk//判定标准二
{
get
{
return a;
}
set
{
if (a <= 0) a = 0;
else a = value;
}
}
}
//判定标准一得出的结果为:-9
//判定标准一得出的结果为:0
//两个判定标准结果不应该都为0吗?
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lei
{
class Program
{
static void Main(string[] args)
{
Shu x = new Shu();
//判定标准一
x.Ss = -9;
Console.WriteLine(x.Ss);
//判定标准二
x.Kk = -9;
Console.WriteLine(x.Kk);
}
}
}
class Shu
{
private int a;
public int Ss//判定标准一
{
get
{
return a;
}
set
{
if (a < 0) a = 0;
else a = value;
}
}
public int Kk//判定标准二
{
get
{
return a;
}
set
{
if (a <= 0) a = 0;
else a = value;
}
}
}
//判定标准一得出的结果为:-9
//判定标准一得出的结果为:0
//两个判定标准结果不应该都为0吗?
else a = value;呵呵,自己看不明白?
else a = value; a=-9
第二次 a 也没赋值 一开始 a=0 通过Kk的判断 if (a <= 0) a = 0;
else a = value; a=0
赋值
赋值
那个x.Ss又不是给a 赋值
else a = value; a=-9
第二个 由于你没重新设置a的值为0或其他值,也没有重新new Shu 这个类,所以a 的值保存为上次的-9 在经过if else 语句 就赋值为a = 0了
是判断value是否小于0,不是判断a,a都没赋值之前你判断它干吗,是判断传进来要赋的值是否小于0,也就是value,如果大于0,就把value赋值给a,a就有值了
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lei;
namespace lei
{
class Program
{
static void Main(string[] args)
{
shu b = new shu();
Console.WriteLine(b.sValue(-9));//判定标准一
Console.WriteLine(b.kValue(-9));//判定标准二
}
}
class shu
{
public int sValue(int x)//判定标准一
{
if (x < 0)
return x = 0;
else
return x;
}
public int kValue(int x)//判定标准二
{
if (x <= 0)
return x = 0;
else
return x;
}
}
}
把这个方法改成属性那种写法
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lei
{
class Program
{
static void Main(string[] args)
{
Shu x = new Shu();
//判定标准一
x.Ss = -9;
Console.WriteLine(x.Ss);
//判定标准二
x.Kk = -9;
Console.WriteLine(x.Kk);
}
}
}
class Shu
{
private int a;
public int Ss//判定标准一
{
get
{
return a;
}
set
{
if (value < 0) a = 0;//这里只能够用value来判断,你用a来判断的话,可以如下:
//a=value;
//if(a<0) a=0;
else a = value;
}
}
public int Kk//判定标准二
{
get
{
return a;
}
set
{
if (value <= 0) a = 0;//同上
else a = value;
}
}
}
{
private int ss ;
public int Ss//判定标准一
{
get
{
return ss;
}
set
{
if (ss < 0) ss = 0;
else ss = value;
}
} private int kk;
public int Kk//判定标准二
{
get
{
return kk;
}
set
{
if (kk <= 0) kk = 0;
else kk = value;
}
}
}
{
private int ss ;
public int Ss//判定标准一
{
get
{
return ss;
}
set
{
if (ss < 0) ss = 0;
else ss = value;
}
} private int kk;
public int Kk//判定标准二
{
get
{
return kk;
}
set
{
if (kk <= 0) kk = 0;
else kk = value;
}
}
}
可是红色代码和黑色代码得出结果不一样啊
看懂你的意思了 应该是这样写: class ShuClass
{
private int ss ;
public int Ss//判定标准一
{
get
{
return ss;
}
set
{
if (value < 0) this.ss = 0;
else this.ss = value;
}
} private int kk;
public int Kk//判定标准二
{
get
{
return kk;
}
set
{
if (value <= 0) this.kk = 0;
else this.kk = value;
}
}
}
{
private int ss ;
public int Ss//判定标准一
{
get
{
return ss;
}
set
{
if (ss < 0) ss = 0;
else ss = value;
}
} private int kk;
public int Kk//判定标准二
{
get
{
return kk;
}
set
{
if (kk <= 0) kk = 0;
else kk = value;
}
}
}
可是红色代码和黑色代码得出结果不一样啊看懂你的意思了 应该是这样写 class ShuClass
{
private int ss ;
public int Ss//判定标准一
{
get
{
return ss;
}
set
{
if (value < 0) this.ss = 0;
else this.ss = value;
}
} private int kk;
public int Kk//判定标准二
{
get
{
return kk;
}
set
{
if (value <= 0) this.kk = 0;
else this.kk = value;
}
}
}