(原創) 一旦為virtaul,則永世為virtual (C/C++) (.NET) (C#)
Abstract
若abstract base class將某個function設為virtual function後,則該function永世都為virtual function。
Introduction
我以前以為一個virtual function,只要繼承體系中某個class不希望其為virtual,只要不下virtual keyword即可,但這個觀念是錯誤的!!
UML
C++
1/*
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : VirtualAlways.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo virtual feature
7Release : 05/15/2007 1.0
8*/
9#include <iostream>
10
11using namespace std;
12
13class AbstractBase {
14public:
15 virtual void func() = 0;
16};
17
18class Derived1st : public AbstractBase {
19public:
20 void func() {
21 cout << "Derived1st's func" << endl;
22 }
23};
24
25class Derived2nd : public Derived1st {
26public:
27 void func() {
28 cout << "Derived2nd's func" << endl;
29 }
30};
31
32int main() {
33 AbstractBase& foo1 = Derived1st();
34 AbstractBase& foo2 = Derived2nd();
35
36 foo1.func();
37 foo2.func();
38}
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : VirtualAlways.cpp
5Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6Description : Demo virtual feature
7Release : 05/15/2007 1.0
8*/
9#include <iostream>
10
11using namespace std;
12
13class AbstractBase {
14public:
15 virtual void func() = 0;
16};
17
18class Derived1st : public AbstractBase {
19public:
20 void func() {
21 cout << "Derived1st's func" << endl;
22 }
23};
24
25class Derived2nd : public Derived1st {
26public:
27 void func() {
28 cout << "Derived2nd's func" << endl;
29 }
30};
31
32int main() {
33 AbstractBase& foo1 = Derived1st();
34 AbstractBase& foo2 = Derived2nd();
35
36 foo1.func();
37 foo2.func();
38}
執行結果
Derived1st's func
Derived2nd's func
Derived2nd's func
儘管20行
void func() {
cout << "Derived1st's func" << endl;
}
cout << "Derived1st's func" << endl;
}
已經沒加virtual了
但27行
void func() {
cout << "Derived2nd's func" << endl;
}
cout << "Derived2nd's func" << endl;
}
仍然可以繼續override,且結果也顯示override成功,這顯示了,一旦為virual function,則永世為virtual function。事實上,在UML也反映出這個原則,一個斜體的virtual function被繼承時,到了derived class仍是斜體。
C#雖然語法稍微不同,但原則仍然相同。
C#
1/*
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : AbstractAlways.cs
5Compiler : Visual Studio 2005 / C# 2.0
6Description : Demo abstract feature
7Release : 05/15/2007 1.0
8*/
9using System;
10
11abstract class AbstractBase {
12 public abstract void func();
13}
14
15class Derived1st : AbstractBase {
16 override public void func() {
17 Console.WriteLine("Derived1st's func");
18 }
19}
20
21class Derived2nd : Derived1st {
22 override public void func() {
23 Console.WriteLine("Derived2nd's func");
24 }
25}
26
27class main {
28 public static void Main() {
29 AbstractBase foo1 = new Derived1st();
30 AbstractBase foo2 = new Derived2nd();
31
32 foo1.func();
33 foo2.func();
34 }
35}
2(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4Filename : AbstractAlways.cs
5Compiler : Visual Studio 2005 / C# 2.0
6Description : Demo abstract feature
7Release : 05/15/2007 1.0
8*/
9using System;
10
11abstract class AbstractBase {
12 public abstract void func();
13}
14
15class Derived1st : AbstractBase {
16 override public void func() {
17 Console.WriteLine("Derived1st's func");
18 }
19}
20
21class Derived2nd : Derived1st {
22 override public void func() {
23 Console.WriteLine("Derived2nd's func");
24 }
25}
26
27class main {
28 public static void Main() {
29 AbstractBase foo1 = new Derived1st();
30 AbstractBase foo2 = new Derived2nd();
31
32 foo1.func();
33 foo2.func();
34 }
35}
執行結果
Derived1st's func
Derived2nd's func
Derived2nd's func
11行
abstract class AbstractBase {
public abstract void func();
}
public abstract void func();
}
C#的abstract,就是C++的pure virtual,且必須在class加上abstract。
16行
override public void func() {
Console.WriteLine("Derived1st's func");
}
Console.WriteLine("Derived1st's func");
}
C#則規定一定要加上override,C++則不用。
Conclusion
C++和C#僅是語法上的小差異,但精神相同。