implicit和 explicit关键字
implicit 关键字用于声明隐式的用户定义类型转换运算符。 如果可以确保转换过程不会造成数据丢失,则可使用该关键字在用户定义类型和其他类型之间进行隐式转换。
class Digit
{
public Digit(double d) { val = d; }
public double val;
// ...other members // User-defined conversion from Digit to double
public static implicit operator double(Digit d)
{
return d.val;
}
// User-defined conversion from double to Digit
public static implicit operator Digit(double d)
{
return new Digit(d);
}
} class Program
{
static void Main(string[] args)
{
Digit dig = new Digit();
//This call invokes the implicit "double" operator
double num = dig;
//This call invokes the implicit "Digit" operator
Digit dig2 = ;
Console.WriteLine("num = {0} dig2 = {1}", num, dig2.val);
Console.ReadLine();
}
}
explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。 例如,在下面的示例中,此运算符将名为 Fahrenheit 的类转换为名为 Celsius 的类
class Celsius
{
public Celsius(float temp)
{
degrees = temp;
}
public static explicit operator Fahrenheit(Celsius c)
{
return new Fahrenheit((9.0f / 5.0f) * c.degrees + );
}
public float Degrees
{
get { return degrees; }
}
private float degrees;
} class Fahrenheit
{
public Fahrenheit(float temp)
{
degrees = temp;
}
// Must be defined inside a class called Fahrenheit:
public static explicit operator Celsius(Fahrenheit fahr)
{
return new Celsius((5.0f / 9.0f) * (fahr.degrees - ));
}
public float Degrees
{
get { return degrees; }
}
private float degrees;
} class MainClass
{
static void Main()
{
Fahrenheit fahr = new Fahrenheit(100.0f);
Console.Write("{0} Fahrenheit", fahr.Degrees);
Celsius c = (Celsius)fahr; Console.Write(" = {0} Celsius", c.Degrees);
Fahrenheit fahr2 = (Fahrenheit)c;
Console.WriteLine(" = {0} Fahrenheit", fahr2.Degrees);
}
}
implicit和 explicit关键字的更多相关文章
- C++ explicit关键字详解
本文系转载,原文链接:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用 ...
- C++ explicit关键字详解(转载)
转载:https://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函 ...
- C++ explicit 关键字
原文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...
- 2.12 C++ explicit关键字详解
参考:http://www.cnblogs.com/ymy124/p/3632634.html 总结: 带参数的构造函数中有两种比较常见的构造函数:拷贝构造函数和转型构造函数. 转型构造函数只有一个参 ...
- 第8课 列表初始化(3)_防止类型收窄、explicit关键字
1. 防止类型收窄 (1)类型收窄:指的是导致数据内容发生变化或者精度丢失的隐式类型转换. (2)类型收窄的几种情况: ①从浮点数隐式转换为整型数,如int i=2.2; ②从高精度浮点数隐式转换为低 ...
- C++雾中风景5:Explicit's better than implicit.聊聊Explicit.
关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议.那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊.而很多时候,Implicit的很多 ...
- <转>C++ explicit关键字详解
要文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...
- C++的explicit关键字
C++程序员对于explicit这个关键字其实不是很熟悉,至少我是如此:原因在于其使用范围不大,而且作用也没有那么大. 但是这不是说明我们的程序中不需要这个关键字,按Google的C++编程规范和Ef ...
- [转] C++ explicit关键字详解
本文转自tiankong19999 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是impl ...
随机推荐
- html <frame>标签使用
标签定义 frameset 中的一个特定的窗口(框架) frameset中的每个框架都可以设置不同的属性,比如border,scrolling,noresize等 frame的常用属性 width: ...
- Solr查询参数
引自:http://www.cnblogs.com/zhangweizhong/p/5056884.html 一.基本查询 q 查询的关键字,此参数最为重要,例如,q=id:1,默认为q=*:*, ...
- c#版本与vs的对应关系
版本 .NET Framework版本 Visual Studio版本 发布日期 特性 C# 1.0 .NET Framework 1.0 Visual Studio .NET 2002 2002.1 ...
- Enginering English for interview (1)
I was lucky to work in a foreign company, Here is an overview of the interview test : 1.Because of t ...
- ApplicationContext详解以及多个ApplicationContext.xml的相互引用
如果说BeanFactory是spring的心脏,那么Application就是完整的身躯.ApplicationContext就是由BeanFactory派生出来的. 一.ApplicationCo ...
- <xsd:import>
最近研究wsdl文件发现在生成客户端代码时报错了, WSDLException (at /wsdl:definitions/wsdl:types/xsd:schema): faultCode=OTHE ...
- Iview同步异步验证
直接上代码了: html: <Form ref="termForm" :model="currentData" :rules="ruleTerm ...
- nwjs 实现的 分块上传
1.先上核心工具类/** * Created by Administrator on 2017/6/12. */let fs = require("fs");/** * Creat ...
- 关于凑数问题的dfs
https://www.nowcoder.com/acm/contest/42/F 首先由于是单一解问题,所以使用返回值类型为bool的dfs 然后为了保证dfs的效率性,应该把加数dfs放在前面,不 ...
- LG1419 【寻找段落】
前言 鉴于这题的题解质量(连LaTeX公式都没有),我决定再发一篇详细的题解,不仅方便大家,还可以作为我学习单调队列优化dp的小结(尽管这题不是dp). 分析 题目要求求一个最大的实数\(x\),使得 ...