1, What is object-oriented
Python has been an object-oriented language since its inception, making it easy to create classes and objects in Python. In addition to Python, Java is also an object-oriented programming language.
Let's have a brief understanding first Object oriented Some basic characteristics of.
- Class: Used to describe a collection of objects with the same properties and methods. It defines the properties and methods shared by each object in the collection. An object is an instance of a class.
- Method: The function defined in the class.
- Class variables: Class variables are common throughout the entire instantiated object. Class variables are defined within the class and outside the function body. Class variables are usually not used as instance variables.
- Data member: Class or instance variables are used to handle data related to a class and its instance objects.
- Method rewrite: If a method inherited from a parent class cannot meet the requirements of a subclass, it can be rewritten. This process is called method override, also known as method rewrite.
- Local variable: A variable defined in a method that only applies to the class of the current instance.
- Instance variable: In the declaration of a class, attributes are represented by variables, which are called instance variables. An instance variable is a variable decorated with self.
- Inheritance: A derived class inherits the fields and methods of the base class. Inheritance also allows an object of a derived class to be treated as a base class object. For example, there is a design where a Dog type object is derived from the Animal class, which simulates" It is an (is-a)" Relationship (e.g., Dog is an Animal).
- Instantiation: Create an instance of a class, which is a concrete object of the class.
- Object: An instance of a data structure defined by a class. The object includes two data members (class variables and instance variables) and a method.
2, Definition of Class
1. Do not define classes using constructors
class Person:
#attribute
name = "bad guy atu"
#method
def say_hello(self):
print(f'hello {Person.name}')
if __name__== '__main__':
# 1. accessing properties of a class - can be done through the class name . direct access to property methods
print(Person.name)
# 2. accessing methods defined in a class - requires instantiating classes and objects . method / object . attribute
p = Person()
p.say_hello()
print(p.name)
# 3. class can be instantiated multiple times to create multiple objects
p1 = Person()
print(p1.name)
2. Define classes using constructors
class Person:
#constructor of class
def __init__(self,name,age):
self.name = name
self.age = age
def say_hello(self):
print(f'hello {self.name }this year, you{ self.age}')
if __name__== '__main__':
p = Person('atu',21)
p.say_hello()
p1 = Person('xiaoya',20)
p1.say_hello()
3, Inheritance of classes
class Animal:
def __init__(self,name,legs):
self.name = name
self.legs = legs
def info(self):
print(f'i am{ self.name }i have{ self.legs }legs !')
class Dog(Animal):
pass
class Cat(Animal):
def walk(self):
print(f'i am a cute one{ self.name }i only have{ self.legs }bar')
if __name__== '__main__':
d = Dog("dog",4)
d.info()
m = Cat("cat",2)
m.walk()
#this info the method is to start from Animal inherited from this class
m.info()
Because
Dog class And
Cat class They all inherit the Animal class, so their instance objects can be used
Animal class Middle
Info method
The Cat class has added its own new methods, so instance objects of the Cat class can use either inherited methods or self added methods.
When a new method added and an inherited method have the same name, it is rewriting .
4, Private properties and methods of classes
class Animal:
def __init__(self,name,legs):
self.name = name
self.legs = legs
self.__location = "earth" # __represents private and can only be used in the parent class, cannot be inherited and used in subclasses
def info(self):
print(f'i am{ self.name }i have{ self.legs }legs ! i come from{ self.__location}')
self.__private_info()
def __private_info(self):
print("i am a private method")
class Dog(Animal):
pass
class Cat(Animal):
def walk(self):
print(f'i am a cute one{ self.name }i only have{ self.legs }bar')
#private properties are also inaccessible in subclasses
# print(f'location {self.__location}')
if __name__== '__main__':
d = Dog("dog",4)
d.info()
m = Cat("cat",2)
m.walk()
#this info the method is to start from Animal inherited from this class
m.info()
#this location cannot access private properties
# print(d.__location)
Private properties and methods can only be used in the parent class, and subclasses cannot inherit and use them, which is equivalent to private in Java.
5, Property methods of classes
class Animal:
def __init__(self,name,legs):
self.name = name
self.legs = legs
self.__location = "earth" # __represents private and can only be used in the parent class, cannot be inherited and used in subclasses
def info(self):
print(f'i am{ self.name }i have{ self.legs }legs ! i come from{ self.__location}')
#property methods can be accessed in subclasses like accessing properties location this method requires that there must be a return value
@property
def location(self):
return self.__location
@location.setter
def location(self,new_location):
self.__location = new_location
if __name__== '__main__':
m = Animal("cat",2)
m.info()
m.location = "universe"
m.info()
Here, private properties in the parent class that cannot be accessed under normal circumstances can be changed and accessed through the property method.