接口、委托、特性、反射、多播委托

本文深入探讨C#中的接口、委托和反射的概念与应用。接口定义了类需实现的方法,委托允许方法作为参数传递,反射则使程序能够自省其结构和行为。这些特性是C#高级编程不可或缺的一部分。

接口

用于与服务有关,只有方法声明,无方法体,接口不能放字段。

默认访问修饰符为public

继承了接口,必须实现所有的接口成员

接口也可以多重接口

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

namespace _01接口
{
    class Program:ICalc,IWork
    {
        static void Main(string[] args)
        {

        }

        public int add(int a, int b)
        {
            return a + b;

        }

        public int cheng(int a, int b)
        {
            return a * b;
        }

        public void Run()
        {
            throw new NotImplementedException();
        }

        public void Talk()
        {
            throw new NotImplementedException();
        }
    }
    class People : ICalc
    {
        public int add(int a, int b)
        {
            throw new NotImplementedException();
        }

        public int cheng(int a, int b)
        {
            throw new NotImplementedException();
        }

        public void Run()
        {
            throw new NotImplementedException();
        }

        public void Talk()
        {
            throw new NotImplementedException();
        }
    }
}

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

namespace _01接口
{
    interface IWork
    {
        void Run();
        void Talk();
    }
}

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

namespace _01接口
{
    interface ICalc:IWork
    {
        int add(int a, int b);
        int cheng(int a, int b);
    }
}

委托

将方法作为方法的参数代入另一个方法中,可以理解为一种数据类型,直接赋值给一个方法。

两种委托:

Action:可有可无参数,无返回值;

Function:必须有一个参数,有返回值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace _02委托
{
    class Program
    {
        //delegate string DelTalk();
        delegate int DelAdd(int a,int b);
        static void Main(string[] args)
        {
            //Console.WriteLine("进来一个人");
            Console.WriteLine("请进行加法运算");
            People p = new People();
            //SetName(p.Talk); 
            NumAdd(p.Add);
            Console.ReadLine();
        }
        //static void SetName(DelTalk talk)
        //{
        //    Console.WriteLine("这个人的名字是:"+talk());
        //}
        static void NumAdd(DelAdd add)
        {
            Console.WriteLine("这两个数之和是:"+add(2,3));
        }
    }
}

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

namespace _02委托
{
    class People
    {
        
        //public string Talk()
        //{
        //    return "张三";
        //}
        public int Add(int a,int b)
        {
            return a + b;
        }

        
    }
}

特性(Attribute)

特性是用于在运行时传递程序中各种元素的行为信息的声明性的标签。一个声明性标签是通过放置在它所应用的元素前面的方括号([]来描述的。

预定义特性:

AttributeUsage、Conditional、Obsolete(过时的)

Conditional代码:

#define DEBUG
using System;
using System.Diagnostics;
public class Myclass
{
    [Conditional("DEBUG")]
    public static void Message(string msg)
    {
        Console.WriteLine(msg);
    }
}
class Test
{
    static void function1()
    {
        Myclass.Message("In Function 1.");
        function2();
    }
    static void function2()
    {
        Myclass.Message("In Function 2.");
    }
    public static void Main()
    {
        Myclass.Message("In Main function.");
        function1();
        Console.ReadKey();
    }
}
//Obsolete(过时的)
using System;
public class MyClass
{
   [Obsolete("Don't use OldMethod, use NewMethod instead", true)]
   static void OldMethod()
   { 
      Console.WriteLine("It is the old method");
   }
   static void NewMethod()
   { 
      Console.WriteLine("It is the new method"); 
   }
   public static void Main()
   {
      OldMethod();
   }
}

反射(Reflection)

反射指程序可以访问、检测和修改它本身状态或行为的一种能力。

//实例类
class ReflectionClass
{
    public int id;

    private string name;
    /// <summary>
    /// 姓名
    /// </summary>
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private string age;
    /// <summary>
    /// 年龄
    /// </summary>
    public string Age
    {
        get { return age; }
        set { age = value; }
    }
    private string sex;
    /// <summary>
    /// 性别
    /// </summary>
    public string Sex
    {
        get { return sex; }
        set { sex = value; }
    }

    public ReflectionClass(string name, string age)
    {
        this.name = name;
        this.age = age;
    }
    public ReflectionClass(string sex)
    {
        this.sex = sex;
    }
    public ReflectionClass()
    { }

    public void Show()
    {
        Console.WriteLine("姓名:" + name + "\n" + "年龄:" + age + "\n" + "性别:" + sex);
    }
}

多播委托

每个委托都只包含一个方法调用,调用委托的次数与调用方法的次数相同。如果调用多个方法,就需要多次显示调用这个委托。当然委托也可以包含多个方法,这种委托称为多播委托。

/// <summary>

/// 多播委托

/// </summary>

public class MultiDelegate

{

private delegate int DemoMultiDelegate(out int x);

private static int Show1(out int x)

{

x = 1;

Console.WriteLine("This is the first show method:"+x);

return x;

}

private static int Show2(out int x)

{

x = 2;

Console.WriteLine("This is the second show method:"+x);

return x;

}

private static int Show3(out int x)

{

x = 3;

Console.WriteLine("This is the third show method:"+x);

return x;

}

/// <summary>

/// 调用多播委托

/// </summary>

public void Show()

{

DemoMultiDelegate dmd = new DemoMultiDelegate(Show1);

dmd += new DemoMultiDelegate(Show2);

dmd += new DemoMultiDelegate(Show3);//检查结果

int x = 5;

int y= dmd(out x);

Console.WriteLine(y);

   }
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值