欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

D2 反射和defineEum! 练习

程序员文章站 2022-03-15 19:37:08
...
备忘:


module DioApp;


import std.stdio;
import std.conv;
import std.string;
import std.metastrings;
import std.typecons;
import samsTools.utils;


mixin(defineEnum!("Shape","Triangle","Square","Ecllipse","Circle","Undefined"));

enum Color
{
None,
White,
Black,
Red,
Undefined,
}
string[] colorInStr=["None","White","Black","Red","Undefined"];
class Animal
{
public void sayHi()
{
writefln("Animal.sayHi()");
}

}
class Cat:Animal
{
public void sayHi()
{
writefln("Cat.sayHi()");
}
}
class Mouse:Animal
{
public void sayHi()
{
writefln("Mouse.sayHi()");
}
}
class Dog:Animal
{
public:

this()
{
Name="No Name";
Age=0;
color=Color.White;
}

string Name()
{
return name;
}
string Name(string name)
{
return this.name=name;
}

int Age()
{
return age;
}
int Age(int age)
{
return this.age=age;
}
Color color()
{
return m_color;
}
Color color(Color m_color)
{
return this.m_color=m_color;
}

void jump()
{
writefln(Name ~" is jumping");
}
void sayHi()
{
string result="Hello ! I'm %s\n"
~"I'am %d years old\n"
~"I'm wearing a %s color cloths.\n"
~"Don't you like it?";
writefln(result,Name,Age,colorInStr[color]);
}
/*static*/ void doit(){}
/*
string toString()
{
return "Name:"~name~"\n"
~"Age:"~age~"\n"
~"Color:"~m_color~"\n";
}
*/
private:
string name;
Color m_color;
int age;

}
void testObjFactory()
{
string[] animal=["DioApp.Cat","DioApp.Mouse","DioApp.Dog"];
//ClassInfo info= ClassInfo.find(animal[0]);
//writefln(info.name);
//writefln(info.base.name);


for(int i=0;i<animal.length;++i)
{
string what=animal[i];
auto obj=cast(Animal)Object.factory(what);
if(__traits(hasMember,Animal,"sayHi"))
obj.sayHi;
}
}
void testEnum()
{
Shape shape= Shape.Triangle;
string triangle= enumToString(shape);
writefln(triangle);

string circle="Circle";
Shape s_circle;
enumFromString( circle,s_circle);
writefln( "%s",s_circle);
writefln(enumToString(s_circle));
}
int main(string[] args)
{
/*
auto input=askfor!(float);

writefln("You entered %12.3f",input);

write("Please enter a char:");
char ch=readln[0];
writefln("you entered %s",ch);

write("Please enter you salary:");
double salary=to!(double)(chomp(readln));
writefln("You entered %f",salary);

salary= askfor!(double)("Please enter your salary below.\nPlease enter ");
writefln("You salary is %.2f",salary);

*/
/*
auto Kitty=new Dog;
Kitty.Name="Kitty";
Kitty.Age=3;
Kitty.color=Color.Black;

//Kitty.sayHi;

//Kitty.color=Color.Red;
//Kitty.sayHi;

//writeln(Kitty);

auto obj=cast(Dog)Object.factory("DioApp.Dog");
//Dog doggie=cast(Dog)obj;
obj.sayHi;
*/
//obj=new Dog;
/*
writefln("First test:");
if(__traits(hasMember,obj,"sayHi"))
writefln("has member sayHi()");

if(obj is null)
writefln("Null class instance");
Dog doggie=cast(Dog)obj;
writefln("Second test");
if(__traits(hasMember,doggie,"sayHi"))
writefln("Has member sayHi()");

writefln("Third test:");
foreach(e;__traits(allMembers,Dog))//.contain("sayHi"))
{
if(e=="sayHi")
writefln("sayHi() is a method of the class");
}

if(doggie !is null)
//writefln("Null Dog instance.");
doggie.sayHi;

if(__traits(compiles,Dog.doit))
writefln("doit is a static function of class Dog");
*/
//auto CDog=ClassInfo.find("Dog").create;
//auto dog=cast(Dog)CDog;
//if(dog is null)
// writefln("dog is null");
//auto dog=cast(Dog)doggie;
//dog.sayHi;

//auto doggie=cast(Dog)CDog.create;
//auto doggie= new CDog.name;
//doggie.sayHi;


//testEnum;
testObjFactory;

pause;
return 0;
}
相关标签: F#