面向对象的代码,面向非面向对象的语言 [英] Object oriented code,in non-object oriented language

查看:168
本文介绍了面向对象的代码,面向非面向对象的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个语句是真的吗


编写面向对象的代码,即使是面向非面向对象的语言。


任何人都可以站点一个例子..或提供一些链接...

解决方案

是的,你可以这样做,你可以像c这样的过程语言模拟继承多态性之类的OOP概念。



由于你要求一个代码示例,这里是一个:



要模拟继承,你需要的是结构的第一个成员是

  struct BaseClass 
{
// ...
};
struct DerivedClass
{
struct BaseClass super;
// ....
};

struct DerivedClass d;
// UpCasting示例
struct BaseClass * base_ptr =(struct BaseClass *)& d;
//下载示例
struct DerivedClass * derived_ptr =(struct DerivedClass *)base_ptr;

当然,如果你真的需要OOP,你应该使用OOP语言,而不是这样。 / p>

Is this statement true??

Writing object oriented code, even in non-object oriented language.

Can anybody site an example.. or provide some links...

解决方案

Yes, you can do that, You can simulate OOP concepts like Inheritance and Polymorphism in a procedural language like c.

Since you ask for a Code Example, Here is one:

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, If you really need OOP you should use a OOP language rather than playing around this way.

这篇关于面向对象的代码,面向非面向对象的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆