Home
07 运算符重载
Unity
07 运算符重载
姜睿
姜睿
August 22, 2022
1 min

Table Of Contents

01
运算符重载
02
可以被重载的运算符
03
自增自减运算符重载
04
布尔值的重载

运算符重载

  • 运算符重载只能运用在类或结构中。
  • 关键字为 public static

示例

1
2internal class Item
3{
4public int Count { get; set; }
5
6public static List<Item> operator +(Item a, Item b)
7{
8// item counts
9int count = a.Count + b.Count;
10// declare a list to store items
11List<Item> items = new List<Item>();
12
13while (count > 0)
14{
15if (count < 64)
16{
17items.Add(new Item(count));
18count = 0;
19}
20else
21{
22items.Add(new Item(64));
23count -= 64;
24}
25}
26return items;
27}
28
29public Item(int count = 0)
30{
31Count = count;
32}
33}
34
35internal class Program
36{
37private static void Main(string[] args)
38{
39Item a = new Item(64);
40Item b = new Item(72);
41
42foreach (Item item in a + b)
43{
44Console.WriteLine(item.Count);
45}
46}
47}
48

可以被重载的运算符

  • 一元运算符:+、-、!、~、++、--、true、false
  • 二元运算符:+、-、*、/、%、&、|、^、<<、>>、==、!=、>、<、>=、<=
  • 不能重载赋值 (=) 运算符

自增自减运算符重载

类的自增自减重载(引用)

1
2using static System.Console;
3
4internal class Class
5{
6public int Count { get; set; }
7
8public static Class operator ++(Class c)
9{
10return new Class(c.Count + 1);
11}
12
13public Class(int count = 0)
14{
15Count = count;
16}
17}
18
19internal class Program
20{
21static void Display(string text, Class c)
22{
23WriteLine($"{text}: {c.Count}");
24}
25
26private static void Main(string[] args)
27{
28Class a = new Class(10);
29WriteLine("Postfix");
30Display("Return", a++); // 10
31
32WriteLine();
33
34WriteLine("Prefix");
35a = new Class(10);
36Display("Return", ++a); // 11
37}
38}
39

后缀自增步骤

1
2Class origin = new Class(1);
3Class postfix;
4// TODO: Class postfix = origin++;
5// temp as a ref of origin
6// orgin address = 0x00000000
7// temp address = orgin address
8Class temp = origin;
9// Error: origin.Count += 1;
10// orgin address = 0x11111111
11origin = new Class(origin.Count + 1);
12// temp address = 0x00000000
13// postfix address = temp address
14postfix = temp;
15Display("Origin", origin); // 2
16Display("Postfix", postfix); // 1
17

前缀自增步骤

1
2Class origin = new Class(1);
3origin = new Class(origin.Count + 1);
4Class temp = origin;
5// TODO: Class prefix = origin++;
6Class prefix = temp;
7Display("Origin", origin); // 2
8Display("prefix", prefix); // 2
9

结构体的自增自减重载(值)

1
2using static System.Console;
3
4internal struct Struct
5{
6public int Count { get; set; }
7
8public static Struct operator ++(Struct c)
9{
10c.Count++;
11return c;
12}
13
14public Struct(int count = 0)
15{
16Count = count;
17}
18}
19
20internal class Program
21{
22static void Display(string text, Struct c)
23{
24WriteLine($"{text}: {c.Count}");
25}
26
27private static void Main(string[] args)
28{
29Struct a = new Struct(10);
30WriteLine("Postfix");
31Display("Return", a++); // 10
32
33WriteLine();
34
35WriteLine("Prefix");
36a = new Struct(10);
37Display("Return", ++a); // 11
38}
39}
40

布尔值的重载

  • truefalse 必须同时被定义。
1
2using static System.Console;
3
4internal class Class
5{
6public int Count { get; set; }
7
8public static bool operator true(Class c)
9{
10return c.Count == 64;
11}
12
13public static bool operator false(Class c)
14{
15return c.Count != 64;
16}
17
18public Class(int count = 0)
19{
20Count = count;
21}
22}
23
24internal class Program
25{
26private static void Main(string[] args)
27{
28Class twelveIs64 = new Class(12);
29if (twelveIs64)
30WriteLine("Full");
31else
32WriteLine("Good");
33}
34}
35

Tags

#game develop
姜睿

姜睿

学生

游戏设计学生

Expertise

游戏开发
平面设计

Related Posts

策略模式
策略模式
November 19, 2023
1 min

Legal Stuff

Privacy NoticeCookie PolicyTerms Of Use