PHP和Java中的类和结构之间的区别 [英] Difference between Class and Structure in PHP and Java

查看:132
本文介绍了PHP和Java中的类和结构之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当处理面向对象编程时,类和结构之间的真正区别是什么。



有些人说只有一个区别:
结构成员默认为public,Class成员默认为私有。



有人说有很多区别。 b $ b

阅读了许多文章和论坛后,我有以下区别:



将DEFAULT类别设为私人会员。结构为DEFAULT以拥有公共成员。



结构是值类型。
类是引用类型。



结构通过堆栈存储在内存中。
通过堆存储在内存中的类。



结构不支持继承。
类支持继承。



构造函数以不同的方式工作。



'new'不同的方式。



为结构分配内存非常快,因为这是内联或堆栈。



您对我上面的列表有什么意见,或者您有不同的意见。感谢

解决方案

这是非常明确的语言。你似乎混合了C ++和C#的公平份额,两者都是非常不同的语言(尽管语法表面上相似)。



在C ++中 struct 确实默认为 public 成员可见性,而默认为 private 。在C# struct 用于声明通过值传递的值类型请注意,堆栈分配是一个实现细节, a>)。



一般来说,两种语言对于 struct class 代表: struct 用于简单的数据结构,和操纵它的方法。它们用于以某种具体或抽象的意义构建对象,而数据结构只是:以结构化形式的数据;他们不需要做那么多的数据,甚至知道什么数据。基本上他们是愚蠢的,不在乎。



但这只是语言设计师认为应该使用它们。人们善于误用东西,所以不是每个 struct 你看到可能是一个简单的,愚蠢的数据结构,而不是每个 class 你看到可能是一个完整的类有很多方法和whatnot。这只是一个约定,如果人们跟着它,其他人可以看看代码,看到哦,好,这是一个 struct 所以我不期望很多逻辑,到更有趣的事情。

ETA:因为你在评论中提到你对PHP或Java特别感兴趣:两者语言在 class struct 的语法或语言级别上有 这就是为什么你的问题打我有点奇怪。在Java和PHP中,无论是只有没有逻辑的数据结构还是具有所有内容的实际类,都可以将事物建模为类。


What is real difference between Class and Structure when you are dealing with Object Oriented Programming. This question is asked many times during my interviews for SE.

Some people says that there is only one difference: Structure members are public by default and Class members are private by default.

Some says there are many differences.

After reading many articles and forums, I have the following differences:

Classes DEFAULT to having private members. Structures DEFAULT to having public members.

Structures are values type. Classes are reference type.

Structure stores in memory via stack. Classes stored in memory via heap.

Structure doesn’t support inheritance. Classes support inheritance.

Constructor works in different way.

‘new’ operator works in different way.

Allocating memory for structure is very fast because this takes place inline or on the stack.

What are your opinion on my above list or you have a different one. Thanks

解决方案

This is pretty language-specific. You seem to be mixing a fair share of both C++ and C#, both of which are very different languages (despite superficial similarities in syntax).

In C++ structs indeed default to public member visibility while class defaults to private. In C# struct is used to declare value types which are passed by value (note that the stack allocation is an implementation detail, not a contract).

Generally both languages seem to have the same idea of what struct and class should represent: struct is for simple data structures which do little more than holding data, while classes have state and methods to manipulate it. They are used to build objects in some concrete or abstract sense while data structures are just that: data in a structured form; they don't need to do much with that data or even know what data that is. Essentially they're dumb and don't care.

But that's just how the language designers thought they should be used. People are good at mis-using things so not every struct you see may be a simple, dumb data structure and not every class you see may be a full-blown class with lots of methods and whatnot. It's merely a convention and if people follow it others can look at the code and see "Oh, nice, that's a struct so I don't expect much logic here and move on to more interesting things." It might work ... in theory.

ETA: Since you mentioned in a comment that you are particularly interested in PHP or Java: Both languages do not have any distinction at the syntax or language level of class or struct which is why your question strikes me as a little odd. In both Java and PHP you model things as classes, regardless of whether they are just data structures without logic or actual classes with everything there is.

这篇关于PHP和Java中的类和结构之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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