测试
曾英-C#教学-32 this关键字\数组在方法和属性中的传递
this关键字\数组在方法和属性中的传递
目录
- 理解this关键字的用法
- 掌握数组在方法中的传递过程
- 掌握数组在属性中的传递过程
理解this关键字的用法
-
this的一种用法:当类中属性与定义的变量重名的时候用thisi关键字
-
this的含义:"对象的"的意思
this关键字的程序实例: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _32_this关键字 { class Program { static void Main(string[] args) { Time time1 = new Time(3, 32, 32); //这里的time1就是下面类中的"this." Console.WriteLine(time1.hour); Console.WriteLine(time1.minute); Console.WriteLine(time1.second); } } class Time { //为讲解方便,这里定义成public类型 public int hour; public int minute; public int second; public Time(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } } }
数组在方法中的传递过程
-
为索引器打下基础
-
这里比较基础
-
类中定义数组的方法:
public int[] Arr(int[] arr1)
-
传递过程:
1\cad.Arr(arr2)传递给类中的Arr(int[] arr1); 2\arr = arr1;传递给At类的参数public int[] 3\类中的 public arr = new int[9];获得arr1的参数 4\打印出arr数组中的参数
数组传递的程序实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _32_this关键字_数组在方法中的传递
{
class Program
{
//主函数进行数组的传递
//功能:arr2传递给arr数组
static void Main(string[] args)
{
At cad = new At();
int[] arr2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cad.Arr(arr2);//传递给arr
//打印arr数组的参数
Console.WriteLine(cad.arr[3]);//结果是4,arr2的数组已经传递给arr数组.
}
}
class At
{
public int[] arr = new int[9];
//方法的参数是一个数组,返回的值也是一个数组
public int[] Arr(int[] arr1)
{
arr = arr1;
return arr;
}
}
}
数组在属性中的传递过程
-
利用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _32_this关键字_数组在方法中的传递 { class Program { //主函数进行数组的传递 //功能:arr2传递给arr数组 static void Main(string[] args) { At cad = new At(); int[] arr2 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //1\arr2的 属性赋值给了Arr cad.Arr = arr2; Console.WriteLine(cad.arr[3]); } } class At { public int[] arr = new int[9]; //属性传递中,属性不存在返回值的问题,用get与set关键字 public int[] Arr //这里的定义与之前的数组定义不同 { set{arr = value; } } } }