Hi i am educating myself oop principles. I would like to know if this is a correct example of Cardellis definition of parametric polymorphism. Please enlighten me.
The example is in cfml's script based syntax.<cfscript>
Parent = createobject("component","webapp.model.Parent").init();
Child = createobject("component","webapp.model.Child").init();
GrandChild = createobject("component","webapp.model.GrandChild").init();
Test = createobject("component","webapp.model.DealWithObject");
dump(Test.getNumberOfParents(Parent));
dump(Test.getNumberOfParents(Child));
dump(Test.getNumberOfParents(GrandChild));
</cfscript>
<cfcomponent>
<cfscript>
// should deal with an infinte number of abstract data types (because of common structure)
public numeric function getNumberOfParents(component arg){
return -1 + arraylen(structfindkey(getmetadata(arguments.arg),"extends","all"));
}
</cfscript>
</cfcompo开发者_如何学Cnent>
No, just no.
Polymorphism means that you do not have to check what type something is, you just use it.
An example would be (C#):
public Boolean AreEqual(Object o1, Object o2)
{
return o1.Equals(o2);
}
The Method can accept any type of Object that inherits from Object (in C# almost everything) and Object implements Equals, so you can use it to make the check and don't have to check the type of any parameter.
Usually you accept some kind of interface to make sure the object supports the operation you want to perform.
I don't believe this pp since the function is explicitly dealing with the type of the any
argument. The point of pp is that the function works without regard to the type of the object.
If I introduce a new type into the system, this function will break since it does not have special handling for it.
EDIT: I think your updated example is subtype polymorphism, since the function will handle object and any of it's subtypes, by virtue that getmetadata handles object (and by substitution principle, it's subtypes.)
精彩评论