4个回答
展开全部
不理解你说的什么意思,随便说一下……
把函数的借口改成引用或指针,把类中所有的属性通过借口传递出来。
把函数的借口改成引用或指针,把类中所有的属性通过借口传递出来。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c++ 本身好像没有反射功能。估计实现不了。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
c++实现不了这种功能
编译后类就不存在了。
编译后类就不存在了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
.NET的反射机制是很有特色的,VB,C#,VC语言都支持。
通过反射机制,可以轻而易举枚举出一个类的各种信息,除了属性外,还可以获得构造器、方法、字段等各种信息,类型可以是公共的,非公共的,实例化的,静态的各种属性、方法等。
这里给出一段VC.NET的实例代码:
using namespace System;
using namespace System::Reflection;
//定义测试用的类 Student
public ref class Student
{
private:
String^ name;
Int32 number;
String^ address;
public:
Student(String^ name, String^ address, Int32 number)
{
this->name = name;
this->address = address;
this->number = number;
}
property String^ Name
{
String^ get()
{
return name;
}
void set(String^ value)
{
name = value;
}
}
property Int32 Number
{
Int32 get()
{
return number;
}
void set(Int32 value)
{
number = value;
}
}
protected:
property String^ Address
{
String^ get()
{
return address;
}
void set(String^ value)
{
address = value;
}
}
};
void DisplayPropertyInfo(Student^ student, array<PropertyInfo^>^ properties);
int main(array<System::String ^> ^args)
{
Student^ student = gcnew Student("Alice", "No.193, St.Robinson, NY, USA", 1);
student->Name = "Alice.Caley";
student->Number = 2;
Console::WriteLine(student->Name);
Console::WriteLine(student->Number);
Console::WriteLine();
//反射
Type^ type = student->GetType();
//公共实例属性
array<PropertyInfo^>^ publicProperties = type->GetProperties(static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
Console::WriteLine("[Display public property info]");
DisplayPropertyInfo(student, publicProperties);
Console::WriteLine();
//非公共实例属性
array<PropertyInfo^>^ nonPublicProperties =type->GetProperties(static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance));
Console::WriteLine("[Display non public property info]");
DisplayPropertyInfo(student, nonPublicProperties);
Console::WriteLine();
return 0;
}
void DisplayPropertyInfo(Student^ student, array<PropertyInfo^>^ properties)
{
for (int i = 0; i < properties->Length; i++)
{
PropertyInfo^ p = properties[i];
Console::WriteLine(p->Name);
Console::WriteLine(p->PropertyType);
//获取student实例的属性值
Console::WriteLine("value -- " + p->GetValue(student, nullptr));
}
}
通过反射机制,可以轻而易举枚举出一个类的各种信息,除了属性外,还可以获得构造器、方法、字段等各种信息,类型可以是公共的,非公共的,实例化的,静态的各种属性、方法等。
这里给出一段VC.NET的实例代码:
using namespace System;
using namespace System::Reflection;
//定义测试用的类 Student
public ref class Student
{
private:
String^ name;
Int32 number;
String^ address;
public:
Student(String^ name, String^ address, Int32 number)
{
this->name = name;
this->address = address;
this->number = number;
}
property String^ Name
{
String^ get()
{
return name;
}
void set(String^ value)
{
name = value;
}
}
property Int32 Number
{
Int32 get()
{
return number;
}
void set(Int32 value)
{
number = value;
}
}
protected:
property String^ Address
{
String^ get()
{
return address;
}
void set(String^ value)
{
address = value;
}
}
};
void DisplayPropertyInfo(Student^ student, array<PropertyInfo^>^ properties);
int main(array<System::String ^> ^args)
{
Student^ student = gcnew Student("Alice", "No.193, St.Robinson, NY, USA", 1);
student->Name = "Alice.Caley";
student->Number = 2;
Console::WriteLine(student->Name);
Console::WriteLine(student->Number);
Console::WriteLine();
//反射
Type^ type = student->GetType();
//公共实例属性
array<PropertyInfo^>^ publicProperties = type->GetProperties(static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
Console::WriteLine("[Display public property info]");
DisplayPropertyInfo(student, publicProperties);
Console::WriteLine();
//非公共实例属性
array<PropertyInfo^>^ nonPublicProperties =type->GetProperties(static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance));
Console::WriteLine("[Display non public property info]");
DisplayPropertyInfo(student, nonPublicProperties);
Console::WriteLine();
return 0;
}
void DisplayPropertyInfo(Student^ student, array<PropertyInfo^>^ properties)
{
for (int i = 0; i < properties->Length; i++)
{
PropertyInfo^ p = properties[i];
Console::WriteLine(p->Name);
Console::WriteLine(p->PropertyType);
//获取student实例的属性值
Console::WriteLine("value -- " + p->GetValue(student, nullptr));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询