- Game Development Patterns and Best Practices
- John P. Doran Matt Casanova
- 303字
- 2021-07-02 23:43:42
Understanding interfaces
An interface implements no functions, but simply declares the methods that the class will support. Then, all of the derived classes will do the implementation. In this way, the developer will have more freedom to implement the functions to fit each instance, while having things work correctly due to the nature of using an object-oriented language.
Interfaces may contain only static final variables, and they may contain only abstract methods, which means that they cannot be implemented within the class. However, we can have interfaces that inherit from other interfaces. When creating theses classes, we can implement whatever number of interfaces we want to. This allows us to make classes become even more polymorphic but, by doing so, we are agreeing that we will implement each of the functions defined in the interface. Because a class that implements an interface extends from that base class, we would say that it has an IS-A relationship with that type.
Now, interfaces have one disadvantage, and that's the fact that they tend to require a lot of coding to implement each of the different versions as needed, but we will talk about ways to adjust and/or fix this issue over the course of this book.
In C++, there isn't an official concept of interfaces, but you can simulate the behavior of interfaces by creating an abstract class.
Here's a simple example of an interface, and an implementation of it:
class Enemy
{
public:
virtual ~Enemy(void) {/*Empty virtual destructor*/}
virtual void DisplayInfo(void) = 0;
virtual void Attack(void) = 0;
virtual void Move(void) = 0;
};
class FakeEnemy: public Enemy
{
public:
virtual void DisplayInfo(void)
{
M5DEBUG_PRINT("I am a FAKE enemy");
}
virtual void Attack(void)
{
M5DEBUG_PRINT("I cannot attack");
}
virtual void Move(void)
{
M5DEBUG_PRINT("I cannot move");
}
};
And here's how it looks in UML:

- 解構(gòu)產(chǎn)品經(jīng)理:互聯(lián)網(wǎng)產(chǎn)品策劃入門(mén)寶典
- 嵌入式軟件系統(tǒng)測(cè)試:基于形式化方法的自動(dòng)化測(cè)試解決方案
- JavaScript修煉之道
- Oracle Exadata性能優(yōu)化
- Web開(kāi)發(fā)的貴族:ASP.NET 3.5+SQL Server 2008
- Python網(wǎng)絡(luò)爬蟲(chóng)從入門(mén)到實(shí)踐(第2版)
- Unity Game Development Scripting
- Java:High-Performance Apps with Java 9
- Spring+Spring MVC+MyBatis從零開(kāi)始學(xué)
- Django Design Patterns and Best Practices
- R語(yǔ)言數(shù)據(jù)挖掘:實(shí)用項(xiàng)目解析
- NGUI for Unity
- HTML5+CSS+JavaScript深入學(xué)習(xí)實(shí)錄
- C語(yǔ)言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)教程
- R語(yǔ)言與網(wǎng)站分析