《C#入门详解》刘老师 方法的定义、调用与调试009(构造器,方法重载,方法调用栈原理)

方法的定义、调用与调试

一、构造器

静态构造器

不带参数的构造器

带参数的构造器

static void main(string[] args)
{
 student stu = new student (2,"MR.OKAY");//传参数满足自定义构造器的需求
 console.writeline(stu.id);
 console.writeline(stu.name);
}

class student
{
 public int id;
 public string name;
 public student ( int initid , string initname)
  { this.id      = intitid;
     this.name= initname;
   }
}

构造器的内存原理

二、方法的重载

声明带有重载的方法

  • 方法签名由方法的名称、类型形参的个数和它的每一个形参(按从左到右的顺序)的类型和种类(值、引用或输出)组成。方法签名不包含返回类型
  • 实例构造函数签名由它的形参(按从左到右的顺序)的类型和种类(值、引用或输出)组成
  • 重载决策(到底调用哪一个重载):用于在给定了参数列表和一组候选函数成员的情况下,选择一个最佳函数成员来实施调用
public int Add<T>(int a ,int b)
{ 
  T t;//t的变量做什么事情,是在方法内部中的事了
  return a+b ;   
 }
/*类型形参是用在泛型方法里的,未来会有一个类型T参与到我的方法中。类型形参是可以参与构成方法签名的*/

public int Add(int a, int b)           //什么修饰符都不加的参数都是传值的
public int Add(ref int a, int b)      //加上ref修饰的是传引用的
public int Add(out int a, int b)     //输出参数
/*参数的种类也参与构成方法的签名的*/

三、方法的调用与栈

方法调用时栈内存的分配(对stack frame的分析)

class program
{
  static void Main(string[] args)
  {
    double result =Calculator.GetConeVolume(100,100);
  }
}
class Calculator
{
 public static double GetCircleArea(double r)
{ return Math.Pi*r*r ;        }

public static double GetCylinderVolume(double r, double h)
{ double a=GetCircleArea(r);
   return a*h;   
}

public static double GetConeVolume(double r,double h)
{ double cv=GetCyclinderVolume(r,h);
   return cv/3;
}


}

方法调用时栈内存的分配(对stack frame的分析)

原文地址:https://www.cnblogs.com/zfcsharp/p/13709527.html