机器视觉 第七章 C#

本文介绍了C#编程中方法的基础知识,包括入口方法Main函数的作用,方法的声明和调用,以及方法的优点——代码复用和封装性。文章详细阐述了方法的声明结构,如访问权限、返回值类型和参数。此外,还讨论了不同类型的参数传递,包括值参数、引用参数(ref)和输出参数(out),并提供了示例代码来说明它们的特点和使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.入口方法

入口方法(Main函数)程序的入口  它是一个特殊的函数(自动执行)入口函数中的代码

作用
    方法是包含一系列语句的代码块。方法必须在类或结构中声明

优点:
减少代码重复率

方法体现了c#语言的封装性

方法的分为 声明方法 和调用方法


声明方法基本结构:
    访问权限  返回值类型   代表方法 是否具有 return 数据的类型

       void  和 其他明确的数据类型 (int  float 。。 string。。。)
    方法名称     大驼峰   见名之意
    方法参数    实参(调用方法) 和形参(声明方法)
    括号{     

     语句块

    }
   调用方法:
     在哪调用(在方法内部调用    Main 入口方法  程序运行时 自动执行   Main方法是静态方法   自定义静态方法直接使用, 自定义非静态方法 需要对象打点调用)

  

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

namespace ZhiYou_Day8_1
{
         
    internal class Program
    {
        
        //入口方法(Main函数)程序的入口   它是一个特殊的函数( 自动执行)入口函数中的代码
        static void Main(string[] args)
        {
            Console.WriteLine("11");
            //方法的调用
            //1.方法调用要放在方法内部
            //2.可以通过入口函数 调用其他自定义方法
            //3.如果在其他自定义的函数中调用 不能执行
            //4.方法可以被重复调用
            Mothod_1();
            Mothod_1();
            for (int i = 0; i < 5; i++) {
                Mothod_1();
            }
            Mothod2();


            //调用其他类的私有方法 不能直接调用
            //ClassTest.ClassTestMothod();
            // ClassTest.ClassTestMothod1();

            //调用其他类的共有方法  可以直接调用
            //ClassTest.ClassTestMothod2();
            //ClassTest.ClassTestMothod3();

            //调用其他类的共有静态方法 必须要标识类的名字
            ClassTest.ClassTestMothod9();
            //调用其他类的共有非静态方法 必须要使用其他类对象的名字
            ClassTest classTest = new ClassTest(); 
            classTest.ClassTestMothod8();
            Console.ReadKey();

        }

        public void ProgramMothod() { 
        
            //非静态方法调用其他类的静态方法
            ClassTest.ClassTestMothod9 ();
            //非静态方法调用其他类的非静态方法
            ClassTest classTest = new ClassTest();
            classTest.ClassTestMothod8 ();

        }
        //C# 方法(函数)
        //作用:
        // 方法是包含一系列语句的代码块。方法必须在类或结构中声明
        // 优点:
        //减少代码重复率
        //方法体现了c#语言的封装性
        //if for  whlie .... 等于语句结构 都要定义在方法中
        //可以在类中自定义方法
        //自定义方法 分为声明方法 和调用方法

        //定义方法的格式: 访问修饰符 静态或非静态 返回值类型  方法的名字 (形式参数){ 封装的代码 }
        private static void Mothod_1 ()
        {
            //封装的代码
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("1");
            }
            
        }

        private static void Mothod2() { 
            Mothod_1 ();
        }

       

    }


    public class ClassTest {

        // 访问修饰符 
        //只在本类当中调用 private

        private static void ClassTestMothod() {
            Console.WriteLine("ClassTestMothod");
            //本类私有方法调用
            ClassTestMothod1();
        }
          static void ClassTestMothod1()
        {

            Console.WriteLine("ClassTestMothod1");
            //本类私有方法调用
            ClassTestMothod();
        }
        //共有的方法
        public static void ClassTestMothod2()
        {

            Console.WriteLine("ClassTestMothod2");
            ClassTestMothod3 ();
            ClassTestMothod();
        }
        public static void ClassTestMothod3()
        {

            Console.WriteLine("ClassTestMothod3");
            ClassTestMothod2 ();
            ClassTestMothod1();
        }


        //从静态和非静态定义方法
        //标识staitc的方法是静态方法  反之是非静态方法
        //区别1 :调用方式不一样
        //静态方法调用使用      类名.静态方法   
        //非静态方法调用使用    类的对象.非静态方法
        private static void ClassTestMothod4() {

            Console.WriteLine("ClassTestMothod4");
        }
        private  void ClassTestMothod5()
        {
            Console.WriteLine("ClassTestMothod5");

        }

        public static void ClassTestMothod6() {

            //在本类的静态方法中调用 静态方法  使用本类类名调用 也可以省略
            ClassTest.ClassTestMothod4();
            //在本类的静态方法中调用 非静态方法 必须要定义本类的对象 来调用方法
            ClassTest classTest  =   new ClassTest();
            classTest.ClassTestMothod5();
        }

        public void ClassTestMothod7()
        {
            //在本类的非静态方法中调用 静态方法 使用本类类名调用 也可以省略
            ClassTestMothod4();
            //在本类的非静态方法中调用 非静态方法 使用this调用 也可以省略
            this.ClassTestMothod5();
        }


        public void ClassTestMothod8()
        {
            Console.WriteLine("ClassTestMothod8");
        }
        public static void ClassTestMothod9()
        {
            Console.WriteLine("ClassTestMothod9");

        }
    }
}

  方法的分类:

   返回值类型分类 

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

namespace ZhiYou8_2
{
    internal class Program
    {
        static void Main(string[] args)
        {

            ClassTest.ClassTestMothod();
            string str   = ClassTest.ClassTestMothod1();
            string c = "456";
            Console.WriteLine(str+c);
            //  Console.WriteLine(c); 
            int intNumber =ClassTest.ClassTestMothod2();
            Console.WriteLine(intNumber);
            long ab =  ClassTest.AddMothod(10,20);
            Console.WriteLine(ab);
            Console.ReadKey();
        }
    }

    public class ClassTest {
        // 返回值类型分类 
        //无返回值类型 和有返回值类型

        //返回值 代表 当前方法的结果的一个出口
        // 无返回值类型 代表方法的结果 没有出口
        //返回值类型  代表方法的结果的一个出口  所有的类型 都可以做为返回值类型
        public static void ClassTestMothod() 
        {
            string a = "abc";
            string b = "123";
            Console.WriteLine(a + b);
        }
        public static string ClassTestMothod1()
        {
            string a = "abc";
            string b = "123";
            //1.return 在方法中使用
            //2.return 跳出方法
            //3.retrun 当前方法结果的出口
            //4 retrun的结果 要和返回值类型保持一致
            return a+b;
            Console.WriteLine("111");
        }
        public static int ClassTestMothod2()
        {
            int a = 123;
            int b = 456;
            //1.return 在方法中使用
            //2.return 跳出方法
            //3.retrun 当前方法结果的出口
            //4 retrun的结果 要和返回值类型保持一致
            return a + b;
        }
        //ClassTest 也是一种数据类型 
        public static ClassTest ClassTestMothod3()
        {
            return new ClassTest { };
        }


        public static long AddMothod(long a, long b) {

            if (a + b > 50)
            {
                return 50;
            }
            else
            {
                return a + b;
            }
           
        }
    }
}

  参数传递 形式:
 

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

namespace ZhiYouDay8_3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ClassTest.ClassTestMothod();
            ClassTest.ClassTestMothod1();
            //
            ClassTest.ClassTestMothod2(8,9);
            string c =  ClassTest.ClassTestMothod3("abc", "abc");
            Console.WriteLine(c);
        }
    }

    public class ClassTest {

        //()无参方法
        //参数分为形式参数(形参) 和实际参数(实参)
        //定义方法中的参数是形参   调用方法中的参数实参
        //形参的个数是无限个  形参的类型 可以是任意类型   和返回值类型一致
        public static void ClassTestMothod() { 
        
             int a = 1;
             int b = 2;
            Console.WriteLine(a+b);
        }
        public static void ClassTestMothod1()
        {
            int a = 3;
            int b = 4;
            Console.WriteLine(a + b);
        }
        //形参 就是定义一个变量
        public static void ClassTestMothod2(int a,int b)
        {
            Console.WriteLine(a+b);
        }

        //
        public static string ClassTestMothod3(string a, string b) {

            return a + b;
        }
    }
}

 

1.值参数:

   特点:

  1.参数传递的默认方式 

  2.当调用一个方法时,会为每个值参数创建一个新的存储位置。

  3.当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全

  总结:无论参数的类型是值类型还是引用类型  都遵循值传递的特性

2.引用参数 

   特点:

   1.引用参数是一个对变量的内存位置的引用 不会创建一个新的存储位置

   2. 参数关键字 ref

2.输出参数

  特点:

1. 是对于方法返回值的补充。return 语句可用于函数中返回一个值  输出参数可以返回多个值

2.关键字 out    out输出参数在方法中 必须被使用,且和retrun保持一致

3. 其他方面与引用参数相似

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZhiYouDay8_4
{
    internal class Program
    {
        static void Main(string[] args)
        {

            //  参数按照传递方法分类:

            //   1. 值参数传递:
            //  (1).参数传递的默认方式
            //  (2).当调用一个方法时,会为每个值参数创建一个新的存储位置
            //  (3).当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全

            int w = 10;
            int h = 20;
            ProgramMothod(w, h);
            Console.WriteLine(w);
            Console.WriteLine(h);

            int c = 50;
            int d = 100;
            ProgramMothod1(ref c, ref d);
            Console.WriteLine(c);
            Console.WriteLine(d);

            string f;
            int intNumber1 = AddMothod(1, 2,out f);
            Console.WriteLine(intNumber1);
            Console.WriteLine(f);

            Console.ReadKey();
            //   2. 引用参数传递
            //特点:
            //  1.引用参数是一个对变量的内存位置的引用 不会创建一个新的存储位置
            //  2.参数关键字 ref


            //   3.输出参数传递  让方法具有多个返回值结果的参数
            // 参数关键字 out
        }

        public static void ProgramMothod(int a ,int b) {

              int intNumber;
              intNumber = a;
              a = b;
              b = intNumber;
            Console.WriteLine(a);
            Console.WriteLine(b);
        }


        public static void ProgramMothod1(ref int a,ref int b)
        {

            int intNumber;
            intNumber = a;
            a = b;
            b = intNumber;
            Console.WriteLine(a);
            Console.WriteLine(b);
        }


        public static int AddMothod(int a,int b,out string x) { 
        
            x = a.ToString()+b.ToString();
            int v = a + b;
            return v;
        
        }   
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值