Third, define the TestDrive() static method in the Program class that takes a Vehicle as the parameter. The Drive() method in each class displays a message in the console: public class Car : Vehicle Second, define the Car and Truck classes that inherit from the Vehicle class. Public static void TestDrive( Vehicle vehicle)įirst, define an abstract class Vehicle that has a Drive() abstract method: public abstract class Vehicle Public override void Drive( ) => Console.WriteLine( "Drive a truck") Public override void Drive( ) => Console.WriteLine( "Drive a car") Let’s take an example to understand the Liskov substitution principle in C#: namespace LSP This means that the behavior of the derived class should not contradict the behavior of the base class. In other words, the derived classes should be substitutable for their base class without causing errors or side effects. The Liskov substitution principle states that if a method uses a base class, then it should be able to use any of its derived classes without the need of having the information about the derived class. Note that the Liskov substitution principle is named after Barbara Liskov, who first formulated it in 1988. The Liskove substitution principle (LSP) is the third principle in the SOLID principles of object-oriented design: Introduction to the C# Liskov Substitution Principle Summary: in this tutorial, you’ll learn about the C# Liskov substitution principle and how to apply it to build more scalable and maintainable software applications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |