Print decimal number: 501.263
Chain printing: Learning C # is fun!
C # allows you to create abstract classes that are used to provide a local class implementation of an Interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods implemented by derived classes. This derived class has more specialized functions.
Here are some rules about abstract classes in C #:
The following example illustrates an abstract class in C #: create 3 classes named Shape, HinhChuNhat and TimDienTich respectively :
using System ; namespace VdLopAbstract {
// Lớp Shape là một lớp abstract abstract class Shape { public abstract int area (); } // Lớp HinhChuNhat là lớp dẫn xuất từ lớp Shape class HinhChuNhat : Shape { private int dai ; private int rong ; public HinhChuNhat ( int a = 0 , int b = 0 ) { dai = a ; rong = b ; } public override int area () { Console . WriteLine ( "Tính diện tích hình chữ nhật" ); return ( dai * rong ); } }
// Lớp TimDienTich chứa phương thức main() để thao tác trên HinhChuNhat class TimDienTich { static void Main ( string [] args ) { HinhChuNhat r = new HinhChuNhat ( 10 , 9 ); double a = r . area (); Console . WriteLine ( "Diện tích là: {0}" , a ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Find the area of a rectangle
Area is: 90
When you have a function defined in a class that you want to deploy a derived class, you use the virtual function in C #. Virtual functions can be deployed in different ways in different inherited classes and calling these functions will be decided at runtime.
Dynamic polymorphism in C # is implemented by abstract classes and virtual functions.
The following example illustrates this: creating 5 classes named as follows, Image, Image, Timeline, HienThiDienTich and TimDienTich.
using System ; namespace VdLopAbstract {
// Lớp Hinh là lop abstract. class Hinh { protected int dai , rong ; public Hinh ( int a = 0 , int b = 0 ) { dai = a ; rong = b ; } public virtual int area () { Console . WriteLine ( "Diện tích của lớp cha là:" ); return 0 ; } }
// Lớp HinhChuNhat kế thừa từ lớp Hinh. class HinhChuNhat : Hinh { public HinhChuNhat ( int a = 0 , int b = 0 ): base ( a , b ) { } public override int area () { Console . WriteLine ( "Diện tích lớp HinhChuNhat là:" ); return ( dai * rong ); } }
// Lớp HinhTamGiac kế thừa từ lớp Hình class HinhTamGiac : Hinh { public HinhTamGiac ( int a = 0 , int b = 0 ): base ( a , b ) { } public override int area () { Console . WriteLine ( "Diện tích lớp HinhTamGiac là:" ); return ( dai * rong / 2 ); } }
// In dữ liệu diện tích ra màn hình. class HienThiDienTich { public void CallArea ( Hinh sh ) { int a ; a = sh . area (); Console . WriteLine ( "Diện tích: {0}" , a ); } }
// TimDienTich chứa main() thao tác với các đối tượng. class TimDienTich { static void Main ( string [] args ) { HienThiDienTich c = new HienThiDienTich (); HinhChuNhat r = new HinhChuNhat ( 10 , 7 ); HinhTamGiac t = new HinhTamGiac ( 10 , 5 ); c . CallArea ( r ); c . CallArea ( t ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Class area of the image is:
Area: 70
The area of Hinh Tam's class is:
Area: 25
Follow tutorialspoint
Previous article: Calculating inheritance in C #
Next lesson: Operator overloading in C #