快捷设置字段的访问方式,只读or只写or可读可写,就可以使用到属性,或称属性访问器。
private string name;
public string Name
{
get
{return name;
}
set{
name = value;
}
}
*首先要有一个字段*
*get访问器用于读取字段,若属性内没有get访问器,则被认为是不可读的。*
*set访问器用于写入字段,若属性内没有set访问器,则被认为是不可写的。*
value被认为是写入的数值
如:Name="小明"
只读
public string Name
{
get
{
return Name;
}
}
只写
public string Name
{
set
{
name = value;
}
}
可读可写
public string Name
{
get
{
return Name;
}
set
{
name = value;
}
}
当然,也可使用private标记外界是否可以访问,从而设置为外界只读,只写.后期也可以使用其他属性访问修饰符,如protected
外界只读
public string Name{
get{
return Name;
}
private set{
name =value;
}
}
外界只写
public string Name
{
private get{
return Name;
}
set {
name=value;
}
}
public int Age{get;get;}
这种写法可以先前不定义字段,因为编译器会自动帮你生成一个字段,例如:_age
练习 定义人类的字段和属性,设置属性的访问级别(只读、只写、可读可写) 姓名 年龄 爱好 银行卡密码 女朋友(男朋友)的数量
public string Name
{
get{
return Name;
}
set{
name = value;
}
}
public string GirlFriend
{
private get{
return Name;
}
set{
name = value;
}
}
按值传递参数
public void Swap(int x,int y){
int temp = x;
x=y;
y=temp;
}
MathTool myMath = new MathTool();
int score_1 = 60;
int score_2 = 100;
myMath.Swap(score_1, score_2);
Console.WriteLine(score_1);
Console.WriteLine(score_2);
结果为60,100
形参改变了数值,实参并没有改变数值
赋值后在栈中存储了一个新内存,与**引用**的区别
MathTool myMath = new MathTool();
int score_1=60;
int score_2=100;
myMath.Swap(score_1,score_2);
按引用传递参数
public void Swap(ref int x,ref int y)
{
int temp = x;
x= y;
y=temp;
}
---
MathTool myMath = new MathTool();
int score_1=60;
int score_2=100;
my.Math.Swap(ref score_1,ref score_2);
注意:引用参数的实参必须是已经初始化的
按输出传递参数
public void getValues(out int x,out int y)
{
Console.WriteLine("请输入第一个值: ");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个值: ");
y = Convert.ToInt32(Console.ReadLine());
}
}
int a = 3;
int b = 1;
my.Math.getValues(out a, out b);
注意:输出参数的形参必须是已经赋过值的
public void Sum(params int[] a)
{
int result =0;
foreach(int x in a)
{
result +=x;
}
Console.WriteLine("结果为:"+result);
}
myMath.Sum(1,2,3,4,5,);
注意:数组参数只能使用一次,而且要放到参数列表的最后。
public int GetYourGrade(int yourAge, int jumpCount = 0)
{
int grade = yourAge - 6 + jumpCount;
grade = grade > 0 && grade < 13 ? grade : -1;
return grade;
}
注意:数组参数只能使用一次,而且要放到参数列表的最后。
练习 1.定义三个整型变量a,b,c要求将最大值赋值给a,中间值赋值给b,最小值赋值给c(要求使用ref)
static void Test4(ref int a,ref int b,ref int c)
{
int max = 0;
int mid = 0;
int min = 0;
if (a>b &&a>c)
{
max = a;
mid = b > c ? b : c;
min = c > b ? b : c;
}
if (b > a && b > c)
{
max = b;
mid = a > c ? a : c;
min = c > a ? a : c;
}
if (c > b && c > a)
{
max = c;
mid = a > b ? a : b;
min = a > b ? b : a;
}
a= max ;
b=mid ;
c= min;
}
int a = 3;
int b = 1;
int c = 5;
Test4(ref a, ref b, ref c);
Console.WriteLine(a + "," + b + "," + c);
2.用一个方法同时计算矩形的面积和体积,并同时返回它的面积和体积(要求使用out)
static void Test2(int a, int b, int c, out int area, out int volume)
{
area = a * b;
volume = a * b * c;
}
int a = 3;
int b = 1;
int c = 5;
int area, volume;
Test2(a, b, c, out area, out volume);
Console.WriteLine(area + "," + volume);
3.写一个方法,可以将数组按a[0]-a[1]+a[2]-a[3]+a[4]……的格式返回结果,在Main方法中调用该方法。(参数要求用params修饰)
static int Test5(params int[] a)
{
int num = 0;
for (int i = 0; i < a.Length; i++)
{
if (i%2==0)
{
num += a[i];
}
else
{
num -= a[i];
}
} return num;
}
int num = Test5(1,3,2,4,2);
Console.WriteLine(num);
全局变量、局部变量、就近原则 局部变量访问之前一定赋初值。
总结 1.属性 2.方法参数 3.数据类型整合