基 础-----接口

 
定义接口的语法如下:
 
[attributes] [access-modifier] interface interface-name[:base-list]
{interface-body}
 
接口的目的是定义一些类所应具有的功能。
 
interface IStorable{
 void Read( );
 void Write( object obj );
 int Status { get; set; }
}
 
interface ICompressible
{
 void Compress( );
 void Decompress( );
}
 
继承自接口的类必须实现接口声明的所有方法和属性。
 
接口中的方法不需要加 访问修饰符(access-modifier) ,否则会产生编译错误。接口中的方法隐式的被声明为public,因为接口是一个由其他类所使用的契约(contract)。
 
不能创建接口的实例,只能创建实现了接口的类的实例。实现了接口的类的实例,可以转换为这一接口类型。
 
WARNING:实现接口的类中的方法不可以是静态的(static)。
实现多个接口
public class Document : IStorable, ICompressible
扩展接口
interface ILoggedCompressible : ICompressible
{
    void LogSavedBytes();
}
复合接口
interface IStorableCompressible : IStorable, ILoggedCompressible
{
     void LogOriginalSize();
}
将类的实例转换为接口类型
Public class Document:IStorableCompressible
{
 // implemention
}
 
Document doc = new Document();
IStorable isDoc = doc as IStorable;
IStorableCompressible iscDoc = doc as IStorableCompressible;
 
其中 IStorable isDoc = doc as IStorable也可直接写作 IStorable isDoc = doc
因为编译器知道Document类实现了IStorable接口,并会进行隐式转换。
 
如果不确定某个类是否实现了 IStrorable 接口,可以使用 as ,如果该类没有实现,将返回null。
 
IStrorable icDoc = someClassInstance as IStrorable;
if ( icDoc != null )
{
   icDoc.Read( );
}
else
   Console.WriteLine( "Read not supported" );
访问接口方法
以多态方式使用接口。
 
IStorable isDoc = new Document("Test Document");
icDoc.Read( );
将对象转换成接口类型
很多情况下,并不知道集合中的某个对象是否实现了接口。如果像下面这样使用接口:
 
Document doc = myCollection[0];
 
IStorable isDoc = (IStorable) doc;
isDoc.Read( );
 
ICompressible icDoc = (ICompressible) doc;
icDoc.Compress( );
 
而 Document的声明是这样的:
 
public class Document : IStorable
 
则将抛出异常。
使用 is 操作符
为了判断某个对象是否实现了特定接口,可以使用 is 操作符:
 
expression is type
 
如果 expression 可以转换成 type类型,则上面的表达式返回 true。
 
使用 is 的范例:
 
if (doc is IStorable)
{
 IStorable isDoc = (IStorable)doc;
 isDoc.Read( );
}
使用 as 操作符
也可以使用 as ,当某个对象不能转换为特定接口时,将返回 null。
 
expression as type
 
使用 as 的范例:
 
IStorable isDoc = doc as IStorable;
if (isDoc != null)
{
 isDoc.Read( );
}
 
在类型转换时使用 as 优于使用 is(原因略)
抽象类 vs 接口
很多情况下 抽象类 和 接口可以互相转换,而且区别甚微。
abstract class Storable
{
 abstract public void Read();
 abstract public void Write();
}
 
使用接口的理由:
l         可以继承多个接口,但只能继承一个抽象类
 
使用抽象类的理由:
l         抽象类可以更好的进行版本控制。如果日后修改一个抽象类,只需再往里添加一个虚拟方法,并提供一个默认实现。而对于一个接口,如果添加一个新方法,则只有两种可能:与此接口相关的所有类都必须改写,以实现这个新添加的方法;可以新起一个接口,并让这个接口继承自老接口,如此一来,你将会获得非常多的接口,很难管理。
覆盖接口实现的方法
一个实现了接口的类,可以将实现接口的任何或者全部方法设为虚拟的。这样,继承此类的其他类可以对这些方法进行覆盖。
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace overridingInterface {
 interface IStorable {
      void Read();
      void Write();
 }
 
 /* 实现IStorable 的简单Document
  * ***************************************/
 public class Document : IStorable {
 
      public Document(string s) {
          Console.WriteLine("Creating document with: {0}", s);
      }
 
      //虚拟方法
      public virtual void Read() {
          Console.WriteLine("Document Read Method for IStorable");
      }
 
      public void Write() {
          Console.WriteLine("Document Write Method for IStorable");
      }
 }
 
 /* 继承自Document 的类
  * ***************************************/
 public class Note : Document {
      public Note(string s): base(s) {
          Console.WriteLine("Creating note with: {0}", s);
      }
 
      // 覆盖Document中的虚拟方法
      public override void Read() {
          Console.WriteLine("Overriding the Read method for Note!");
      }
 
      // 实现一个Note的新Write方法
      public new void Write() {
          Console.WriteLine("Implementing the Write method for Note!");
      }
 }
 
 public class Tester {
 
      static void Main() {
          Document theDoc = new Document("** Doc");
          theDoc.Read();
          theDoc.Write();
 
          Console.WriteLine();
 
          IStorable isDoc = theDoc as IStorable;
          if (isDoc != null) {
             isDoc.Read();
              isDoc.Write();
          }
 
          Console.WriteLine("/n*************************************/n");
 
          Document theNote = new Note("** Fist Note");
          theNote.Read(); // 输出为覆盖了的Read() 方法
          theNote.Write();
         
          Console.WriteLine();
         
          IStorable isNote = theNote as IStorable;
          if (isNote != null) {
              isNote.Read(); //输出为覆盖了的Read() 方法
              isNote.Write();
          }
 
          Console.WriteLine("/n*************************************/n");
 
          Note note2 = new Note("** Second Note");
          note2.Read();
         note2.Write();// 输出为使用new 的,重新实现了的Write()方法
 
          Console.WriteLine();
 
          IStorable isNote2 = note2 as IStorable;
          if (isNote2 != null) {
              isNote2.Read();
              isNote2.Write();// 输出为Document 实现的的方法
          }
 
          Console.ReadKey();
      }
 }
}
 
输出为:
Creating document with: ** Doc
Document Read Method for IStorable
Document Write Method for IStorable
 
Document Read Method for IStorable
Document Write Method for IStorable
 
*************************************
 
Creating document with: ** Fist Note
Creating note with: ** Fist Note
Overriding the Read method for Note!
Document Write Method for IStorable
 
Overriding the Read method for Note!
Document Write Method for IStorable
 
*************************************
 
Creating document with: ** Second Note
Creating note with: ** Second Note
Overriding the Read method for Note!
Implementing the Write method for Note!
 
Overriding the Read method for Note!
Document Write Method for IStorable
显式实现接口方法
当一个类实现了两个接口(假设Document 类实现了IStorable和ITalk接口),但是两个接口中有方法名相同时,可以使用下面的语法来显式地实现一个接口:
 
void ITalk.Read()
 
显式实现接口的方法时,不可以加访问修饰符(access modifier),将隐式地声明为public。
 
不能通过类的实例来直接访问显式实现的方法。假设该类还实现了IStorable接口中的Read()的方法,当使用下面的语句时:
 
theDoc.Read( );
 
将会隐式调用IStorable的Read() 方法。
 
如果该类仅实现了ITalk接口,而没有实现IStorable接口,也就不存在方法名冲突的情况,但是却仍使用显示的接口声明,那么当使用 theDoc.Read() 时,将会出现编译错误。
 
'ExplicitImplementation.Document' does not contain a definition for 'Read' F:/MyApp/Test/ExplictImplament.cs 57 11 Test
 
当想使用 ITalk接口的方法时,需要进行一次类型转换,使用下面的语法:
 
ITalk itDoc = theDoc;
itDoc.Read();
成员隐藏
假设有如下两个接口:
 
interface IBase
{
   int P { get; set; }
}
 
interface IDerived : IBase
{
   new int P();
}
 
继承 IDerived的类至少需要进行一个显示实现。
 
class myClass : IDerived
{
   int IBase.P { get {...} }
  
   public int P( ) {...}
}
 
class myClass : IDerived
{
   public int P { get {...} }
  
   int IDerived.P( ) {...}
}
 
class myClass : IDerived
{
   int IBase.P { get {...} }
  
   int IDerived.P( ) {...}
}
实现接口的值类型(Struct)
如果使用值类型实现接口,则应通过值类型的对象访问接口方法,而不要转换成接口,再用接口进行访问,此时会多出一个“复制”了的引用对象,而原来的值对象依然存在,两个对象是各自独立的。
 
myStruct theStruct = new myStruct( );
theStruct.Status = 2;
 
IStorable isTemp = ( IStorable ) theStruct;
Console.WriteLine( "isTemp: {0}", isTemp.Status );
 
isTemp.Status = 4;
Console.WriteLine("theStruct:{0}, isTemp: {1}",theStruct.Status, isTemp.Status );
 
theStruct.Status = 6;
Console.WriteLine( "theStruct: {0}, isTemp: {1}",theStruct.Status, isTemp.Status );
 
输出为:
 
isTemp: 2
theStruct: 2, isTemp: 4
theStruct: 6, isTemp: 4
AI 代码审查Review工具 是一个旨在自动化代码审查流程的工具。它通过集成版本控制系统(如 GitHub 和 GitLab)的 Webhook,利用大型语言模型(LLM)对代码变更进行分析,并将审查意见反馈到相应的 Pull Request 或 Merge Request 中。此外,它还支持将审查结果通知到企业微信等通讯工具。 一个于 LLM 的自动化代码审查助手。通过 GitHub/GitLab Webhook 监听 PR/MR 变更,调用 AI 分析代码,并将审查意见自动评论到 PR/MR,同时支持多种通知渠道。 主要功能 多平台支持: 集成 GitHub 和 GitLab Webhook,监听 Pull Request / Merge Request 事件。 智能审查模式: 详细审查 (/github_webhook, /gitlab_webhook): AI 对每个变更文件进行分析,旨在找出具体问题。审查意见会以结构化的形式(例如,定位到特定代码行、问题分类、严重程度、分析和建议)逐条评论到 PR/MR。AI 模型会输出 JSON 格式的分析结果,系统再将其转换为多条独立的评论。 通用审查 (/github_webhook_general, /gitlab_webhook_general): AI 对每个变更文件进行整体性分析,并为每个文件生成一个 Markdown 格式的总结性评论。 自动化流程: 自动将 AI 审查意见(详细模式下为多条,通用模式下为每个文件一条)发布到 PR/MR。 在所有文件审查完毕后,自动在 PR/MR 中发布一条总结性评论。 即便 AI 未发现任何值得报告的问题,也会发布相应的友好提示和总结评论。 异步处理审查任务,快速响应 Webhook。 通过 Redis 防止对同一 Commit 的重复审查。 灵活配置: 通过环境变量设置
【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种于耦合DC-DC变换器的状态空间平均模型的建模策略。该方法通过数学建模手段对直流微电网系统进行精确的状态空间描述,并对其进行线性化处理,以便于系统稳定性分析与控制器设计。文中结合Matlab代码实现,展示了建模与仿真过程,有助于研究人员理解和复现相关技术,推动直流微电网系统的动态性能研究与工程应用。; 适合人群:具备电力电子、电力系统或自动化等相关背景,熟悉Matlab/Simulink仿真工具,从事新能源、微电网或智能电网研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网的动态建模方法;②学习DC-DC变换器在耦合条件下的状态空间平均建模技巧;③实现系统的线性化分析并支持后续控制器设计(如电压稳定控制、功率分配等);④为科研论文撰写、项目仿真验证提供技术支持与代码参考。; 阅读建议:建议读者结合Matlab代码逐步实践建模流程,重点关注状态变量选取、平均化处理和线性化推导过程,同时可扩展应用于更复杂的直流微电网拓扑结构中,提升系统分析与设计能力。
内容概要:本文介绍了于物PINN驱动的三维声波波动方程求解(Matlab代码实现)理信息神经网络(PINN)求解三维声波波动方程的Matlab代码实现方法,展示了如何利用PINN技术在无需大量标注数据的情况下,结合物理定律约束进行偏微分方程的数值求解。该方法将神经网络与物理方程深度融合,适用于复杂波动问题的建模与仿真,并提供了完整的Matlab实现方案,便于科研人员理解和复现。此外,文档还列举了多个相关科研方向和技术服务内容,涵盖智能优化算法、机器学习、信号处理、电力系统等多个领域,突出其在科研仿真中的广泛应用价值。; 适合人群:具备一定数学建模和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事计算物理、声学仿真、偏微分方程数值解等相关领域的研究人员; 使用场景及目标:①学习并掌握PINN在求解三维声波波动方程中的应用原理与实现方式;②拓展至其他物理系统的建模与仿真,如电磁场、热传导、流体力学等问题;③为科研项目提供可复用的代码框架和技术支持参考; 阅读建议:建议读者结合文中提供的网盘资源下载完整代码,按照目录顺序逐步学习,重点关注PINN网络结构设计、损失函数构建及物理边界条件的嵌入方法,同时可借鉴其他案例提升综合仿真能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值