Difference between Class and Structure in PHP and Java

对着背影说爱祢 提交于 2020-01-03 05:13:17

问题


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


回答1:


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.




回答2:


This is entirely language dependent, so there can be no single correct answer. If you are interested in a specific language, please specify it in your question.




回答3:


From an OO perspective, there are no difference. They are both types that have a public API with methods (and properties if your language supports them).

From a technical standpoint, there can be many differences, but that depends on the language and/or platform.

When it comes to OO design, I simply choose to ignore that such a thing as a struct exists at all as it gives me no additional capabilities or features. As we dive deeper into the implementation, a class may turn out to be better implemented as a struct, but it's a pure implementation detail.




回答4:


Difference between Structure and Classes

-The major difference between class and structure is that the declaration of structure starts with the keyword 'struct' whereas on the other hand, a class starts with the keyword 'class'. -In class the data member and member are private by default whereas in structure they are public by default. -Data hiding is supported in classes but not in structure. -Structure deals with variables only whereas objects deal with real-world objects.

-If we explicitly specify the access type of each member, then a structure will behave exactly as a class.



来源:https://stackoverflow.com/questions/1920849/difference-between-class-and-structure-in-php-and-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!