📜  OOP inC# (1)

📅  最后修改于: 2023-12-03 15:18:07.006000             🧑  作者: Mango

OOP in C#

Object-Oriented Programming (OOP) is a programming paradigm that uses objects to represent and manipulate data. C# is a modern, high-level programming language that supports OOP. In C#, everything is an object, and you can create classes to define new objects and their behavior.

Classes and Objects

In C#, a class is a blueprint for creating objects. A class defines the properties and methods that an object of that class can have. To create an object, you must first instantiate the class. This is done using the new keyword.

// Define a class
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    
    public void SayHello()
    {
        Console.WriteLine("Hello, my name is " + Name);
    }
}

// Instantiate the class
Person john = new Person();
john.Name = "John";
john.Age = 42;

In this example, we have defined a class Person with two properties (Name and Age) and a method SayHello(). We then create an instance of this class using the new keyword and set the Name and Age properties.

Inheritance

Inheritance is a key concept in OOP that allows classes to inherit properties and methods from their parent class. In C#, you can create a new class that inherits from an existing class using the : operator.

// Define a base class
public class Animal
{
    public string Name { get; set; }
    
    public void MakeSound()
    {
        Console.WriteLine("I am an animal");
    }
}

// Define a derived class
public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Woof!");
    }
}

// Instantiate the derived class
Dog fido = new Dog();
fido.Name = "Fido";
fido.Bark();

In this example, we have defined a base class Animal with a Name property and a MakeSound() method. We then define a derived class Dog that inherits from the Animal class and adds a new Bark() method. We create an instance of the Dog class and call the Bark() method.

Polymorphism

Polymorphism is the ability of objects to take on multiple forms. In C#, this is achieved through method overriding and method overloading.

// Define a base class
public class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

// Define a derived class
public class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
    
    public void Draw(int radius)
    {
        Console.WriteLine("Drawing a circle with radius " + radius);
    }
}

// Call the methods
Shape shape = new Circle();
shape.Draw();

Circle circle = new Circle();
circle.Draw();
circle.Draw(10);

In this example, we have defined a base class Shape with a virtual Draw() method. We then define a derived class Circle that overrides the Draw() method and adds a new Draw(int radius) method. We create an instance of the Circle class using the base class Shape, and then call the Draw() method. We also create another instance of the Circle class and call both the Draw() and Draw(int radius) methods.

Encapsulation

Encapsulation is the idea of hiding the complexity of a class from the outside world. In C#, you can achieve encapsulation using access modifiers (public, private, protected, and internal).

// Define a class with encapsulated properties
public class Rectangle
{
    private int width;
    private int height;
    
    public void SetWidth(int width)
    {
        this.width = width;
    }
    
    public void SetHeight(int height)
    {
        this.height = height;
    }
    
    public int GetArea()
    {
        return width * height;
    }
}

// Instantiate the class
Rectangle rect = new Rectangle();
rect.SetWidth(10);
rect.SetHeight(5);
Console.WriteLine("Area: " + rect.GetArea());

In this example, we have defined a class Rectangle with encapsulated properties width and height. We have also defined public methods SetWidth(int width), SetHeight(int height), and GetArea() to modify and access these properties, while keeping them hidden from the outside world. We create an instance of the Rectangle class and set its width and height properties using the SetWidth(int width) and SetHeight(int height) methods, respectively. We then call the GetArea() method to calculate the area of the rectangle.

Conclusion

C# is a powerful and flexible programming language that supports OOP concepts like classes and objects, inheritance, polymorphism, and encapsulation. Understanding these concepts is essential for writing efficient and maintainable code in C#.