Python Notes (7) - Introduction

2018-03-20  本文已影响20人  SilentSummer

转载请注明出处: http://blog.csdn.net/cxsydjn/article/details/71303658

The note focuses on Classes, which are a crucial part of object-oriented programming (OOP). In this lesson, we'll explain what classes are, why they're important, and how to use them effectively.

Python notes of open courses @Codecademy.

Class Basics

Why Use Classes?

Python is an object-oriented programming (OOP) language, which means it manipulates programming constructs called objects.

You can think of an object as a single data structure that contains data as well as functions; functions of objects are called methods.

A class is just a way of organizing and producing objects with similar attributes and methods.

Class Syntax

Example

By Now, We Have:

# Class definition
class Animal(object):
    """Makes cute animals."""
    # For initializing our instance objects
    def __init__(self, name, age, is_hungry):
        self.name = name
        self.age = age
        self.is_hungry = is_hungry

# Note that self is only used in the __init__() function definition;
# we don't need to pass it to our instance objects.

zebra = Animal("Jeffrey", 2, True)

print zebra.name, zebra.age, zebra.is_hungry

Member Variables and Functions

Class Scope

Another important aspect of Python classes is scope. The scope of a variable/function is the context in which it's visible to the program.

Not all variables are accessible to all parts of a Python program at all times. When dealing with classes, you can have variables/functions that are:

Inheritance

Inheritance is the process by which one class takes on the attributes and methods of another, and it's used to express an is-a relationship. For example, a Panda is a bear, so a Panda class could inherit from a Bear class.

Inheritance Syntax

Example

By Now, We Have A Comprehensive Example:

# 1. Creating a class
class Car(object):
    # 3. Creating member variables
    condition = "new"
    # 5. Initializing a class
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
        
    # 6. Creating class methods
    def display_car(self):
        # 7. Referring to member variables
        return "This is a " + self.color + " " + self.model + " with "+ str(self.mpg) + " MPG."
        
    def drive_car(self):
        # 8. Modifying member variables
        self.condition = "used"
        return self.condition  
        
# 9. Inheritance
class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg   = mpg
        self.battery_type = battery_type
    # 10. Overriding methods
    def drive_car(self):
        self.condition = "like new"
        return self.condition 

# 2. Creating an instance of a class
my_car = Car("DeLorean", "silver", 88)
# 4. Calling class member variables
print my_car.condition
my_car.drive_car()
print my_car.condition

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")
print my_car.condition
my_car.drive_car()
print my_car.condition

External Resources

上一篇 下一篇

猜你喜欢

热点阅读