zoukankan
html css js c++ java
运算符重载 operator+[纯属温习啊][附加了一些内容 如:同名属性,复制构造函数]
//
转换运算符具有以下特点:
//
声明为 implicit 的转换在需要时自动进行。
//
声明为 explicit 的转换需要调用强制转换。
//
所有转换都必须是 static 转换。
public
class
OperatorOverride
{
public
static
void
Execute()
{
Complex num1
=
new
Complex(
12
,
3
);
Complex num2
=
new
Complex(
16
,
4
);
//
overload
Complex sum
=
num1
+
num2;
Complex sum1
=
num1
-
num2;
Complex sum2
=
num1
*
num2;
Complex sum3
=
num1
/
num2;
//
implicit
double
dd
=
sum;
//
explicit
byte
b
=
3
;
Digit d
=
(Digit)b;
//
implicit
Digit digit
=
new
Digit(
3
);
byte
bb
=
digit;
//
Display
System.Console.WriteLine(
"
complex number: {0}
"
, sum);
System.Console.WriteLine(
"
complex number: {0}
"
, sum1);
System.Console.WriteLine(
"
complex number: {0}
"
, sum2);
System.Console.WriteLine(
"
complex number: {0}
"
, sum3);
System.Console.WriteLine(
"
complex number: {0}
"
, dd);
System.Console.WriteLine(
"
complex number: {0}
"
, d.GetType());
System.Console.WriteLine(
"
complex number: {0}
"
, bb);
}
}
public
struct
Complex
{
int
real;
int
imaginary;
public
Complex(
int
real,
int
imaginary)
{
this
.real
=
real;
this
.imaginary
=
imaginary;
}
//
重载 + - * /
public
static
Complex
operator
+
(Complex c1, Complex c2)
{
return
new
Complex(c1.real
+
c2.real, c1.imaginary
+
c2.imaginary);
}
public
static
Complex
operator
-
(Complex c1, Complex c2)
{
return
new
Complex(c1.real
-
c2.real, c1.imaginary
-
c2.imaginary);
}
public
static
Complex
operator
*
(Complex c1, Complex c2)
{
return
new
Complex(c1.real
*
c2.real, c1.imaginary
*
c2.imaginary);
}
public
static
Complex
operator
/
(Complex c1, Complex c2)
{
return
new
Complex(c1.real
/
c2.real, c1.imaginary
/
c2.imaginary);
}
//
隐式转换运算符
public
static
implicit
operator
double
(Complex cc)
{
return
(
double
)cc.real
/
cc.imaginary;
}
public
override
string
ToString()
{
return
(String.Format(
"
{0}+{1}i
"
, real, imaginary));
}
}
struct
Digit
{
byte
value;
public
Digit(
byte
value)
//
constructor
{
if
(value
>
9
)
{
throw
new
System.ArgumentException();
}
this
.value
=
value;
}
//
显式转换运算符
//
此运算符将类型 Byte 转换为称为 Digit 的值类型。
//
*由于不是所有字节都可以转换为数字,因此转换是显式的,这意味着必须使用强制转换
public
static
explicit
operator
Digit(
byte
b)
{
Digit d
=
new
Digit(b);
System.Console.WriteLine(
"
conversion occurred
"
);
return
d;
}
//
隐式转换运算符
//
由于任何数字都可以转换为 Byte,因此没有必要一定让用户知道进行的转换
public
static
implicit
operator
byte
(Digit d)
{
return
d.value;
}
}
有关属性:
如何访问基类中被派生类中具有同一名称的另一个属性隐藏的属性。
public
class
Employee
{
private
string
name;
public
string
Name 《《《
----
{
get
{
return
name; }
set
{ name
=
value; }
}
}
public
class
Manager : Employee
{
private
string
name;
public
new
string
Name 《《《
----
{
get
{
return
name; }
set
{ name
=
value; }
}
}
sealed
class
UserProperty
{
public
void
execute()
{
Manager m
=
new
Manager();
m.Name
=
"
RuiLei
"
;
((Employee)m).Name
=
"
Lei
"
;
//
Base Name
System.Console.WriteLine(
"
Name in the derived class is: {0}
"
, m.Name);
System.Console.WriteLine(
"
Name in the base class is: {0}
"
, ((Employee)m).Name);
}
}
比较有意思
复制构造函数:
static
void
Main(
string
[] args)
{
Instance person
=
new
Instance(
"
George
"
,
40
);
//
浅度赋值
Instance personFleet
=
person;
//
深度赋值
//
类是没有Clone方法的,需要继承ICloneable
//
Instance personDeep = (Instance)person.Clone();
System.Console.WriteLine(personFleet.Details);
//
System.Console.WriteLine(personDeep.Details);
person.name
=
"
AAA
"
;
System.Console.WriteLine(personFleet.Details);
//
System.Console.WriteLine(personDeep.Details);
/**/
///
Fleet result:
///
George is 40
///
AAA is 40
///
///
Deep result:
///
George is 40
///
George is 40
}
[Serializable]
public
class
Instance : ICloneable
{
public
string
name;
private
int
age;
//
Copy constructor.
public
Instance(Instance previousPerson)
{
name
=
previousPerson.name;
age
=
previousPerson.age;
}
//
Instance constructor.
public
Instance(
string
name,
int
age)
{
this
.name
=
name;
this
.age
=
age;
}
//
Get accessor.
public
string
Details
{
get
{
return
name
+
"
is
"
+
age.ToString();
}
}
ICloneable Members
#region
ICloneable Members
public
object
Clone()
{
return
base
.MemberwiseClone();
}
#endregion
}
查看全文
相关阅读:
git常用命令
Laravel框架数据库CURD操作、连贯操作总结
Laravel 5 系列入门教程(四)【最适合中国人的 Laravel 教程】【完结】
Laravel 5 系列入门教程(三)【最适合中国人的 Laravel 教程】
Laravel 5 系列入门教程(二)【最适合中国人的 Laravel 教程】
Laravel 5 系列入门教程(一)【最适合中国人的 Laravel 教程】
linux环境下安装nginx步骤
关于C++中的虚拟继承的一些总结
C++中拷贝构造函数
C++之类与对象(3)
原文地址:https://www.cnblogs.com/RuiLei/p/679983.html
最新文章
keras实现textcnn
推送问题
数组排序
根据时间删除sqlite3中数据
xib中拖的scrollview
mjrefresh 刷新后上移
MAC 命令行 查看硬盘和文件夹大小
mac安装使用redis
go grpc入门
mac安装xdebug调试php
热门文章
php运用PhpAmqpLib操作队列
rabbitmq一些常用命令
php word转html(可能样式会有问题 还没有空细究)
设置跨域后 options返回状态405
centos安装使用elasticsearch
php7.3安装使用rabbitMq
前端神器avalonJS入门(二)
前端神器avalonJS入门(一)
css 实现div阴影,上下移动
Git 常用命令速查表(图文+表格)
Copyright © 2011-2022 走看看