Created
December 19, 2017 14:52
-
-
Save luchenqun/38858812550a273e2e19ef80e6ad6f14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class BirdType //鸟类 | |
{ | |
}; | |
class InsectType //昆虫类 | |
{ | |
}; | |
class BeastType //走兽类 | |
{ | |
}; | |
template <typename A> | |
class Traits | |
{ | |
}; | |
template <> | |
class Traits<BirdType> | |
{ | |
public: | |
void fly() | |
{ | |
cout << "Birder fly " << endl; | |
} | |
}; | |
template <> | |
class Traits<InsectType> | |
{ | |
public: | |
void fly() | |
{ | |
cout << "Insect fly " << endl; | |
} | |
}; | |
template <> | |
class Traits<BeastType> | |
{ | |
public: | |
void fly() | |
{ | |
cout << "Beast can't fly " << endl; | |
} | |
}; | |
template <typename T> | |
class Animal | |
{ | |
public: | |
void Fly() | |
{ | |
Traits<T>().fly(); | |
}; | |
}; | |
int main() | |
{ | |
Animal<BirdType> brider; | |
brider.Fly(); | |
Animal<InsectType> insect; | |
insect.Fly(); | |
Animal<BeastType> beast; | |
beast.Fly(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment