A custom abstract class is used to calculate the area of a circle and to customize the area of a circle.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 6 namespace Test07 7 {8 /******************************** * ****************** 9*10 * example code 11 ************* ***************************************/ 12 // public abstract class myClass13 // {14 // private int r = 0; 15 ///// <summary> 16 //// circle radius 17 ///// </summary> 18 // public int R19 // {20 // get21 // {22 // return r; 23 //} 24 // set25 // {26 // r = value; 27 //} 28 //} 29 // <summary> 30 //// abstract method, used to calculate the circular Area 31 // </summary> 32 // public abstract double Area (); 33 //} 34 // public class DriveClass: myClass // inherit the abstract class 35 // {36 //// <summary> 37 // rewrite the method for calculating the circular area in the abstract class 38 // </summary> 39 // public override double Area () 40 // {41 // return Math. PI * R; 42 //} 43 //} 44 // class Program45 // {46 // static void Main (string [] args) 47 // {48 // DriveClass driveclass = new DriveClass (); // instantiate a derived class 49 // myClass myclass = driveclass; // use a derived class object to instantiate an abstract class 50 // myclass. R = 3; // use an abstract class object to access the radius attribute 51 in the abstract class // Console. writeLine ("Circle Area:" + myclass. area ()); // use an abstract class object to call method 52 in the derived class //} 53 //} 54 55 56 /****************** * *********************************** 57 ** 58 * My code 59 ************************************** * ************/60 public abstract class myClass 61 {62 private int r; 63 public int R 64 {65 get {return r;} 66 set {r = value;} 67} 68 public abstract double Area (); 69} 70 public class Driveclass: myClass 71 {72 public override double Area () 73 {74 return Math. PI * R; 75} 76} 77 class Program 78 {79 static void Main (string [] args) 80 {81 Driveclass driveclass = new Driveclass (); 82 myClass myclass = driveclass; 83 driveclass. R = 1; 84 85 Console. writeLine ("the area of the circle is" + driveclass. area (); 86} 87} 88}
After debugging, it is found that after instantiating a derived class, you can directly access the radius attribute of the abstract class by using the instantiated derived class object or call the derived class method by using the instantiated derived class object,
After all, the derived classes inherited from the abstract class can access the attributes in the abstract class.