惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - Jason Li 2011

Maven - import a web project into eclipse [分享] 兰迪·波许教授的最后一课[PDF/PPT/AVI] 转:Controlling Access to Members of a Class 转:退火算法 Simulate Anneal Arithmetic (SAA,模拟退火算法) Scrum in practise 转:MOSS 2007 and Code Access Security 转:CollaDec FriendlyQuery类库(beta)说明 转:MOSS站点的迁移(备份还原) 转:MOSS漫游(3):说说MOSS中的母版页 转:关于MOSS 2007的Content Types 转:Best Practices: Common Coding Issues When Using the SharePoint Object Model 转:Best Practices: Using Disposable Windows SharePoint Services Objects 转:Free the SPWeb 转:将你的Asp.NET应用程序嵌入到SharePoint Duff International Sites 项目总结 转:Code-blocks are not allowed in this file: Using Server-Side Code with SharePoint 转:ASP.NET Web Services or .NET Remoting: How to Choose 转:SharePoint Server 2007 页面模型 转:MOSS 2007基础:WSS 3.0 中的母版页(Master Pages)和内容页(Content Pages) 转:How to: Create a Minimal Master Page
转:类与结构的差别
Jason Li 2011 · 2010-12-29 · via 博客园 - Jason Li 2011

目录

类与结构的实例比较

类与结构的差别

如何选择结构还是类 

一.类与结构的示例比较:

结构示例:

public struct person

{

string name;

int height;

int weight

public bool overweight()

{

//implement something

}

}

类示例:

public class testtime

{

int hours;

int minutes;

int seconds;

public void passtime()

{

//implementation of behavior

}

}

调用过程:

public class test

{

public static ovid main

{

person myperson=new person //声明结构

testtime mytime=new testtime //声明类

}

}

从上面的例子中我们可以看到,类的声明和结构的声明非常类似,只是限定符后面是 struct 还是 class 的区别,而且使用时,定义新的结构和定义新的类的方法也非常类似。那么类和结构的具体区别是什么呢?

.类与结构的差别

1. 值类型与引用类型

结构是值类型:值类型在堆栈上分配地址,所有的基类型都是结构类型,例如:int 对应system.int32 结构,string 对应 system.string 结构 ,通过使用结构可以创建更多的值类型

类是引用类型:引用类型在堆上分配地址

堆栈的执行效率要比堆的执行效率高,可是堆栈的资源有限,不适合处理大的逻辑复杂的对象。所以结构处理作为基类型对待的小对象,而类处理某个商业逻辑

因为结构是值类型所以结构之间的赋值可以创建新的结构,而类是引用类型,类之间的赋值只是复制引用

注:

1.虽然结构与类的类型不一样,可是他们的基类型都是对象(object,c#中所有类型的基类型都是object

2.虽然结构的初始化也使用了new 操作符可是结构对象依然分配在堆栈上而不是堆上,如果不使用“新建”(new),那么在初始化所有字段之前,字段将保持未赋值状态,且对象不可用

2.继承性

结构:不能从另外一个结构或者类继承,本身也不能被继承,虽然结构没有明确的用sealed声明,可是结构是隐式的sealed .

类:完全可扩展的,除非显示的声明sealed 否则类可以继承其他类和接口,自身也能被继承

注:虽然结构不能被继承 可是结构能够继承接口,方法和类继承接口一样

例如:结构实现接口

interface iimage

{

void paint();

}

struct picture : iimage

{

public void paint()

{

// painting code goes here

}

private int x, y, z; // other struct members

}

3.内部结构:

结构:

没有默认的构造函数,但是可以添加构造函数

没有析构函数

没有 abstract sealed(因为不能继承)

不能有protected 修饰符

可以不使用new 初始化

在结构中初始化实例字段是错误的

类:

有默认的构造函数

有析构函数

可以使用 abstract sealed

protected 修饰符

必须使用new 初始化

三.如何选择结构还是类

讨论了结构与类的相同之处和差别之后,下面讨论如何选择使用结构还是类:

1 堆栈的空间有限,对于大量的逻辑的对象,创建类要比创建结构好一些

2 结构表示如点、矩形和颜色这样的轻量对象,例如,如果声明一个含有 1000 个点对象的数组,则将为引用每个对象分配附加的内存。在此情况下,结构的成本较低。

3 在表现抽象和多级别的对象层次时,类是最好的选择

4大多数情况下该类型只是一些数据时,结构时最佳的选择

posted on 2010-12-29 22:32  Jason Li 2011  阅读(175)  评论()    收藏  举报