Home
04 继承 & 成员
Unity
04 继承 & 成员
姜睿
姜睿
August 19, 2022
1 min

Table Of Contents

01
屏蔽基类成员
02
基类访问
03
虚方法与覆写方法
04
多重继承中的屏蔽与覆写

屏蔽基类成员

  • 使用 new 关键字屏蔽(mask)继承自基类的成员。
  • 要求:
    • 与基类成员类型相同,并且名字相同。
    • 具有相同的签名,即名称和参数列表,不包含返回值。
1
2internal class Animal
3{
4public void Greet()
5{
6Console.WriteLine("I'm an Animal.");
7}
8}
9
10internal class Duck : Animal
11{
12new public void Greet()
13{
14Console.WriteLine("I'm a Duck.");
15}
16}
17
18internal class Program
19{
20private static void Main(string[] args)
21{
22Animal animal = new Animal();
23Duck duck = new Duck();
24
25animal.Greet(); // I'm an Animal.
26duck.Greet();   // I'm an Duck.
27}
28}
29

基类访问

1
2internal class Entity
3{
4protected string name = "Arrow";
5protected int amount = 64;
6public void PrintInfo()
7{
8Console.WriteLine("Name: {0}", name);
9Console.WriteLine("Amount: {0}", amount);
10}
11}
12
13internal class Arrow : Entity
14{
15private bool isCritical = false;
16new public void PrintInfo()
17{
18base.PrintInfo();
19Console.WriteLine("IsCritical: {0}", isCritical);
20}
21}
22
23internal class Program
24{
25private static void Main(string[] args)
26{
27Arrow arrow = new Arrow();
28arrow.PrintInfo();
29}
30} 
31

虚方法与覆写方法

  • 基类方法使用 virtual 关键字,派生类方法使用 override 关键字。
    • 覆写和被覆写的方法需要具有相同的访问性。
  • new 关键字不同,当基类的引用调用方法时,将执行派生类的方法。(见例一,对比例二)

例一

1
2internal class Base
3{
4virtual public void PrintInfo()
5{
6Console.WriteLine("@Base");
7}
8}
9
10internal class Derived : Base
11{
12override public void PrintInfo()
13{
14Console.WriteLine("@Derived");
15}
16}
17
18internal class Program
19{
20private static void Main(string[] args)
21{
22Derived derived = new Derived();
23derived.PrintInfo(); // @Derived
24Base derivedToBase = derived;
25derivedToBase.PrintInfo(); // @Derived
26}
27} 
28

例二

1
2internal class Base
3{
4public void PrintInfo()
5{
6Console.WriteLine("@Base");
7}
8}
9
10internal class Derived : Base
11{
12new public void PrintInfo()
13{
14Console.WriteLine("@Derived");
15}
16}
17
18internal class Program
19{
20private static void Main(string[] args)
21{
22Derived derived = new Derived();
23derived.PrintInfo(); // @Derived
24Base derivedToBase = derived;
25derivedToBase.PrintInfo(); // @Base
26}
27}
28

多重继承中的屏蔽与覆写

例一

1
2internal class Base
3{
4public void PrintInfo()
5{
6Console.WriteLine("@Base");
7}
8}
9
10internal class Derived : Base
11{
12new public void PrintInfo()
13{
14Console.WriteLine("@Derived");
15}
16}
17
18internal class SecondDerived : Derived
19{
20new public void PrintInfo()
21{
22Console.WriteLine("@Derived#2");
23}
24}
25
26internal class Program
27{
28private static void Main(string[] args)
29{
30SecondDerived secondDerived = new SecondDerived();
31secondDerived.PrintInfo(); // @Derived#2
32Derived derived = secondDerived;
33derived.PrintInfo(); // @Derived
34Base derivedToBase = derived;
35derivedToBase.PrintInfo(); // @Base
36}
37}
38

例二

1
2internal class Base
3{
4virtual public void PrintInfo()
5{
6Console.WriteLine("@Base");
7}
8}
9
10internal class Derived : Base
11{
12override public void PrintInfo()
13{
14Console.WriteLine("@Derived");
15}
16}
17
18internal class SecondDerived : Derived
19{
20new public void PrintInfo()
21{
22Console.WriteLine("@Derived#2");
23}
24}
25
26internal class Program
27{
28private static void Main(string[] args)
29{
30SecondDerived secondDerived = new SecondDerived();
31secondDerived.PrintInfo(); // @Derived#2
32Derived derived = secondDerived;
33derived.PrintInfo(); // @Derived
34Base derivedToBase = derived;
35derivedToBase.PrintInfo(); // @Derived
36}
37}
38

Tags

#game developc#
姜睿

姜睿

学生

游戏设计学生

Expertise

游戏开发
平面设计

Related Posts

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

Legal Stuff

Privacy NoticeCookie PolicyTerms Of Use