public int b { get; set; } public int c { get; set { if (this.b == 1) this.c = value; } }
bdmh 版主大人说的对,你的需求比较无敌,编译器帮你实现不了了,哈哈,不过还有一种思路就是即使不等于1也可以写但是跑出个异常,不知此方法是否科学,例如: private int b = 0; private int c = 0; public int C { get{return c} set { if(b != 1) { //抛出异常 } c = value; } }
class ClassA { private int b; private int c; public int B { set { b = value; } get { return b; } } public int C { set { if (b == 1) { c = value; } }
get { return c; } } }
private int b = 0; private int c = 0; public int C { get{return c} set { if(b != 1) { throw new Exception("b!=1时此属性为只读"); } c = value; } }
很费劲,先这么写了,看行不行. public class A { public virtual int Count { get; set; } } public class A1: A { public override int Count { get { return base.Count; } } } public class A2: A { public override int Count { get { return base.Count; } set { base.Count = value; } }
public int c
{
get;
set
{ if (this.b == 1) this.c = value; }
}
private int b = 0;
private int c = 0;
public int C
{
get{return c}
set
{
if(b != 1)
{
//抛出异常
}
c = value;
}
}
{
private int b;
private int c;
public int B
{
set { b = value; }
get { return b; }
}
public int C
{
set
{
if (b == 1)
{
c = value;
}
}
get { return c; }
}
}
private int c = 0;
public int C
{
get{return c}
set
{
if(b != 1)
{
throw new Exception("b!=1时此属性为只读");
}
c = value;
}
}
public class A
{
public virtual int Count { get; set; }
}
public class A1: A
{
public override int Count
{
get
{
return base.Count;
}
}
}
public class A2: A
{
public override int Count
{
get
{
return base.Count;
}
set
{
base.Count = value;
}
}
}