In python, a class is a blueprint that defines the behavior and properties of an object. An object is an instance of a class and has all the properties and methods defined by the class.
We can define a class through the class
keyword, as shown below:
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old.")
In this example, Person
is a class that defines two attributes name
and age
, and a method greet()
.
We can create objects on the Person
class as follows:
person = Person("John", 25) person.greet()
In this way, we create an object named person
and call its greet()
method.
A class can have any number of properties and methods. Properties are the state of the class, and methods are the behavior of the class.
The properties of a class can be accessed through the dot operator .
, as shown below:
print(person.name) print(person.age)
The attributes of a class can also be modified through the assignment operator =
, as shown below:
person.name = "Jane" person.age = 30
The properties and methods of the class can be accessed using the self
keyword, where self
represents a reference to the current object.
The properties and methods of a class are public, which means they can be accessed anywhere in the class.
Inheritance is a method of creating a new class (derived class) that inherits properties and methods from an existing class (base class).
Derived classes can override base class methods to provide different implementations.
Derived classes can be created through the class
keyword and the ()
operator, as shown below:
class Student(Person): def __init__(self, name, age, major): super().__init__(name, age) self.major = major def study(self): print(f"{self.name} is studying {self.major}.")
In this example, the Student
class inherits from the Person
class, and the Student
class adds the major
attribute and study ()
method.
Polymorphism is an important feature of Object-oriented Programming, which allows us to use a unified way to deal with different types of objects.
One way to achieve polymorphism is to use abstract classes. Abstract classes are classes that cannot be instantiated, they can only be inherited.
Abstract classes can be defined using the abc
module. The abc
module provides many decorators for defining abstract classes.
Methods in abstract classes can be overridden by subclasses to provide different implementations.
Another way to achieve polymorphism is to use an interface. An interface is a specification that defines a set of methods. It cannot be instantiated, but it can be implemented by a class.
The class that implements the interface must implement all methods defined in the interface.
Interfaces can be defined using the typing
module. The typing
module provides many type annotations for defining interfaces.
Classes and objects are the foundation of object-oriented programming. They can help us build clearer and easier-to-maintain code.
Inheritance and polymorphism are important features of object-oriented programming. They can help us reuse code and create more flexible programs.
The above is the detailed content of Explore Python classes and objects from scratch and uncover the mysteries of object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!