问题描述
这是真正的声明?
写作面向对象code,即使在非面向对象的语言。
有谁能够现场一个例子..或者提供一些链接...
Can anybody site an example.. or provide some links...
推荐答案
是的,你能做到这一点,你可以在一个程序模拟例如继承和多态性 OOP概念语言如C
Yes, you can do that, You can simulate OOP concepts like Inheritance and Polymorphism in a procedural language like c.
既然你问一个code为例,这里有一个:
Since you ask for a Code Example, Here is one:
要模拟继承所有你需要的是一个结构的第一个成员是超类的一个实例,然后你可以投三分球周围的立足和派生类就像C ++继承。
To simulate Inheritance all you need is the first member of a structure be an instance of the superclass, and then you can cast around pointers to base and derived classes just like C++ inheritance.
struct BaseClass
{
//...
};
struct DerivedClass
{
struct BaseClass super;
//....
};
struct DerivedClass d;
//UpCasting example
struct BaseClass *base_ptr = (struct BaseClass *)&d;
//Downclasting Example
struct DerivedClass *derived_ptr = (struct DerivedClass *)base_ptr;
Ofcourse,如果你真的需要OOP你应该使用OOP语言,而不是围绕这样打。
Ofcourse, If you really need OOP you should use a OOP language rather than playing around this way.
这篇关于面向对象的code,在非面向对象语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!