C # Language Primer (3)

Source: Internet
Author: User
Tags abstract getcolor
In this last example, we look at the abstraction and polymorphism of C #. Let's start by defining these two new terms. Abstract is implemented by extracting the common parts from multiple objects and merging them into a separate abstract class. In this example we will create an abstract class shape (shape). Each shape has a method of returning its color, whether it is a square or a circle or a rectangle, and the method of returning the color is always the same, so this method can be extracted into the parent shape. So, if we have 10 different shapes that need a way to return the color, now we just have to create a method in the parent class. You can see using abstractions to make your code shorter.

In the field of object-oriented programming, polymorphism (polymorphism) is the ability of an object or method to behave differently depending on the class. In the following example, the abstract class shape has a Getarea () method that has different functions for different shapes (circles, squares, or rectangles).

Here's the code:


Public abstract class Shape {
protected string color;
Public Shape (string color) {
This.color = color;
}
public string GetColor () {
return color;
}
public abstract double Getarea ();
}

public class Circle:shape {
private double radius;
Public Circle (string color, double radius): base (color) {
This.radius = radius;
}
public override double Getarea () {
return System.Math.PI * radius * RADIUS;
}
}

public class Square:shape {
Private double Sidelen;
Public Square (string color, double Sidelen): base (color) {
This.sidelen = Sidelen;
}
public override double Getarea () {
return Sidelen * Sidelen;
}
}

/*
public class Rectangle:shape
... Slightly...
*/

public class Example3
{
static void Main ()
{
Shape mycircle = new Circle ("Orange", 3);
Shape myrectangle = new Rectangle ("Red", 8, 4);
Shape mysquare = new Square ("Green", 4);
System.Console.WriteLine ("The color of the circle is" + Mycircle.getcolor ()
+ "It's area is" + mycircle.getarea () + ".");
System.Console.WriteLine ("The color of the rectangle is" + myrectangle.getcolor ()
+ "It's area is" + myrectangle.getarea () + ".");
System.Console.WriteLine ("The color of the square is" + mysquare.getcolor ()
+ "It's area is" + mysquare.getarea () + ".");
}
}






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.