
使用c#构造函数时,为什么属性set中的判断条件无效
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespa...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02构造方法
{
class student
{
public student(string name, char sex, double chinese,double english)
{
this.name = name;
this.Sex = sex;
this.chinese = chinese;
this.english = english;
}
private string name;
public string Name
{
get { return name; }
set
{
if (value != "张三" && value != "李四")
{
name = "匿名";
}
else
{
name = value;
}
}
}
private char sex;
public char Sex
{
get { return sex; }
set {
if (value != '男' && value != '女')
{
sex = '男';
}
else
{
sex = value;
}
}
}
private double chinese;
public double Chinese
{
get { return chinese; }
set
{
if (value < 0 || value > 100)
{
chinese = 0;
}
else
{
chinese = value;
}
}
}
private double english;
public double English
{
get { return english; }
set { english = value; }
}
public void sayhello()
{
Console.WriteLine("我的名字叫{0},今年{1}岁了,是位{2}生!",name ,age ,sex );
}
public void score()
{
Console.WriteLine("我的语文成绩为{0},数学成绩为{1},英语成绩为{2},平均成绩为{3}!",chinese ,match ,english ,(chinese +match +english )/3);
}
}
}
static void Main(string[] args)
{
student stu2 = new student("李四", '女', -78, 90);
stu.sayhello();
stu.score();
stu2.sayhello();
stu2.score();
Console.ReadKey();
}
为什么-78输出时没有变为0? 展开
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02构造方法
{
class student
{
public student(string name, char sex, double chinese,double english)
{
this.name = name;
this.Sex = sex;
this.chinese = chinese;
this.english = english;
}
private string name;
public string Name
{
get { return name; }
set
{
if (value != "张三" && value != "李四")
{
name = "匿名";
}
else
{
name = value;
}
}
}
private char sex;
public char Sex
{
get { return sex; }
set {
if (value != '男' && value != '女')
{
sex = '男';
}
else
{
sex = value;
}
}
}
private double chinese;
public double Chinese
{
get { return chinese; }
set
{
if (value < 0 || value > 100)
{
chinese = 0;
}
else
{
chinese = value;
}
}
}
private double english;
public double English
{
get { return english; }
set { english = value; }
}
public void sayhello()
{
Console.WriteLine("我的名字叫{0},今年{1}岁了,是位{2}生!",name ,age ,sex );
}
public void score()
{
Console.WriteLine("我的语文成绩为{0},数学成绩为{1},英语成绩为{2},平均成绩为{3}!",chinese ,match ,english ,(chinese +match +english )/3);
}
}
}
static void Main(string[] args)
{
student stu2 = new student("李四", '女', -78, 90);
stu.sayhello();
stu.score();
stu2.sayhello();
stu2.score();
Console.ReadKey();
}
为什么-78输出时没有变为0? 展开
1个回答
展开全部
你把构造函数里的这句话:
this.chinese = chinese;
改为:
this.Chinese = chinese;
只改了一个字母,原因在于没改前,你是对成员变量在赋值,根本没执行到Chinese属性的set访问器。
this.chinese = chinese;
改为:
this.Chinese = chinese;
只改了一个字母,原因在于没改前,你是对成员变量在赋值,根本没执行到Chinese属性的set访问器。
追问
谢谢了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询