九、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开发学习中,我们往往会接触到一些较新的语法,它们相对以前的老语法相比,做了很多的改进,简化了很多繁杂的代码格式,也大大减少了我们这些菜鸟码农的代码量.但是,在开心欢乐之余,我们也 ...
随机推荐
- jQuery动态五星评分
效果 css .star ul, .star li { list-style: none; } .star { position: relative; width: 600px; height: 24 ...
- 挖坑#3-----DP优化+CDQ分治+期望DP
1492: [NOI2007]货币兑换Cash 1176: [Balkan2007]Mokia 1452: [JSOI2009]Count 1563: [NOI2009]诗人小G tyvj1309 ...
- android滑动删除的多种实现方式(一)
个人习惯,先上图 同事是个妹子(这点很重要),写滑动删除动能的时候用到了SwipeLayout,然后悲催的是,滑动时间被拦截了,解决方法先不提,在(一)中先讲解SwipeLayout下载listvie ...
- 前端程序员:月薪 5K 到 5 万,我干了啥
高贵的前端程序猿们: 如何在前端开发这种高精尖的技术领域找到心仪的工作?实现在咖啡馆喝喝咖啡敲敲代码就能升职加薪.买房买车.迎娶白富美走上人生巅峰的职业梦想?这篇<进化论:从 0 到 100,前 ...
- pm grant 命令
CustomLocale.apk所需要的权限"android.permission.CHANGE_CONFIGURATION"自Android 4.2,4.2.2起系统定义为and ...
- poj1691绘画板
1 7 0 0 2 2 1 0 2 1 6 2 2 0 4 2 1 1 2 4 4 2 1 4 3 6 1 4 0 6 4 1 3 4 6 6 2 #include<stdio.h> #i ...
- MySQL查询过程中出现lost connection to mysql server during query 的解决办法
window7 64位系统,MySQL5.7 问题:在使用shell进行数据表更新操作的过程,输入以下查询语句: ,; 被查询的表记录数达到500W条,在查询过程中出现如题目所示的问题,提示" ...
- 【Android - 框架】之Retrofit的使用
Retrofit是Square公司发布的一个可以应用在Android和Java中的Http客户端访问框架,其底层应用的是OkHttp. 在这个帖子中,我们以下面这个Http请求为例: https:// ...
- hdu2128之BFS
Tempter of the Bone II Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 98304/32768 K (Java/ ...
- C#自定义泛型类绑定ComboBox控件
C# WinForm ComboBox 自定义数据项 (ComboBoxItem ) WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用, 因为大家日常应 ...