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
}
查看全文
相关阅读:
15、常量指针和指针常量区别?
14、strlen和sizeof区别?
12、变量声明和定义区别?
10、宏定义和函数和typedef有何区别?
hdoj--1495--非常可乐(搜索+隐式图)
hdoj--2579--Dating with girls(2)(搜索+三维标记)
poj--3630--Phone List(字典树+前缀判断)
poj--2001--Shortest Prefixes(字典树)
Huatuo's Medicine
hdoj--2803--The MAX(水题)
原文地址:https://www.cnblogs.com/RuiLei/p/679983.html
最新文章
Min Stack
Reverse Integer
Count and Say
Excel Sheet Column Title
zabbix api 批量添加主机(python3 requests)
zabbix api的使用
CCIE SP-EIGRP 小结
端口处于err-disabled
RTBH
BGP总结
热门文章
BGP三种汇总方法
BGP同步
BGP 13条选路原则
BGP Dampening
20、C和C++的区别
19、迭代器失效的情况
13、哪几种情况必须用到初始化成员列表?
18、野指针和悬空指针
17、数组名和指针(这里为指向数组首元素的指针)区别?
16、a和&a有什么区别?
Copyright © 2011-2022 走看看