Day 18 OOP 面向对象编程

 

Day 18 OOP 面向对象编程

day18思维导图

一 what is OOP?

Python is a multi-paradigm/ˈpærədaɪm/ 范例programming language. It supports different programming approaches. One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).

why is OOP used?

Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism /,pɒlɪ'mɔːfɪzəm/ 多态 etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function

二 Python Classes/Objects

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class

class MyClass:
x = 5

Create Object

Now we can use the class named MyClass to create objects:

p1 = MyClass()
print(p1.x)

The init() Function

The examples above are classes and objects in their simplest form, and are not really useful in real life applications.

To understand the meaning of classes we have to understand the built-in init() function.

All classes have a function called init(), which is always executed when the class is being initiated.

Use the init() function to assign values to object properties, or other operations that are necessary to do when the object is being created:

class Person:
def __init__(self, name, age):
  self.name = name
  self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)

Object Methods

Objects can also contain methods. Methods in objects are functions that belong to the object.

Let us create a method in the Person class:

class Person:
def __init__(self, name, age):
  self.name = name
  self.age = age

def myfunc(self):
  print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

The self Parameter

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class:

class Person:
def __init__(mysillyobject, name, age):
  mysillyobject.name = name
  mysillyobject.age = age

def myfunc(abc):
  print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()

三 Summary

Summary:

"Class" is a logical grouping of functions and data. Python class provides all the standard features of Object Oriented Programming.

  • Class inheritance mechanism/ˈmekənɪzəm/机制

  • A derived派生的 class that override推翻,重写 any method of its base class

  • A method can call the method of a base class with the same name

  • Python Classes are defined by keyword "class" itself

  • Inside classes, you can define functions or methods that are part of the class

  • Everything in a class is indented, just like the code in the function, loop, if statement, etc.

  • The self argument in Python refers to the object itself. Self is the name preferred by convention by Pythons to indicate the first parameter of instance methods in Python

  • Python runtime will pass "self" value automatically when you call an instance method on in instance, whether you provide it deliberately or not

  • In Python, a class can inherit attributes and behavior methods from another class called subclass or heir class.

四 Data hiding

In Python, we use double underscore下划线 before the attributes name to make them inaccessible/private or to hide them

An object's attributes may or may not be visible outside the class definition. You need to name attributes with a double underscore prefix前缀, and those attributes then are not be directly visible to outsiders.

class JustCounter:
  __secretCount = 0
  def count(self):
    self.__secretCount += 1
    print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount

Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName. If you would replace your last line as following, then it works for you −

.........................
print counter._JustCounter__secretCount

 

posted @ 2021-01-09 08:08  fengshili0721  阅读(115)  评论(0编辑  收藏  举报