Use polymorphism to find the area and perimeter of a rectangle and the area and perimeter of a circle

Source: Internet
Author: User

Use polymorphism to find the area and perimeter of the rectangle and the area of the circle my perimeter

Shape shape = new Circle (5); New Square (5,6);
Double area = shape. Getarea ();
Double perimeter = shape. Getperimeter ();
Console.WriteLine ("The area of this shape is {0}, the perimeter is {1}", Area,perimeter);
Console.readkey ();

}

Public abstract class Shape
{


public abstract double Getarea ();
public abstract double getperimeter ();

}




public class Circle:shape
{
Private double _r;
Public double R
{
get {return _r;}
set {_r = value;}
}

Public Circle (Double R)
{

This. R = r;
}


public override double Getarea ()
{
Return Math.PI * this. R * this. R
}
public override double Getperimeter ()
{
Return 2*math.pi*this. R
}
}

public class Square:shape
{
Private double _height;
Public double Height
{
get {return _height;}
set {_height = value;}

}
Private double _width;
Public double Width
{
get {return _width;}
set {_width = value;}
}

Public Square (double height, double width)
{
This. Height = height;
This. width = width;
}
public override double Getarea ()
{
return this. Height * this. Width;
}
public override double Getperimeter ()
{
Return (this. Height+this. Width) * *;
}
}

Use polymorphism to find the area and perimeter of a rectangle and the area and perimeter of a circle

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.