九、C# 合式类型
class Program
{
static void Main(string[] args)
{ Angle a = new Angle(,,);
Angle b = new Angle(,,);
Coordinate c1 = new Coordinate();
Coordinate c2 = new Coordinate();
c1.Latitude = a;
c2.Latitude = a;
c1.Longitude = b;
c2.Longitude = b;
Console.WriteLine(c1 == c2);
Console.WriteLine(c1 != c2);
Console.ReadLine(); }
}
struct Angle
{ public Angle(int hours, int minutes, int seconds)
{
_Hours = hours;
_Minutes = minutes;
_Seconds = seconds;
}
public int Hours
{
get
{
return _Hours;
}
}
private int _Hours;
public int Minutes
{
get
{
return _Minutes;
}
}
private int _Minutes;
public int Seconds
{
get
{
return _Seconds;
}
}
private int _Seconds;
public Angle Move(int hours, int minutes, int seconds)
{
return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds);
}
} class Coordinate
{
public Angle Latitude
{
get
{
return _Latitude;
}
set
{
_Latitude = value;
}
}
private Angle _Latitude; public Angle Longitude
{
get
{
return _Longitude;
}
set
{
_Longitude = value;
}
}
private Angle _Longitude;
public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide)
{
if (ReferenceEquals(leftHandSide, null))
{
return ReferenceEquals(rightHandSide, null);
}
return (leftHandSide.Equals(rightHandSide));
}
public static bool operator !=(Coordinate leftHandSide, Coordinate rightHandSide)
{
return !(leftHandSide == rightHandSide); }
//重写
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
//检查类型是否相同
if (this.GetType() != obj.GetType())
{
return false;
}
return Equals((Coordinate)obj);
}
//重载
public bool Equals(Coordinate obj)
{
if (ReferenceEquals(obj, null))
{
return false;
}
//如果引用相同,肯定为true
if (ReferenceEquals(this, obj))
{
return true;
}
//如果散列码值不相同,肯定不同。散列码本身就是根据值生成的整形值
if (this.GetHashCode() != obj.GetHashCode())
{
return false;
}
//检查基类是否有自定义的Equals()
//System.Diagnostics.Debug.Assert(base.GetType() != typeof(object));
//if (!base.Equals(obj))
//{
// return false;
//}
//最后,写上自定义的Equals 计算方法
return Longitude.Equals(obj.Longitude) && (Latitude.Equals(obj.Latitude));
}
//重写
public override int GetHashCode()
{
int hashCode = Longitude.GetHashCode();
hashCode ^= Latitude.GetHashCode();//使用自定义的计算方法(本处常用异或 )
return hashCode;
}
class Program
{
static void Main(string[] args)
{ Angle a = new Angle(, , );
Angle b = new Angle(, , );
Coordinate c1 = new Coordinate();
Coordinate c2 = new Coordinate();
c1.Latitude = a;
c2.Latitude = a;
c1.Longitude = b;
c2.Longitude = b;
Console.WriteLine(c1 == c2);
Console.WriteLine(c1 != c2);
Console.WriteLine((c1 + c2).ToString());
Console.ReadLine(); }
}
struct Angle
{ public Angle(int hours, int minutes, int seconds)
{
_Hours = hours;
_Minutes = minutes;
_Seconds = seconds;
}
public int Hours
{
get
{
return _Hours;
}
}
private int _Hours;
public int Minutes
{
get
{
return _Minutes;
}
}
private int _Minutes;
public int Seconds
{
get
{
return _Seconds;
}
}
private int _Seconds;
public Angle Move(int hours, int minutes, int seconds)
{
return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds);
}
} class Coordinate
{
public Angle Latitude
{
get
{
return _Latitude;
}
set
{
_Latitude = value;
}
}
private Angle _Latitude; public Angle Longitude
{
get
{
return _Longitude;
}
set
{
_Longitude = value;
}
}
private Angle _Longitude; public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide)
{
if (ReferenceEquals(leftHandSide, null))
{
return ReferenceEquals(rightHandSide, null);
}
return (leftHandSide.Equals(rightHandSide));
}
public static bool operator !=(Coordinate leftHandSide, Coordinate rightHandSide)
{
return !(leftHandSide == rightHandSide); }
public static Coordinate operator +(Coordinate leftHandSide, Coordinate rightHandSide)
{
Coordinate a = new Coordinate();
a.Latitude = new Angle(
leftHandSide.Latitude.Hours + rightHandSide.Latitude.Hours,
leftHandSide.Latitude.Minutes + rightHandSide.Latitude.Minutes,
leftHandSide.Latitude.Seconds + rightHandSide.Latitude.Seconds
);
a.Longitude = new Angle(
leftHandSide.Longitude.Hours + rightHandSide.Longitude.Hours,
leftHandSide.Longitude.Minutes + rightHandSide.Longitude.Minutes,
leftHandSide.Longitude.Seconds + rightHandSide.Longitude.Seconds
);
return a;
}
//重写
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
//检查类型是否相同
if (this.GetType() != obj.GetType())
{
return false;
}
return Equals((Coordinate)obj);
}
//重载
public bool Equals(Coordinate obj)
{
if (ReferenceEquals(obj, null))
{
return false;
}
//如果引用相同,肯定为true
if (ReferenceEquals(this, obj))
{
return true;
}
//如果散列码值不相同,肯定不同。散列码本身就是根据值生成的整形值
if (this.GetHashCode() != obj.GetHashCode())
{
return false;
}
//检查基类是否有自定义的Equals()
//System.Diagnostics.Debug.Assert(base.GetType() != typeof(object));
//if (!base.Equals(obj))
//{
// return false;
//}
//最后,写上自定义的Equals 计算方法
return Longitude.Equals(obj.Longitude) && (Latitude.Equals(obj.Latitude));
}
//重写
public override int GetHashCode()
{
int hashCode = Longitude.GetHashCode();
hashCode ^= Latitude.GetHashCode();//使用自定义的计算方法(本处常用异或 )
return hashCode;
}
//重写
public override string ToString()
{
string str = "";
str = "Latitude " + ((Latitude.Hours.ToString().Length == ) ? Latitude.Hours.ToString() : "" + Latitude.Hours.ToString()) + ":"
+ ((Latitude.Minutes.ToString().Length == ) ? Latitude.Minutes.ToString() : "" + Latitude.Minutes.ToString()) + ":"
+ ((Latitude.Hours.ToString().Length == ) ? Latitude.Seconds.ToString() : "" + Latitude.Seconds.ToString());
str += "\n";
str += "Longitude " + ((Longitude.Hours.ToString().Length == ) ? Longitude.Hours.ToString() : "" + Longitude.Hours.ToString()) + ":"
+ ((Longitude.Minutes.ToString().Length == ) ? Longitude.Minutes.ToString() : "" + Longitude.Minutes.ToString()) + ":"
+ ((Longitude.Hours.ToString().Length == ) ? Longitude.Seconds.ToString() : "" + Longitude.Seconds.ToString());
return str;
}
}
class Program
{
static void Main(string[] args)
{ Angle a = new Angle(, , );
Angle b = new Angle(, , );
Coordinate c1 = new Coordinate();
c1.Latitude = -a;
c1.Longitude = -b; Console.WriteLine(c1 ); Console.ReadLine(); }
}
struct Angle
{ public Angle(int hours, int minutes, int seconds)
{
_Hours = hours;
_Minutes = minutes;
_Seconds = seconds;
}
public int Hours
{
get
{
return _Hours;
}
}
private int _Hours;
public int Minutes
{
get
{
return _Minutes;
}
}
private int _Minutes;
public int Seconds
{
get
{
return _Seconds;
}
}
private int _Seconds;
public Angle Move(int hours, int minutes, int seconds)
{
return new Angle(Hours + hours, Minutes + minutes, Seconds + seconds);
}
//一元运算符重载
public static Angle operator -(Angle a)
{
Angle temp = new Angle();
temp._Hours = -a.Hours;
temp._Minutes = -a.Minutes;
temp._Seconds =- a.Seconds;
return temp;
}
} class Coordinate
{
public Angle Latitude
{
get
{
return _Latitude;
}
set
{
_Latitude = value;
}
}
private Angle _Latitude; public Angle Longitude
{
get
{
return _Longitude;
}
set
{
_Longitude = value;
}
}
private Angle _Longitude; //二元运算符重载
public static bool operator ==(Coordinate leftHandSide, Coordinate rightHandSide)
{
if (ReferenceEquals(leftHandSide, null))
{
return ReferenceEquals(rightHandSide, null);
}
return (leftHandSide.Equals(rightHandSide));
}
public static bool operator !=(Coordinate leftHandSide, Coordinate rightHandSide)
{
return !(leftHandSide == rightHandSide); }
public static Coordinate operator +(Coordinate leftHandSide, Coordinate rightHandSide)
{
Coordinate a = new Coordinate();
a.Latitude = new Angle(
leftHandSide.Latitude.Hours + rightHandSide.Latitude.Hours,
leftHandSide.Latitude.Minutes + rightHandSide.Latitude.Minutes,
leftHandSide.Latitude.Seconds + rightHandSide.Latitude.Seconds
);
a.Longitude = new Angle(
leftHandSide.Longitude.Hours + rightHandSide.Longitude.Hours,
leftHandSide.Longitude.Minutes + rightHandSide.Longitude.Minutes,
leftHandSide.Longitude.Seconds + rightHandSide.Longitude.Seconds
);
return a;
} //Object成员方法重写
//重写
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
//检查类型是否相同
if (this.GetType() != obj.GetType())
{
return false;
}
return Equals((Coordinate)obj);
} public override int GetHashCode()
{
int hashCode = Longitude.GetHashCode();
hashCode ^= Latitude.GetHashCode();//使用自定义的计算方法(本处常用异或 )
return hashCode;
} public override string ToString()
{
string str = "";
str = "Latitude " + ((Latitude.Hours.ToString().Length == ) ? Latitude.Hours.ToString() : "" + Latitude.Hours.ToString()) + ":"
+ ((Latitude.Minutes.ToString().Length == ) ? Latitude.Minutes.ToString() : "" + Latitude.Minutes.ToString()) + ":"
+ ((Latitude.Hours.ToString().Length == ) ? Latitude.Seconds.ToString() : "" + Latitude.Seconds.ToString());
str += "\n";
str += "Longitude " + ((Longitude.Hours.ToString().Length == ) ? Longitude.Hours.ToString() : "" + Longitude.Hours.ToString()) + ":"
+ ((Longitude.Minutes.ToString().Length == ) ? Longitude.Minutes.ToString() : "" + Longitude.Minutes.ToString()) + ":"
+ ((Longitude.Hours.ToString().Length == ) ? Longitude.Seconds.ToString() : "" + Longitude.Seconds.ToString());
return str;
}
//重载
public bool Equals(Coordinate obj)
{
if (ReferenceEquals(obj, null))
{
return false;
}
//如果引用相同,肯定为true
if (ReferenceEquals(this, obj))
{
return true;
}
//如果散列码值不相同,肯定不同。散列码本身就是根据值生成的整形值
if (this.GetHashCode() != obj.GetHashCode())
{
return false;
}
//检查基类是否有自定义的Equals()
//System.Diagnostics.Debug.Assert(base.GetType() != typeof(object));
//if (!base.Equals(obj))
//{
// return false;
//}
//最后,写上自定义的Equals 计算方法
return Longitude.Equals(obj.Longitude) && (Latitude.Equals(obj.Latitude));
}
}
if (c1)
{
Console.WriteLine(c1);
}
public static bool operator false(Coordinate c)
{
if (c.Latitude.Hours < )
{ return false;
}
else
{
return true;
}
}
public static bool operator true(Coordinate c)
{
if (c.Latitude.Hours < )
{
return false;
}
else
{
return true;
}
}
public static implicit operator double(Coordinate c)
{
return (double)c.Latitude.Hours;
}
public static implicit operator Coordinate(double hours)
{
Coordinate c = new Coordinate();
c.Latitude = new Angle((int)hours, , );
return c;
}
implict 隐式 explicit显式
private WeakReference Data;
public FileStream GetData()
{
FileStream data = (FileStream)Data.Target;
if (data != null)
{
return data;
}
else
{
//创建新的文件流
return data;
}
}
public class TemporaryFileStream
{
private readonly FileStream _Stream;
public FileStream Stream
{
get { return _Stream; }
}
private FileInfo _File = new FileInfo(Path.GetTempFileName());
public FileInfo File
{
get { return _File; }
} public TemporaryFileStream()
{
_File = new FileInfo(Path.GetTempFileName());
_Stream = new FileStream(File.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
~TemporaryFileStream()
{
Close();
}
public void Close()
{
if (Stream != null)
{
Stream.Close();
}
if (File != null)
{
File.Delete();
}
} }
class Program
{
static void Main(string[] args)
{
TemporaryFileStream fileStream = new TemporaryFileStream();
//Use temporary file stream; //.. fileStream.Dispose(); //.. }
} public class TemporaryFileStream : IDisposable
{
private readonly FileStream _Stream;
public FileStream Stream
{
get { return _Stream; }
}
private FileInfo _File = new FileInfo(Path.GetTempFileName());
public FileInfo File
{
get { return _File; }
} public TemporaryFileStream()
{
_File = new FileInfo(Path.GetTempFileName());
_Stream = new FileStream(File.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
~TemporaryFileStream()
{
Close();
}
public void Close()
{
if (Stream != null)
{
Stream.Close();
}
if (File != null)
{
File.Delete();
}
// Turn off calling the finalizer
System.GC.SuppressFinalize(this);
}
public void Dispose()
{
Close();
} }
static void Search()
{
using (TemporaryFileStream fileStream1 = new TemporaryFileStream(), fileStream2 = new TemporaryFileStream())
{
//出了这个范围,会自动被释放
}
}
class DataCache
{
private TemporaryFileStream _FileStream = null;
public TemporaryFileStream FileStream
{
get
{
if (_FileStream == null)
{
_FileStream = new TemporaryFileStream();
}
return _FileStream;
}
} }
class DataCache
{ //为泛型和lambda表达式使用推迟加载(C#4.0 CLR添加了一个新类来帮助进行推迟初始化:System.Lazy<T>
private Lazy<TemporaryFileStream> _FileStream = null; public DataCache()
{
_FileStream = new Lazy<TemporaryFileStream>(()=> new TemporaryFileStream() );
}
public TemporaryFileStream FileStream
{
get
{
return _FileStream.Value;
}
} }
九、C# 合式类型的更多相关文章
- JSP JSP工作原理 JSP语法 JSP声明 JSP注释 JSP指令 jsp九大隐式/内置对象
1 什么是JSP 1)为什么说,Servlet是一个动态Web开发技术呢? Servlet是基于服务端的一种动态交互技术, HttpServletRequest表示客户端到服务端的 ...
- JSP页面以及JSP九大隐式对象
JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比 ...
- jsp学习--JSP运行原理,九大隐式对象和JSP常用标签
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...
- JSP--JSP语法--指令---九大隐式对象--四大域对象--JSP内置标签--JavaBean的动作元素--MVC三层架构
一.JSP 原理:JSP其实就是一个servlet. Servlet负责业务逻辑处理,JSP只负责显示.开发中,JSP中不能有一行JAVA代码 二.JSP语法 1. JSP模板元素:JSP中HTML标 ...
- JSP--JSP语法--指令--include(动态包含/静态包含)--九大隐式对象--四大域对象--JSP内置标签--JavaBean的动作元素--MVC三层架构
一.JSP 原理:JSP其实就是一个servlet. Servlet负责业务逻辑处理,JSP只负责显示.开发中,JSP中不能有一行JAVA代码 二.JSP语法 1. JSP模板元素:JSP中HT ...
- JSP九大隐式对象和四大域对象-----面试
因为jsp实质是一个Servlet对象:jsp在第一次访问时会被Web容器翻译成Servlet,在执行过程:第一次访问---->inex.jsp---->index_jsp.java--- ...
- C#中的隐式类型var——详细示例解析
从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,它的具体类型由编译器根据上下文推断而出. 下面就让我来总结下隐式类型的一些特点: 1.va ...
- C++中的显式类型转化
类型转化也许大家并不陌生,int i; float j; j = (float)i; i = (int)j; 像这样的显式转化其实很常见,强制类型转换可能会丢失部分数据,所以如果不加(int)做强制转 ...
- .NET中那些所谓的新语法之一:自动属性、隐式类型、命名参数与自动初始化器
开篇:在日常的.NET开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...
随机推荐
- Delphi中禁止WebBrowser右键的方法
uses MSHtml; //在控件标签additional中找到TApplicationEvents控件,拖到窗体上.在TApplicationEvents的OnMessage事件中加入以下代码: ...
- 网络流相关(拓扑)CodeForces 269C:Flawed Flow
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious prog ...
- XP与Win2003下网站配置
一. 安装.net 4.0 1. 双击打开文件dotNetFx40_Full_x86_x64.exe.如图4.1 所示 (图4.1) 2. 勾选[我已阅读并结束许可条款],点击[安装]按钮.如图4.2 ...
- javascipt取整数四舍五入
1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...
- C# 客服端上传文件与服务器器端接收 (简单代码)
简单代码: /*服务器端接收写入 可以实现断点续传*/ public string ConnectUpload(string newfilename,string filepath,byte[] fi ...
- vijosP1071 新年趣事之打牌
vijosP1071 新年趣事之打牌 链接:https://vijos.org/p/1071 [思路] 01背包+路径输出. 用d[][]记录[][]可转移的数目,>=2则输出-1,0输出0,否 ...
- XML文档部署到Tomcat服务器上总是加载出错
config.xnl 起初文档路径是在src/Dao/config.xml 在Dao目录下BaseDao类中,解析config.xml文件路径 path="/Dao/config.xml&q ...
- Nginx + Tomcat 动静分离实现负载均衡
0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...
- java 检查是否是数组 检查是否是空数组 检查数组是否包含某个元素
/** * Determine whether the given object is an array: * either an Object array or a primitive array. ...
- c#基础语言编程-程序集和反射
程序集 什么是程序集? 1.程序集(assembly)是一个及一个以上托管模块,以及一些资源文件的逻辑组合. 2.程序集是组件复用,以及实施安全策略和版本策略的最小单位. 3.程序集是包含一个或者多个 ...