当前位置:网站首页>C inheritance and interface design polymorphism

C inheritance and interface design polymorphism

2022-07-07 23:33:00 Bobo in summer

Hide base class : Law 1- Replace the base member with a new derived member

class A
        {
    
            public void fun()
            {
    
                Console.WriteLine("A");
            }
        }
 class B : A
        {
    
            **new public void fun() // Hide base class methods fun**
            {
    
                Console.WriteLine("B");
            }
        }
 static void Main(string[] args)
        {
    
            B b = new B();
            b.fun();
        }
        // The result is “B”

Law 2- Rewrite the virtual base member
rewrite : Subclass base class has the same signature .: Overridden base method virtual、abstract or override.
heavy load : Of the same kind , Same name , Different parameters , Different signatures .
Default , Method is not virtual , You cannot override a non virtual method .virtual A modifier cannot be compared with static、abstract and override Use modifiers together . Use on static properties virtual The modifier is wrong .

  class Student
        {
    
            protected int no;                // Student number 
            protected string name;      // full name 
            protected string tname;     // Head teacher or instructor 
            public void setdata(int no1, string name1, string tname1)
            {
    
                no = no1; name = name1; tname = tname1;
            }
            public virtual void dispdata() // Virtual method 
            {
    
                Console.WriteLine(" Undergraduate   Student number :{0}  full name :{1}  class   Lord   ren :{2}", no, name, tname);
            }
 class Graduate : Student
            {
    
                public override void dispdata() // Rewriting methods 
                {
    
                    Console.WriteLine(" Graduate student   Student number :{0}  full name :{1}  The instructor :{2}",no, name, tname);
                }
            }
 static void Main(string[] args)
            {
    
                Student s = new Student();
                s.setdata(101, " Wang Hua ", " Li Liang ");
                s.dispdata();
                Graduate g = new Graduate();
                g.setdata(201, " Zhang hua ", " Chen Jun ");
                g.dispdata();
            }

 Insert picture description here
Design a console application , Use the virtual method to find a rectangle 、 round 、 Area or surface area of sphere and cylinder .

 public class Rectangle              // Rectangle class 
        {
    
            public const double PI = Math.PI;
            protected double x, y;
            public Rectangle() {
     }
            public Rectangle(double x1, double y1)
            {
    
                x = x1; y = y1;
            }
            public virtual double Area()    // Find the area 
            {
    
                return x * y;
            }
 public class Circle : Rectangle     // Circles 
            {
    
                public Circle(double r) : base(r, 0) {
     }
                public override double Area()   // Find the area 
                {
    
                    return PI * x * x;
                }
            }
 class Sphere : Rectangle                // Spheroids 
            {
    
                public Sphere(double r) : base(r, 0) {
     }
                public override double Area()   // Find the area 
                {
    
                    return 4 * PI * x * x;
                }
            }
 class Cylinder : Rectangle          // Cylinder class 
            {
    
                public Cylinder(double r, double h) : base(r, h) {
     }
                public override double Area()   // Find the area 
                {
    
                    return 2 * PI * x * x + 2 * PI * x * y;
                }
            }
 static void Main(string[] args)
            {
    
                double x = 2.4, y = 5.6;
                double r = 3.0, h = 5.0;
                Rectangle t = new Rectangle(x, y);
                Rectangle c = new Circle(r);
                Rectangle s = new Sphere(r);
                Rectangle l = new Cylinder(r, h);
                Console.WriteLine(" Long for {0}, Wide for {1} Rectangular area of ={2:F2}",
          x, y, t.Area());
                Console.WriteLine("  The radius is {0} Circular area of ={1:F2}",
          r, c.Area());
                Console.WriteLine("  The radius is {0} The surface area of the sphere ={1:F2}",
          r, s.Area());
                Console.WriteLine(" The radius is {0}, The height is {1} Surface area of cylinder ={2:F2}", r, h, l.Area());
            }

 Insert picture description here

原网站

版权声明
本文为[Bobo in summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130558212403.html