您好,欢迎来到五一七教育网。
搜索
您的当前位置:首页C#方法的定义、调用与调试(下)

C#方法的定义、调用与调试(下)

来源:五一七教育网
构造器(一种特殊的方法)

1. 构造器(constructor)是类型的成员之一

2. 狭义的构造器指的是“实例构造器”(instance constructor) 3. 如何调用构造器

4. 声明构造器:ctor+tab+tab :快速声明构造器 5. 构造器的内存原理 using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; namespace ConstructorExample {

class Program {

static void Main(string[] args) {

Student stu = new Student(2, \"Mr okay\");//调用构造器 Console.WriteLine(stu.ID); Console.WriteLine(stu.Name);

Console.WriteLine(\"===========\"); Student stu2 = new Student(); Console.WriteLine(stu2.ID); Console.WriteLine(stu2.Name);

} }

class Student //没有返回值类型 {

public Student() { }

public Student(int intid, string initName) {

this.ID = intid;

this.Name = initName; }

public Student() {

this.ID = 1;

this.Name = \"No1\"; }

public int ID;

public String Name; } }

方法的重载(Overload)

1. 调用重载方法的实例 2. 声明带有重载的方法

1. 方法签名(method signature)由方法的名称、类型形参的个数和它的每一个形参(按从左到右的顺序)的类型和种类(值、引用或输出)组成。方法签名不包含返回类型。

2. 实例构造函数签名由它的每一个形参(按从左到右的顺序)的类型和种类(值、引用或输出)组成

3. 重载决策(到底调用哪一个重载):用于在给定了参数列表和一组候选函数成员的情况下,选择一个最佳函数成员来实施调用。 using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; namespace OverloadExample {

class Program {

static void Main(string[] args) { } }

class Calculator {

public int Add(int a, int b) {

return a + b; }

public int Add(out int a, int b) {

a = 100;

return a + b; } } }

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks; namespace OverloadExample {

class Program {

static void Main(string[] args) {

Calculator c = new Calculator();

double x = c.Add(100D, 200D); Console.WriteLine(x); } }

class Calculator {

public int Add(int a, int b) {

return a + b; }

public int Add(int a, int b, int c) {

return a + b + c; }

public double Add(double x, double y) {

return x + y; } } }

如何对方法进行debug

1. 设置断点(breakpoint)

2. 观察方法调用时的call stack

3. Step-in, Step-over, Step-out 4. 观察局部变量的值与变化 using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using System.Threading.Tasks;

namespace CSharpMethodExample1 {

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 = GetCylinderVolume(r, h); return cv / 3; }

//自顶向下,逐步求精 } }

方法的调用与栈*

1. 方法调用时栈内存的分配

1. 对stack frame的分析

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务