引用类型变量的初始为空(null)
声明一个装备数组,并初始化
Equip[] heroEquip= new Equip[6];
由于装备对象还没有实例化(分配内存),所以初值都为null
如何给数组中的装备实例化?
public void LoadEquip(Equip equip) {
//检测装备栏是否有空余位置
for (int i = 0; i < heroEquips.Length; i++) {
//实例化
heroEquips[i] = new Equip();
//打印数组中的元素是否是null
Console.WriteLine(heroEquips[i] == null);
}
}
属性
属性命名方式使用大驼峰
using System;
namespace cProperties
{
class Person {
private string name;
public string Name
{
get { return name; }
set { name = value; }}
private int level;
public int Level
{
get { return level; }
set { //设置等级
level = value;
//等级提升后的影响,在属性访问器内写代码,不要再去读写该属性,如Level--,形成递归
}
}
//private string name;//Field
//public void SetName(string name) {
// this.name = name;
//}
//public string GetName() {
// return name;
//}
}
class Program
{
static void Main(string[] args)
{
Person xiaogang = new Person();
//执行属性访问器中的set方法
//"小刚刚"就是value
xiaogang.Name = "小刚";
string littleName = xiaogang.Name;
Console.WriteLine("xiaogang.name:" + xiaogang.Name);
xiaogang.Level++;
}
}
}
引用参数ref
using System;
namespace dMathParameters
{
class Person {
public int age = 5;
}
class MathTool {
public void Swap(int x, int y) {
int temp = x;
x = y;
y = temp;
Console.WriteLine(x + "|" + y);
}
public void PersonSwap(Person p1, Person p2) {
int temp = p1.age;
p1.age = p2.age;
p2.age = temp;
}
}
class Program
{
static void Main(string[] args)
{
MathTool ml = new MathTool();
int num01 = 60;
int num02 = 100;
ml.Swap(num01, num02);
//分析:调用方法时,实参赋值给形参
Console.WriteLine(num01+"|"+num02);
Person xiaoming = new Person();
xiaoming.age = 18;
Person huangxiaoming = new Person();
huangxiaoming.age = 40;
ml.PersonSwap(xiaoming, huangxiaoming);
Console.WriteLine(huangxiaoming.age);
}
}
}
如何使值类型达到引用类型参数的效果
引用参数ref
添加了ref关键词的参数,传递的就是地址,使用ref 参数的实参一定时赋过初值的,所以ref一般加在值类型参数前面
using System;
namespace dMathParameters
{
class Person {
public int age = 5;
}
class MathTool {
public void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
Console.WriteLine(x + "|" + y);
}
public void PersonSwap(Person p1, Person p2) {
int temp = p1.age;
p1.age = p2.age;
p2.age = temp;
}
}
class Program
{
static void Main(string[] args)
{
MathTool ml = new MathTool();
int num01 = 60;
int num02 = 100;
ml.Swap(ref num01, ref num02);
//分析:调用方法时,实参赋值给形参
Console.WriteLine(num01+"|"+num02);
Person xiaoming = new Person();
xiaoming.age = 18;
Person huangxiaoming = new Person();
huangxiaoming.age = 40;
ml.PersonSwap(xiaoming, huangxiaoming);
Console.WriteLine(huangxiaoming.age);
}
}
}
输出参数out
添加了out关键词的参数
参数就成了一个输出通道,离开方法之前形参必须赋值
实参必须时一个变量
传递的实参一般时值类型
使用输出参数,无论是形参还是实参前面都要加out关键词
using System;
namespace dMathParameters
{
class Person {
public int age = 5;
}
class MathTool {
public void Swap(ref int x, ref int y) {
int temp = x;
x = y;
y = temp;
Console.WriteLine(x + "|" + y);
}
public void PersonSwap(Person p1, Person p2) {
int temp = p1.age;
p1.age = p2.age;
p2.age = temp;
}
public void Cal(int a, int b, out int r1, out int r2)
{
r1 = a + b;
r2 = a - b;
}
}
class Program
{
static void Main(string[] args)
{
MathTool ml = new MathTool();
int num01 = 60;
int num02 = 100;
ml.Swap(ref num01, ref num02);
//分析:调用方法时,实参赋值给形参
Console.WriteLine(num01+"|"+num02);
Person xiaoming = new Person();
xiaoming.age = 18;
Person huangxiaoming = new Person();
huangxiaoming.age = 40;
ml.PersonSwap(xiaoming, huangxiaoming);
Console.WriteLine(huangxiaoming.age);
MathTool mal = new MathTool();
mal.Cal(3, 5,out int m,out int n);
Console.WriteLine(m + "|" + n);
}
}
}