C#---属性、方法参数

【千锋合集】史上最全Unity3D全套教程|匠心之作_哔哩哔哩_bilibili

引用类型变量的初始为空(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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值