The art of OOP Polymorphism

Source: Internet
Author: User

See the code
: The final output result. Please do not use ide to write down your answers and explain the reasons.



Package
{
Public Class
{

Public Function ()
{

}

Public Function Test (): void
{
Trace ("method of Class ");
}
}

}







Package
{
Public Class B extends
{

Public Function B ()
{

}

Override public function test (): void
{
Trace ("B class method ");
}
}

}







Package
{
Import flash. display. Sprite;
Import flash. Events. event;

Public class main extends Sprite
{

Public Function main (): void
{
VaR Ta: A = new B ();
Ta. Test ();

VaR TB: B = new B ();
Ta = Tb as;
Ta. Test ();


VaR TC: B = new B ();
Ta = a (TC );
Ta. Test ();

}
}

}













































































































The answer should be:


Method B
Method B


Method B




I am wrong. The answer is:


Method B
Method of Class


Method B



As: if an object belongs to a type, as returns this object; otherwise, null is returned.
For example:
Trace (9 is number) // output: 9
Trace (9 as array) // output: NULL
Where
Ta = Tb as a; TB as a is a downward conversion method. If TB is a class, TB is returned. Otherwise, null is returned. TB as a is TB, so the second print should be: Class B Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.