C # getting started with language (4)

Source: Internet
Author: User

The first class we created is Shape. This is an abstract class, because we do not want to create an instance of this class. We want to create an instance of its derived class. We extract common features from all shapes (circles, rectangles, and squares) to Shape classes. The Shape class has an instance variable color with the protected modifier in its variable declaration. The protected modifier indicates that the variable can only be accessed within the class or in the derived class of the class. The variable declares the constructor of the Shape class and the access method getColor (). There is nothing new between the two methods. The last method, getArea (), is added with the abstract modifier, because different shapes have different area calculation methods. Therefore, this method must be defined by various shapes.

The following three classes, Circle, Rectangle, and Square, are derived from the Shape class and both have the features described by the Shape. This can be seen from their definition that their declarations contain "public class: Shape {". This ": Shape" indicates that the current class is derived from the Shape class. Since these three classes are derived from Shape, they automatically have all the public or protected instance variables defined in Shape, that is, Circle, Rectangle, and Square contain the instance variable color.

Each Sharp derived class has its own constructor. It is responsible for calling the constructor of the parent class Shape to set public instance variables (color) and its own unique instance variables. For example, in the "public Circle (string color, double radius): base (color)" statement, ": base (color)" indicates that the constructor of the parent class is called with the color parameter.

Finally, let's take a look at the getArea () method, which is a sample of polymorphism. All shapes have the getArea () method, but the calling method varies depending on whether the object is a circle, a rectangle, or a square.

To run this example, first save all the files to the same directory and then execute the following command:


Csc/target: library/out: Shapes. dll
Shapes. cs Circle. cs Rectangle. cs Square. cs


Then execute:


Csc/reference: Shapes. dll Example3.cs


Now, if we run example3.exe, we will get the following output:


The circle color is orange. Its area is 28.274333882308138 square meters.
The color of a square is green. Its area is 16.
The color of a rectangle is red. Its area is 32.

Refer:

C # Language Reference
C # Station


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.