九、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开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...
 
随机推荐
- 【HDOJ】2888 Check Corners
			
二维RMQ. /* 2888 */ #include <iostream> #include <algorithm> #include <cstdio> #incl ...
 - 【转】vc中使用SendMessage正确发送自定义消息的方法--不错
			
原文网址:http://zhoumf1214.blog.163.com/blog/static/5241940200910265532959/ 最近在用VC2008做开发,后来由于要用到消息的发送,而 ...
 - pm grant 命令
			
CustomLocale.apk所需要的权限"android.permission.CHANGE_CONFIGURATION"自Android 4.2,4.2.2起系统定义为and ...
 - poj 3710 Christmas Game(树上的删边游戏)
			
Christmas Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1967 Accepted: 613 Des ...
 - MVC 5 第二章 项目结构
			
通过本章学习,你将了解到一个MVC 5应用程序的项目组成以及项目文件的相关信息,从而更好地架构设计出自己的项目结构. 单从MVC的字面意思我们便能够注意到M-模型, View-视图, Controll ...
 - 《University Calculus》-chape4-极坐标与圆锥曲线-极坐标系下的面积与弧长
			
极坐标系下的面积: 在直角坐标系下一样,这里在极坐标系下,我们面临一个同样的问题:如何求解一个曲线围成的面积?虽然两种情况本质上是一样的,但是还是存在一些细小的区别. 在直角坐标系下中,我们是讨论一条 ...
 - 实现自己的脚本语言ngscript之零
			
正式开始介绍前先扯点没用的. 从小玩basic长大的小朋友大多有一个梦想,就是自己实现一个basic解释器. 不过这里我实现的不是basic,而是一个语法和功能类似javascript的东西. 暂且称 ...
 - android进程间通信:使用AIDL
			
android 的binder其实是基于 openbinder实现的,openbinder的地址:http://www.angryredplanet.com/~hackbod/openbinder/d ...
 - XMPP and Asterisk integration
			
http://www.mundoopensource.com.br/en_page_xmpp_asterisk_pratical_example/ www.mundoopensource.com.br ...
 - Jenkins的plugin开发
			
Jenkins强大的功能主要靠其丰富的plugin体现,之前的一篇博客<Jenkins安装plugin>中介绍了如何找到并安装需要的plugin.虽然目前已经有大量非常优秀的plugin可 ...