VC.NET 2005中关于Property的用法

    在新的VC2005中,增加了不少关于property的新的语法,害的我百度了半天才找到解决方法,现在列出来以供参考。
     标准property
定义:
ref class DefProperty
{
private:
   String^ mData;
public:
   property String^ MyData
   {
       String^ get();
        void set(String^);
   }
};
实现:
String^ DefProperty::MyData::get()
{
    return mData;
}

void DefProperty::MyData::set(String^ value)
{
   mData = value;
}

简化的property
ref class EasyProperty
{
public:
   property String^ MyData;
};

静态property
定义:
ref class StaticProperty
{
private:
    static String^ mData;
public:
    static property String^ Data1;
    static property String^ Data2
   {
       String^ get();
        void set(String^);
   }
};
实现:
String^ StaticProperty:Data2::get()
{
    return mData;
}

void StaticProperty: ata2::set(String^ value)
{
   mData = value;
}

虚拟property
定义:
interface class IProperty
{
public:
   property String^ Data1;
   property String^ Data2
   {
       String^ get();
        void set(String^);
   }
};

ref class VirtualProperty : public IProperty
{
protected:
   String^ mData2;
   String^ mData3;
public:
   property String^ Data1;
   property String^ Data2
   {
       String^ get();
        void set(String^);
   }
   property String^ Data3
   {
       virtual String^ get();
       virtual void set(String^);
   }
};

ref class VirtualPropertyNew : public VirtualProperty
{
public:
   property String^ Data2
   {
       String^ get();
        void set(String^);
   }
   property String^ Data3
   {
       virtual String^ get();
       virtual void set(String^);
   }
};
实现:
String^ VirtualProperty: ata2::get()
{
    return mData2->Insert(0, "Base2:");
}

void VirtualProperty: ata2::set(System::String ^ value)
{
   mData2 = value;
}

String^ VirtualProperty: ata3::get()
{
    return mData3->Insert(0, "Base3:");
}

void VirtualProperty: ata3::set(System::String ^ value)
{
   mData3 = value;
}

String^ VirtualPropertyNew: ata2::get()
{
    return mData2->Insert(0, "New2:");
}

void VirtualPropertyNew: ata2::set(System::String ^ value)
{
   mData2 = value;
}

String^ VirtualPropertyNew: ata3::get()
{
    return mData3->Insert(0, "New3:");
}

void VirtualPropertyNew: ata3::set(System::String ^ value)
{
   mData3 = value;
}

索引property
ref class IdxProperty
{
public:
   property int default[ int]
   {
        int get( int idx)
       {
            return 0;
       }
        void set( int idx, int value)
       {
       }
   }
   property int default[String^]
   {
        int get(String^ idx)
       {
            return 0;
       }
        void set(String^ idx, int value)
       {
       }
   }

   property int ItemSlot[ int]
   {
        int get( int idx)
       {
            return 0;
       }
        void set( int idx, int value)
       {
       }
   }

   property int Data3D[ int, int, int]
   {
        int get( int x, int y, int z)
       {
            return 0;
       }
        void set( int x, int y, int z, int v)
       {
       }
   }
};

转载于:https://www.cnblogs.com/feiweiwei/archive/2007/11/20/966266.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值