九、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开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...
随机推荐
- 【canvas】基于坐标的碰撞检测 / 基本的动画 / 多物体动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Move Zeroes——Leetcode
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- Qt 与 JavaScript 通信
使用QWebView加载网页后,解决Qt与JavaScript通信的问题: The QtWebKit Bridge :http://qt-project.org/doc/qt-4.8/qtwebkit ...
- std::numeric_limits<int>::max() error C2589: '(' : illegal token on right side of '::' 解决办法
int max =std::numeric_limits<int>::max(); 根据错误提示: f:\code\cpp\webspider\main.cpp(47) : war ...
- c语言运算符号详细说明
C语言中具有右结合性的运算符包括所有单目运算符以及赋值运算符(=)和条件运算符.其它都是左结合性. 判断表达式计算顺序时,先按优先级高的先计算,优先级低的后计算,当优先级相同时再按结合性,或从左至右顺 ...
- 折腾iPhone的生活——运营商信号显示数据化
iOS7以后iphone的信号都是用5个小圆圈显示的,像这样 但是还有种显示方法可以用数字信号显示信号量,比较适合很专注于生活品质的人和对数字有偏爱的人,像这样: 这样还有个好处是可以节约顶部状态栏的 ...
- Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学
1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 394 Solve ...
- (转)WS2008远程桌面连接时提示:“要登录到此远程计算机,您必须被授予允许通过终端服务登录的权限”的解决办法
原文:http://www.chunfengxiyu.com/ws2008-mstsc-privilege.html WS2008远程桌面连接时提示:“要登录到此远程计算机,您必须被授予允许通过终端服 ...
- [JIT_APP]Java基础知识总结
一.Java语言的基础知识 1. 开发Java语言的公司 美国Sun(Sum Microsystems)公司开发. 2.Java的3个版本 J2SE(Java2 Standard Edition) ...
- mysql 主从同步配置
1 环境 mac air 主机做 主库,使用的是XAMPP自带的mysql 版本为 5.6.21, for osx10.6 (x86_64) 虚拟机mysql 做从库 版本为 5.5.38, fo ...