Use of abstract, virtual and override in c\

Time:2022-6-29

Reprinted from: https://www.cnblogs.com/akwwl/archive/2012/08/03/2622026.html

1. the abstract modifier indicates that the modified content is missing or not fully implemented. The abstract modifier is available for classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can only be the base class of other classes. Members marked as abstract or contained in abstract classes must be implemented through classes derived from abstract classes.

(1) Abstract classes have the following characteristics:

1) Abstract classes cannot be instantiated.

2) Abstract classes can contain abstract methods and abstract accessors.

3) An abstract class cannot be decorated with the sealed modifier because the two modifiers have opposite meanings. A class with the sealed modifier cannot inherit, while the abstract modifier requires that the class inherit.

4) A non abstract class derived from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.

5) Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain an implementation.

(2) Abstract methods have the following characteristics:

1) Abstract methods are implicit virtual methods.

2) Abstract method declarations are only allowed in abstract classes.

3) Because the abstract method declaration does not provide the actual implementation, there is no method body; The method declaration simply ends with a semicolon and has no braces ({}) after the signature.

(3) It is wrong to use static or virtual modifiers in abstract method declarations. Abstract properties behave the same as abstract methods, except that they differ in declaration and invocation syntax. It is an error to use the abstract modifier on a static attribute. Abstract inheritance properties can be overridden in a derived class by including a property declaration that uses the override modifier.

public abstract class parent

{

protected int x=100;

protected int y = 200;

public abstract void function();

public abstract int X { get; }

public abstract int Y { get; }

}

public class newperson : parent

{

public override void function()

{

x++;

y++;

}

public override int X

{

get { return x+100; }

}

public override int Y

{

get { return y+100; }

}

}

static void Main(string[] args)

{

newperson p = new newperson();

Console.WriteLine(p.X);

Console.WriteLine(p.Y);

p.function();

Console.WriteLine(p.X);

Console.WriteLine(p.Y);

Console.ReadKey();

}

 

2. The virtual keyword is used to decorate method, property, indexer, or event declarations and make them overridable in derived classes. Virtual members can be implemented byOverride memberChanges. When a virtual method is called, the runtime type of the object is checked for overriding members. This overriding member in most derived classes will be called, and if no derived class overrides the member, it may be the original member. By default, methods are non virtual. Non virtual methods cannot be overridden. The virtual modifier cannot be used with the static, abstract, private, or override modifiers. The same as the abstract method is that the override keyword is used to rewrite the method. The difference is that the abstract method must exist in the abstract class, and the subclass must be rewritten, unless the subclass is also abstract.

public class Dimensions

{

public const double PI = Math.PI;

protected double x;

protected double y;

public Dimensions() { }

public Dimensions(double x, double y)

{

this.x = x;

this.y = y;

}

public virtual double Area()

{

return x * y;

}

}

public class Circle : Dimensions

{

public Circle(double r) : base(r,0) { }

 

public override double Area()

{

Console.WriteLine(base.Area());

return PI * x * x;

}

}

public class Sphere : Dimensions

{

public Sphere(double r) : base(r,0) { }

public override double Area()

{

return 4 * PI * x * x;

}

}

public class Cylinder : Dimensions

{

public Cylinder(double r, double h) : base(r, h) { }

public override double Area()

{

return 2 * PI * x * x + 2 * PI * x * y;

}

}

static void Main(string[] args)

{

Dimensions P = new Circle(5.0);

Sphere sphere = new Sphere(10);

Cylinder cylinder = new Cylinder(10, 10);

Console.WriteLine(P.Area());

Console.WriteLine(sphere.Area());

Console.WriteLine(cylinder.Area());

Console.ReadKey();

}

 

3.The override method provides a new implementation of a member inherited from the base class. The method overridden by the override declaration is called the override base method. Overridden base methods must have the same signature as override methods. Non virtual or static methods cannot be overridden. Overridden base methods must be virtual, abstract, or override. Override declarations cannot change the accessibility of virtual methods. Override method and virtual method must have the sameAccess level modifier。 Overriding a property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

4. Difference between base and this

1) Base function: used to access members of the base class from derived classes and call methods on the base class that have been overridden by other methods. Specifies the base class constructor that should be called when creating a derived class instance.

Note: it is wrong to use the base keyword from a static method.
2) Difference:

Base refers to the parent class in the subclass and is used to access overridden base class members in the derived class.
This is the current class. It refers to itself and is used to access the members of this class. Of course, it also includes inherited public and protected members.

3) Note:
a). Both base and this are not allowed in static members. The reason is that both base and this access instances of classes, that is, objects, while static members can only be accessed by classes, not objects.
b). Base is designed for polymorphism.
c). Only one constructor can be specified with this or base keyword, that is, this and base cannot be used on one constructor at the same time.
d). In addition to base, another way to access base class members is to implement them by displaying type conversions. Only this method cannot be static.

,

(1) Abstract classes have the following characteristics:

1) Abstract classes cannot be instantiated.

2) Abstract classes can contain abstract methods and abstract accessors.

3) An abstract class cannot be decorated with the sealed modifier because the two modifiers have opposite meanings. A class with the sealed modifier cannot inherit, while the abstract modifier requires that the class inherit.

4) A non abstract class derived from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.

5) Use the abstract modifier in a method or property declaration to indicate that the method or property does not contain an implementation.

(2) Abstract methods have the following characteristics:

1) Abstract methods are implicit virtual methods.

2) Abstract method declarations are only allowed in abstract classes.

3) Because the abstract method declaration does not provide the actual implementation, there is no method body; The method declaration simply ends with a semicolon and has no braces ({}) after the signature.

(3) It is wrong to use static or virtual modifiers in abstract method declarations. Abstract properties behave the same as abstract methods, except that they differ in declaration and invocation syntax. It is an error to use the abstract modifier on a static attribute. Abstract inheritance properties can be overridden in a derived class by including a property declaration that uses the override modifier.

public abstract class parent

{

protected int x=100;

protected int y = 200;

public abstract void function();

public abstract int X { get; }

public abstract int Y { get; }

}

public class newperson : parent

{

public override void function()

{

x++;

y++;

}

public override int X

{

get { return x+100; }

}

public override int Y

{

get { return y+100; }

}

}

static void Main(string[] args)

{

newperson p = new newperson();

Console.WriteLine(p.X);

Console.WriteLine(p.Y);

p.function();

Console.WriteLine(p.X);

Console.WriteLine(p.Y);

Console.ReadKey();

}

 

2. The virtual keyword is used to decorate method, property, indexer, or event declarations and make them overridable in derived classes. Virtual members can be implemented byOverride memberChanges. When a virtual method is called, the runtime type of the object is checked for overriding members. This overriding member in most derived classes will be called, and if no derived class overrides the member, it may be the original member. By default, methods are non virtual. Non virtual methods cannot be overridden. The virtual modifier cannot be used with the static, abstract, private, or override modifiers. The same as the abstract method is that the override keyword is used to rewrite the method. The difference is that the abstract method must exist in the abstract class, and the subclass must be rewritten, unless the subclass is also abstract.

public class Dimensions

{

public const double PI = Math.PI;

protected double x;

protected double y;

public Dimensions() { }

public Dimensions(double x, double y)

{

this.x = x;

this.y = y;

}

public virtual double Area()

{

return x * y;

}

}

public class Circle : Dimensions

{

public Circle(double r) : base(r,0) { }

 

public override double Area()

{

Console.WriteLine(base.Area());

return PI * x * x;

}

}

public class Sphere : Dimensions

{

public Sphere(double r) : base(r,0) { }

public override double Area()

{

return 4 * PI * x * x;

}

}

public class Cylinder : Dimensions

{

public Cylinder(double r, double h) : base(r, h) { }

public override double Area()

{

return 2 * PI * x * x + 2 * PI * x * y;

}

}

static void Main(string[] args)

{

Dimensions P = new Circle(5.0);

Sphere sphere = new Sphere(10);

Cylinder cylinder = new Cylinder(10, 10);

Console.WriteLine(P.Area());

Console.WriteLine(sphere.Area());

Console.WriteLine(cylinder.Area());

Console.ReadKey();

}

 

3.The override method provides a new implementation of a member inherited from the base class. The method overridden by the override declaration is called the override base method. Overridden base methods must have the same signature as override methods. Non virtual or static methods cannot be overridden. Overridden base methods must be virtual, abstract, or override. Override declarations cannot change the accessibility of virtual methods. Override method and virtual method must have the sameAccess level modifier。 Overriding a property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

4. Difference between base and this

1) Base function: used to access members of the base class from derived classes and call methods on the base class that have been overridden by other methods. Specifies the base class constructor that should be called when creating a derived class instance.

Note: it is wrong to use the base keyword from a static method.
2) Difference:

Base refers to the parent class in the subclass and is used to access overridden base class members in the derived class.
This is the current class. It refers to itself and is used to access the members of this class. Of course, it also includes inherited public and protected members.

3) Note:
a). Both base and this are not allowed in static members. The reason is that both base and this access instances of classes, that is, objects, while static members can only be accessed by classes, not objects.
b). Base is designed for polymorphism.
c). Only one constructor can be specified with this or base keyword, that is, this and base cannot be used on one constructor at the same time.
d). In addition to base, another way to access base class members is to implement them by displaying type conversions. Only this method cannot be static.

Recommended Today

Modern Enterprise Architecture Framework – Application Architecture

Modern Enterprise Architecture Framework:https://mp.weixin.qq.com/s/SlrEu0_t0slijrNZ6DP4Ng Business Architecture:https://mp.weixin.qq.com/s/zQCjiHuxFvAg5QiOAuLAcQ 4. Application Architecture The core concern of application architecture is which applications carry the business requirements, how they interact with users, how and how they interact, and what data they access or change. The design of the application architecture is mainly based on the application (Application) design as the […]