主函数
using System;
namespace Lesson07_1
{
class MainClass
{
public static void Main (string[] args)
{
// Cat c1 = new Cat ();
c1.name = "喵老大";
c1.phoneNumber = "13858705019";
// Console.WriteLine (c1.Name);//调用get方法
// c1.Name = "喵喵";//调用set方法
// Cat c1 = new Cat ();
// c1.PhoneNumber = "123456";
// Console.WriteLine (c1.PhoneNumber);
// Cat c1 = new Cat ();
// string[] i = {"1", "20", "30", "40" };
// c1.Toys = i;
// string[] i1 = {"1", "20", "30", "40" };
// c1.Toys = i1;
// int a = 24;
// int b = 345;
// int c = 44;
// MyMath math = new MyMath ();
// math.Swap (ref a,ref b);//传进去的a,b叫做实际参数(实参)
// //ref 在传递参数前一定要有初始值
Console.WriteLine (a);
Console.WriteLine (b);
//
// int sum, dot;
// math.AddAndDot (a, b, out sum, out dot);
// Console.WriteLine (sum);
// Console.WriteLine (dot);
//
// Console.WriteLine (math.Add (1,2,3,4));
// math.Fuzhi (ref a,ref b,ref c);
// int S, V;
// math.SAndV(a,b,c,out S,out V);// Console.WriteLine (S);
// Console.WriteLine (math.AddAndSubtract (1, 2, 3, 4, 5, 6, 7, 8));
}
}
}
-----------------------------------------------------------------------------------------------------------------
Cat类
using System;
namespace Lesson07_1
{
public class Cat
{
//字段
private string name;
private string phoneNumber;
private string[] toys;
//方法(函数)
public void SetName(string name){
//通过方法对字段进行赋值
this.name = name;
}
//属性
/*
* 1.set,get访问器
* 2.在属性中,只能对一个访问器添加访问修饰符
* 3.我们可以在调用set,get访问器的时候,进行暗箱操作
* 4.get,set访问器可以只要一个
*/
// public string Name{
// get{
// return name;
// }
set{
//value是set访问器的隐藏参数,value等于调用者所赋的值
name = value;
}
// }
public string PhoneNumber{
get{
return phoneNumber;
}
set{
if (value.Length == 11) {
phoneNumber = value;
} else {
Console.WriteLine ("你输入的号码错误");
}
}
}
//属性简写,C#内部会帮我们构建一个叫_friend字段,用来取值和赋值
public string Friend {
get;
set;
}
public string[] Toys {
get{
return toys;
}
set{
//在第一次赋值的时候,初始化数组
Console.WriteLine ("000");
if (toys == null) {
Console.WriteLine ("1");
toys = new string[value.Length];//懒价值,需要的时候再初始化数组
}
for (int i = 0; i < value.Length; i++) {
toys [i] = value [i];
}
}
}
}
}
---------------------------------------------------------------------------------------------------------------------
Math类