C#Lesson07 属性,引用数据类型和方法参数.

主函数

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类

using System;
namespace Lesson07_1
{
public class MyMath
{
//交换两个数
public void Swap(ref int num1,ref int num2){
//num1,num2叫做形式参数(形参)
int t = num1;
num1 = num2;
num2 = t;
}


//计算两个整数的和,积
//out 来修饰形参
public void AddAndDot(int num1,int num2,out int sum,out int dot){
sum = num1 + num2;
dot = num1 * num2;
}


//计算n位整数的和
//params 关键字
public int Add(params int[] arr){
int sum = 0;
for (int i = 0; i < arr.Length; i++) {
sum += arr [i];
}
return sum;
}


public void Fuzhi(ref int a, ref int b,ref int c){
if (a < b) {
a = b;
}
if (a < c) {
a = c;
}
if (c > a) {
c = a;
}
if (c > b) {
c = b;
}
}


public void SAndV(int a,int b,int c,out int S,out int V){
S = (a * b + a * c + b * c) * 2;
V = a * b * c;
}
public int AddAndSubtract(params int[] a){
int sum = a[0];
for (int i = 1; i < a.Length; i++) {
if (i % 2 == 0) {
sum += a [i];
} else {
sum -= a [i];
}
}
return sum;
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值