Java5.0与C#2.0的区别之一:Struct类型

注意:以下如果没有具体说明版本号, Java一律表示Java5.0, C#一律表示C#2.0
 
在Java中没有Struct关键字, 但C#有. 在C#中, Struct与Class的区别有以下几点:
1. Struct是值类型(Value type),但Class是引用类型(reference type), structs在managed stack中被创建, classes在managed heap中被创建
2. Struct不支持继承,但Class支持,所以Struct的成员不能被protected修饰
3. 不能为Struct提供默认构造函数和析构函数
4. Struct的实例成员不能在声明的时候进行初始化
5. Struct可以实现接口, 也可以包含constructors, constants, fields, methods, properties, indexers, operators, events, 和nested types, 但要包含上述成员时, 建议优先考虑用Class
 
下面是一个Struct的Sample
 
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp
{
    public class StructTest
    {
        static void Main()
        {
            Book b1 = new Book(15.2, "Effective C#", "Bill");
            Book b2 = new Book(18.5, "Effective Java", "Joshua");
            if (b2 >= b1)
            {
                b1.log("b2 is greater than or equal to b1");
            }
        }
    }
    public struct Book:ICloneable
    {
        private double d_price;
        private string s_title;
        private string s_author;
        //private string s_author="Jash"; //Error, Can't have instance field initializers in struct
        public const string Type = "novel";
        public Book(double _price,string _title,string _author)
        {
            d_price = _price;
            s_title = _title;
            s_author = _author;
            log("In Constructor of struct Book");
        }
        internal void log(String msg)
        {
            Console.WriteLine(msg);
        }
        public String this[string field]
        {
            get
            {
                if (field.Equals("title")) return title;
                else if (field.Equals("author")) return author;
                else if (field.Equals("price")) return price.ToString();
                else return null;
            }
            private set
            {
                if (field.Equals("title")) title = value;
                else if (field.Equals("author")) author = value;
                else if (field.Equals("price")) price = Double.Parse(value);
            }
        }
        public double price
        {
            get
            {
                return d_price;
            }
            private set
            {
                d_price = value;
            }
        }
        public string title
        {
            get
            {
                return s_title;
            }
            set
            {
                s_title = value;
            }
        }
        public string author
        {
            get
            {
                return s_author;
            }
            set
            {
                s_author = value;
            }
        }
        public object Clone()
        {
            return null;
        }
        //overload operator ">="
        public static bool operator >= (Book src,Book target)
        {
            if (src.price >= target.price) return true;
            else return false;
        }
        public static bool operator <= (Book src, Book target)
        {
            if (src.price <= target.price) return true;
            else return false;
        }
        //nested reference type in enclosing struct
        public class Nested
        {
            Nested() { }
        }
        //nested struct in enclosing struct
        public struct NestedStruct
        {           
        }
    }
}

 运行这个程序的结果如下:

In Constructor of struct Book
In Constructor of struct Book
b2 is greater than or equal to b1
price:18.5,title:Effective Java,author:Joshua
price:15.2,title:Effective C#,author:Bill
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值