Using the idea of object-oriented polymorphism to find the area and perimeter of circle and rectangle respectively

Source: Internet
Author: User

//using the idea of object-oriented polymorphism to find the area and perimeter of circle and rectangle respectivelyStatic voidMain (string[] args) { Sharp Sharp=NewCircle (5); DoubleArea=Sharp .            Getarea (); DoublePerimeter=Sharp .            Getperimeter (); Console.WriteLine ("area is {0}, perimeter is {1}", Area,perimeter);        Console.readkey (); }         Public Abstract classSharp { Public Abstract DoubleGetarea ();  Public Abstract DoubleGetperimeter (); }         Public classCircle:sharp {Private Double_r;  Public DoubleR {Get{return_r;} Set{_r =value;} }             PublicCircle (Doubler) { This. R =R; }             Public Override DoubleGetarea () {returnMath.PI * This. R This.            R }             Public Override DoubleGetperimeter () {return 2* Math.PI * This.            R }        }         Public classSquare:sharp {Private Double_height;  Public DoubleHeight {Get{return_height;} Set{_height =value;} }            Private Double_width;  Public DoubleWidth {Get{return_width;} Set{_width =value;} }             PublicSquare (DoubleHeightDoublewidth) {                 This. Height =height;  This. Width =width; }             Public Override DoubleGetarea () {return  This. Height * This.            Width; }             Public Override DoubleGetperimeter () {return( This. Height + This. Width) *2; }        }

Using the idea of object-oriented polymorphism to find the area and perimeter of circle and rectangle respectively

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.