• c#学习0216


    2017-03-02

    out  关键字指定所给的参数为一个输出参数 该参数的值将返回给函数调用中使用的变量

    注意事项 1未赋值的变量用作ref参数是非法的,但是可以把未赋值的变量用作out参数

    2 在函数使用out参数时,必须把它看成尚未赋值

    例如:

    static void Main(string[] args)
    {
    //int myNunber = 5;
    //Console.WriteLine("mynumber is {0}",myNunber);
    //DoubleValue(ref myNunber);
    //Console.WriteLine(myNunber);

    int[] myArray = { 1,4,2,5,6,7,8,0,9,3};
    int maxIndex;
    Console.WriteLine(MaxValue(myArray,out maxIndex));
    Console.WriteLine($"{maxIndex+1}");

    Console.ReadKey();
    }

    public static int MaxValue(int[] myArray, out int maxIndex)
    {
    int maxValue = myArray[0];
    maxIndex = 0;
    for (int i = 0; i < myArray.Length; i++)
    {
    if (myArray[i] > maxValue)
    {
    maxValue = myArray[i];
    maxIndex = i;
    }
    }
    return maxValue;
    }

    ref 引用传参 函数处理的变量和函数调用中使用的变量相同,而不仅仅是值相同的变量。对这个变量做的任何的改变都会影响用作参数的变量值 用ref关键字指定参数

    注意 两个限制 1 必须在函数调用中使用"非常量的变量"  2必须使用初始化过的变量   const 定义该变量始终为常量 限制一个变量被改变,只读变量 在定义的时候初始化,之后不能被更改

    例如:

    namespace Ref和Out
    {
    class Program
    {
    static void Main(string[] args)
    {
    int myNunber = 5;
    Console.WriteLine("mynumber is {0}",myNunber); //5
    DoubleValue(ref myNunber);//10
    Console.WriteLine(myNunber);//10
    Console.ReadKey();

    }
    public static void DoubleValue(ref int val)
    {
    val =val* 2;
    Console.WriteLine($"the value is {val}");
    }

    }
    }

     Params

    当需要传递多个参数或者参数的个数不确定的时候,就可以使用params类型的参数。例如

    namespace Params
    {
    class Program
    {
    static void Main(string[] args)
    {
    UserParams(1,2,3);
    UserParamsTwo("1","test",true);
    int[] myArray = { 1, 2, 3, 4, 5 };
    UserParams(myArray);
    Console.ReadKey();

    }

    public static void UserParams(params int[] list)
    {
    for (int i = 0; i < list.Length; i++)
    {
    Console.WriteLine(list[i]);
    }
    Console.WriteLine();
    }
    public static void UserParamsTwo(params object[] list)
    {
    foreach (var i in list)
    {
    Console.WriteLine(i);
    }
    Console.WriteLine();
    }
    }

    }

    1 构造函数

    作用是初始化对象,对对象的属性依次的赋值 没有返回值 没有void 构造函数的名称必须和类名一致 构造函数也可以实现重载  

    创建对象的时候调用构造函数 每个类都会有一个默认的无参数的构造函数 当显示的声明了一个构造函数后,默认的无参数的构造函数就被干掉了

    2 重载

    函数名称相同,参数的个数或者类型不同

    3 this

    1代表当前类的对象 2 显示的调用当前类的构造函数

    4 构造函数调用构造函数

    public Student(string name,int age,char gender,int chinese,int math,int english)
    {
    this.Name = name;
    this.Age = age;
    this.Gender = gender;
    this.Chinese = chinese;
    this.Math = math;
    this.English = english;
    }

    :this

    public Student(string name, int age, char gender):this(name,age,gender,0,0,0)
    {
    //this.Name = name;
    //this.Age = age;
    //this.Gender = gender;
    }

    5 属性 property  字段 fileds  方法 method

    作用是保护字段 对字段的取值和赋值做限制 其本质就是一个get方法 一个set方法

     6 析构函数

    程序执行结束的时候 执行析构函数 现在c#中有一个GC垃圾回收机制

    7 继承  基类/父类 派生类/子类

    子类隐式的获得父类的除构造函数和析构函数以为的所有成员 ???

    子类只能有一个直接父类 所以c#不能实现多重继承  而父类可以有多个直接子类 所以继承是可以传递的;

    如果class b 派生出 class c ; class a 派生出 class b 那么 class c会继承 class a 和class b的声明的成员;

    可以实现某个类禁止被其他类继承吗?  c#提供了一个 sealed修饰符  会阻止其他类继承改基类

    //父类 person

    public class person

    {

     //字段

    private string _name;

    private int _age;

    private char _gender;

    //属性

    public string Name {get;set;}

    public int Age {get;set;}

    pubic char Gender {get;set;}

    //构造函数

    public person(string name,int age,char gender)

    {

      this.Name=name;this.Age=age;this.Gender=gender;

    }

    //自定义函数

    public void SayHello()

    {

      console.writeline("我是{0},我今年{1}岁了,我是{2}生",this.Name,this.Age,this.Gender);

    }

    }

    //第一个子类 reporter 记者类  继承父类 person

    public class repoter:person

    {

    //字段

    private string _hobby;

    //属性

    public string Hobby{get;set;}

    //构造函数 继承父类的

    public reporter(string name,int age,char gender,string hobby):base(name,age,gender)

    {

      this.Hobby=hobby;

    }

    //自定义函数

    public void reporterSayHello()

    {

      console.writeline("我是{0},我今年{1}岁了,我是{2}生,我的爱好是{3}",this.Name,this.Age,this.Gender,this.Hobby);

    }

    }

    //第二个子类 programmer 继承父类person

    public class programmer:person

    {

    //字段

    private int _iWorkYear;

    //属性

    public int iWrokYear{get;set;}

    //构造函数 继承父类

    pulic programmer(string name,int age,char gender,int iWorkYear):base(name,age,gender)

    {

      this.iWrokYear=iWorkYear;

    }

    //自定义函数 

    public void programmerSayHello()

    {

      console.writeline("我是{0},我今年{1}岁了,我是{2}生,我的工作年限是{3}",this.Name,this.Age,this.Gneder,this.iWorkYear);

    }

    }

  • 相关阅读:
    【GruntMate】一个让你更方便使用Grunt的工具
    HTML5小游戏【是男人就下一百层】UI美化版
    【Grunt】关于Grunt可视化的尝试
    在腾讯ubuntu云服务器上面部署asp.net core 2.1网站
    存储过程中执行动态Sql语句
    我的2016年总结
    程序员的成长阶梯和级别定义
    让IE8在win7下面能显示使用window.showmodaldialog弹出窗口的地址状态栏
    更改计算机名称后 导致 sql server 2008 R2 用windows账户不能附加的错误解决办法
    【转】通过js获取系统版本以及浏览器版本
  • 原文地址:https://www.cnblogs.com/yachao1120/p/6407837.html
Copyright © 2020-2023  润新知