struct和class最本质的区别是:struct是值类型,而class则是引用类型;struct继承自System.ValueType类,class继承自System.Object类
struct一般用于存储数据,值保存于堆栈中,class则是面向对象一个重要概念,着重体现行为,当new 一个实例的时候,对象保存的是实际数据的引用地址,而对象实际数据则保存于堆中
struct不能继承另一个结构,也不能从class继承,没有继承特性,结构的成员不能用protected修饰符,但是struct可以继承接口;class则既可以从类继承,也可以从
接口继承,但不可以从struct继承,也不能继承自多个类,但可以继承自多个接口;struct 可以重载System.Object类的ToString(),Equals(),GetHashCode()方法
struct不能自定义的显式声明的无参构造函数,但具有默认的无参构造函数,功能就是将成员初始化为0的等价值
结构还可以包含构造函数、常量、字段、方法、属性、索引器、运算符、事件和嵌套类型,但如果同时需要上述几种成员,则应当考虑改为使用类作为类型;struct通常用来封装小型相关变量组.
Demo:
interface IBook
{
void ShowBook();
}
public struct Book:IBook
{
public string _title;
public string _author;
public decimal _price;
public bool _type;
//public Book()
//{
//}
public Book(string title, string author, decimal price,bool type)// 在控制离开构造函数之前,字段“struct_class.Book._type”必须完全赋值
{
this._title = title;
this._author = author;
this._price = price;
this._type = type;
}
public string Title
{
get
{
return this._title;
}
}
public string Author
{
get
{
return this._author;
}
}
public decimal Price
{
get
{
return this._price;
}
}
public bool Type
{
get
{
return this._type;
}
}
public void ShowBook()
{
if (Type)
{
Console.WriteLine("This is {0} and its author is {1} and its price is {2}.It is an english book",this.Title,this.Author,this.Price);
}
else
{
Console.WriteLine("This is {0} and its author is {1} and its price is {2}.It is an chinese book", this.Title, this.Author, this.Price);
}
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override string ToString()
{
return base.ToString();
}
}
static void Main(string[] args)
{
//Book b = new Book();
Book b = new Book("Love", "zhangxi24",(decimal)20.5, true);
b.ShowBook();
}
struct一般用于存储数据,值保存于堆栈中,class则是面向对象一个重要概念,着重体现行为,当new 一个实例的时候,对象保存的是实际数据的引用地址,而对象实际数据则保存于堆中
struct不能继承另一个结构,也不能从class继承,没有继承特性,结构的成员不能用protected修饰符,但是struct可以继承接口;class则既可以从类继承,也可以从
接口继承,但不可以从struct继承,也不能继承自多个类,但可以继承自多个接口;struct 可以重载System.Object类的ToString(),Equals(),GetHashCode()方法
struct不能自定义的显式声明的无参构造函数,但具有默认的无参构造函数,功能就是将成员初始化为0的等价值
结构还可以包含构造函数、常量、字段、方法、属性、索引器、运算符、事件和嵌套类型,但如果同时需要上述几种成员,则应当考虑改为使用类作为类型;struct通常用来封装小型相关变量组.
Demo:
interface IBook
{
void ShowBook();
}
public struct Book:IBook
{
public string _title;
public string _author;
public decimal _price;
public bool _type;
//public Book()
//{
//}
public Book(string title, string author, decimal price,bool type)// 在控制离开构造函数之前,字段“struct_class.Book._type”必须完全赋值
{
this._title = title;
this._author = author;
this._price = price;
this._type = type;
}
public string Title
{
get
{
return this._title;
}
}
public string Author
{
get
{
return this._author;
}
}
public decimal Price
{
get
{
return this._price;
}
}
public bool Type
{
get
{
return this._type;
}
}
public void ShowBook()
{
if (Type)
{
Console.WriteLine("This is {0} and its author is {1} and its price is {2}.It is an english book",this.Title,this.Author,this.Price);
}
else
{
Console.WriteLine("This is {0} and its author is {1} and its price is {2}.It is an chinese book", this.Title, this.Author, this.Price);
}
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override string ToString()
{
return base.ToString();
}
}
static void Main(string[] args)
{
//Book b = new Book();
Book b = new Book("Love", "zhangxi24",(decimal)20.5, true);
b.ShowBook();
}
浙公网安备 33010602011771号