C#基础 第七天
//class Person
//{
//利用字段快速生成属性:字段右键,快速操作和重构,选第一个。
//private string name { get; set; }
//private int age { get; set; }
////这样最简单,字段都省了
//public double Score { get; set; }
//public string Name { get => name; set => name = value; }
//public int Age { get => age; set => age = value; }
//public void SetName(string n)
//{
// name = n;
//}
//public string GetName()
//{
// return name;
//}
//属性
//public string Name
//{
// get
// {
// return name;
// }
// set
// {
// name = value;
// }
//}
//public int Age
//{
// get
// {
// return age;
// }
//设置只读属性,set前面加private或者去掉set
//set
//{
// age = value;
//}
//}
class Tool
{
//ref引用参数
public void ChangeInt(ref int x,ref int y)
{
int c = x;
x = y;
y = c; Console.WriteLine("x={0},y={1}",x,y);
}
public void Sort(int[]arr)
{
for (int i = 0; i < arr.Length-1; i++)
{
for (int j = i+1; j < arr.Length; j++)
{
if (arr[i]>arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public double Calculate(double x,double y,out double m,out double g)
{
double s = x + y;
m = x - y;
g = x * y;
return s;
}
//params参数称为可变数组参数,在传参的时候,既可以传递一个数组进来,也可以直接传递数组里面的元素.在形参列表里面,只能有一个params修饰的参数,并且只能放在最后一位,而且必须是一维数组类型
public int Sum(int a,params int[]arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
}
//}
class Program
{
static void Main(string[] args)
{
//Person s = new Person();
// s.SetName("zhangsan");
//string p= s.GetName();
//s.Age = 21;
//Console.WriteLine(s.Age);
//值类型的数据在赋值是拷贝赋值,当把实参赋值给形参后,形参的值发生改变,实参的值不会发生改变
Tool t = new Tool();
int a = 10, b = 20;
//t.ChangeInt(a,b);
//Console.WriteLine("a={0},b={1}",a,b);
//引用类型在赋值时,两个变量引用同一块内存
int[] arr = { 6,8,3,2,9,10};
int[] brr = arr;
brr[0] = 7;
t.Sort(arr);
foreach (var item in arr)
{
Console.WriteLine(item);
}
Console.WriteLine(arr[0]);//7
//ref引用参数:用在值类型的参数前面,调用方法时,实参前面也要加ref关键字,当形参发生改变后,实参也会改变,实参要用初始值;
t.ChangeInt(ref a,ref b);
Console.WriteLine("a={0},b={1}",a,b);
//out输出参数,当方法体里面的代码运行完以后,程序跳回到主程序的时候会把out修饰的形参向外赋值给实参,调用方法时,实参前面也要有out关键字。如果一个方法得到多个返回值,可以使用out参数
double f,g;
double d = t.Calculate(23.5,56.2,out f,out g);
Console.WriteLine("d={0},f={1}",d,f);
int[] o = { 1,4,6,3,8};
int[] i = { 3,6,9};
int sum = t.Sum(3,6,9);
Console.WriteLine(sum);
string str = "hello world/bei%jing";
char[] ch = {' ','/','%' };
string[] sts=str.Split(ch);
foreach (var item in sts)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}