C# 程序员参考--运算符重载教程(转)

本文介绍了如何通过运算符重载实现复数加法及三值逻辑运算。首先,通过定义复数类并重载加法运算符实现复数相加。其次,定义了一个三值逻辑类型,并重载了多种逻辑运算符。

本教程演示用户定义的类如何重载运算符。

示例文件

请参见“运算符重载”示例以下载和生成本教程中讨论的示例文件。
教程

运算符重载允许为运算指定用户定义的运算符实现,其中一个或两个操作数是用户定义的类或结构类型。本教程包含两个示例。第一个示例展示如何使用运算符重载创建定义复数加法的复数类。第二个示例展示如何使用运算符重载实现三值的逻辑类型。

示例 1

本示例展示如何使用运算符重载创建定义复数加法的复数类 Complex。本程序使用 ToString 方法的重载显示数字的虚部和实部以及加法结果。

// complex.csusing System;public struct Complex {   public int real;   public int imaginary;   public Complex(int real, int imaginary)    {      this.real = real;      this.imaginary = imaginary;   }   // Declare which operator to overload (+), the types    // that can be added (two Complex objects), and the    // return type (Complex):   public static Complex operator +(Complex c1, Complex c2)    {      return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);   }   // Override the ToString method to display an complex number in the suitable format:   public override string ToString()   {      return(String.Format("{0} + {1}i", real, imaginary));   }   public static void Main()    {      Complex num1 = new Complex(2,3);      Complex num2 = new Complex(3,4);      // Add two Complex objects (num1 and num2) through the      // overloaded plus operator:      Complex sum = num1 + num2;     // Print the numbers and the sum using the overriden ToString method:      Console.WriteLine("First complex number:  {0}",num1);      Console.WriteLine("Second complex number: {0}",num2);      Console.WriteLine("The sum of the two numbers: {0}",sum);    }}
输出
First complex number:  2 + 3iSecond complex number: 3 + 4iThe sum of the two numbers: 5 + 7i
示例 2

本示例展示如何使用运算符重载实现三值的逻辑类型。该类型的可能值有 DBBool.dbTrueDBBool.dbFalseDBBool.dbNull,其中 dbNull 成员表示未知值。

注意 定义 True 和 False 运算符只对表示 True、False 和 Null(既非 True 也非 False)的类型有用,如数据库中使用的类型。
// dbbool.csusing System;public struct DBBool{   // The three possible DBBool values:   public static readonly DBBool dbNull = new DBBool(0);   public static readonly DBBool dbFalse = new DBBool(-1);   public static readonly DBBool dbTrue = new DBBool(1);   // Private field that stores -1, 0, 1 for dbFalse, dbNull, dbTrue:   int value;    // Private constructor. The value parameter must be -1, 0, or 1:   DBBool(int value)    {      this.value = value;   }   // Implicit conversion from bool to DBBool. Maps true to    // DBBool.dbTrue and false to DBBool.dbFalse:   public static implicit operator DBBool(bool x)    {      return x? dbTrue: dbFalse;   }   // Explicit conversion from DBBool to bool. Throws an    // exception if the given DBBool is dbNull, otherwise returns   // true or false:   public static explicit operator bool(DBBool x)    {      if (x.value == 0) throw new InvalidOperationException();      return x.value > 0;   }   // Equality operator. Returns dbNull if either operand is dbNull,    // otherwise returns dbTrue or dbFalse:   public static DBBool operator ==(DBBool x, DBBool y)    {      if (x.value == 0 || y.value == 0) return dbNull;      return x.value == y.value? dbTrue: dbFalse;   }   // Inequality operator. Returns dbNull if either operand is   // dbNull, otherwise returns dbTrue or dbFalse:   public static DBBool operator !=(DBBool x, DBBool y)    {      if (x.value == 0 || y.value == 0) return dbNull;      return x.value != y.value? dbTrue: dbFalse;   }   // Logical negation operator. Returns dbTrue if the operand is    // dbFalse, dbNull if the operand is dbNull, or dbFalse if the   // operand is dbTrue:   public static DBBool operator !(DBBool x)    {      return new DBBool(-x.value);   }   // Logical AND operator. Returns dbFalse if either operand is    // dbFalse, dbNull if either operand is dbNull, otherwise dbTrue:   public static DBBool operator &(DBBool x, DBBool y)    {      return new DBBool(x.value < y.value? x.value: y.value);   }   // Logical OR operator. Returns dbTrue if either operand is    // dbTrue, dbNull if either operand is dbNull, otherwise dbFalse:   public static DBBool operator |(DBBool x, DBBool y)    {      return new DBBool(x.value > y.value? x.value: y.value);   }   // Definitely true operator. Returns true if the operand is    // dbTrue, false otherwise:   public static bool operator true(DBBool x)    {      return x.value > 0;   }   // Definitely false operator. Returns true if the operand is    // dbFalse, false otherwise:   public static bool operator false(DBBool x)    {      return x.value < 0;   }   // Overload the conversion from DBBool to string:   public static implicit operator string(DBBool x)    {      return x.value > 0 ? "dbTrue"           : x.value < 0 ? "dbFalse"           : "dbNull";   }   // Override the Object.Equals(object o) method:   public override bool Equals(object o)    {      try       {         return (bool) (this == (DBBool) o);      }      catch       {         return false;      }   }   // Override the Object.GetHashCode() method:   public override int GetHashCode()    {      return value;   }   // Override the ToString method to convert DBBool to a string:   public override string ToString()    {      switch (value)       {         case -1:            return "DBBool.False";         case 0:            return "DBBool.Null";         case 1:            return "DBBool.True";         default:            throw new InvalidOperationException();      }   }}class Test {   static void Main()    {      DBBool a, b;      a = DBBool.dbTrue;      b = DBBool.dbNull;      Console.WriteLine( "!{0} = {1}", a, !a);      Console.WriteLine( "!{0} = {1}", b, !b);      Console.WriteLine( "{0} & {1} = {2}", a, b, a & b);      Console.WriteLine( "{0} | {1} = {2}", a, b, a | b);      // Invoke the true operator to determine the Boolean       // value of the DBBool variable:      if (b)         Console.WriteLine("b is definitely true");      else         Console.WriteLine("b is not definitely true");      }}
输出
!DBBool.True = DBBool.False!DBBool.Null = DBBool.NullDBBool.True & DBBool.Null = DBBool.NullDBBool.True 
[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924144/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924144/

提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值