结构与类 [英] Structs vs. Classes

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

问题描述

我对结构和类之间的区别感到困惑吗?

有时候,我只想要一组字段一起去。没有方法的类

似乎是错误的,因为它带来了太多的开销(我认为是b $ b)。结构似乎更合适。至少这是我在其他语言中使用的
。但是因为一个Struct *可以*持有方法,

如果我保存任何东西我会徘徊。如果不是,为什么要使用它?

I''m really confused by the difference between a Struct and a Class?
Sometimes, I want just a group of fields to go together. A Class
without methods seems wrong, in that it carries too much overhead (I
think). A Struct seems more appropriate. At least it is what I would
have used in other languages. But since a Struct *can* hold methods,
I wander if I am saving anything. If not, why use it?

推荐答案



主要区别在于类是由参考,但结构

按值传递,如String类型。

所以类比结构更快..


再见,

迭戈

Hi,
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
So Classes are faster than Structures..

Bye,
Diego


好吧,好吧。但我不得不问,什么是结构的正确使用?

我会用它而不是类吗?


5月5日,7日:08 pm,Diego Jancic< jan ... @ gmail.comwrote:
Well, fine. But I have to ask, what is the proper use of a Struct?
Would I ever use it instead of a Class?

On May 5, 7:08 pm, Diego Jancic <jan...@gmail.comwrote:



主要区别在于班级是通过引用,但结构

按值传递,如String类型。

所以类比结构更快..


再见,

迭戈
Hi,
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
So Classes are faster than Structures..

Bye,
Diego



Diego Jancic写道:
Diego Jancic wrote:



主要区别在于类是通过引用,但结构

是按值传递的,就像字符串类型一样。

所以类比结构更快..
Hi,
The main difference is that Classes are by reference, but the structs
are passed by value, like the String type.
So Classes are faster than Structures..



Eugh。

这取决于你在做什么。考虑一下为什么通过

值通过,作为一个极端的例子。


Alun Harford

Eugh.
It depends on what you''re doing. Think about why ints are passed by
value, as an extreme example.

Alun Harford


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

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