返回卡包市场

Chapter 58 - Use of Object-Oriented Programming (OOP)

暂无描述。系统推荐的高质量记忆内容,适合每天坚持背诵学习。

卡片总数: 21内容版本: v4公开卡包更新时间: 8/1/2026

卡片预览 (21 张)

#1
正面 (问题)

What is Object-Oriented Programming?

背面 (解答)

Object-Oriented Programming (OOP) attempts to capture data, information and related functionality (code) to form structured items known as objects. In OOP, the world is viewed as a collection of objects.

#2
正面 (问题)

What are objects and what can they be.

背面 (解答)

Objects are instances of classes (defined in the main body). They can be anything: from specific things such as cars and planes, to more abstract things such as bank accounts or data structures (stacks).

#3
正面 (问题)

Object features

背面 (解答)

Objects have their own attributes, states and methods (behaviours)

#4
正面 (问题)

Attributes

背面 (解答)

Attributes are variables or data members that belong to a class and represent its characteristics or state

#5
正面 (问题)

State

背面 (解答)

A state of an object is represented by its attributes e.g the radio is on/off

#6
正面 (问题)

Methods/Behaviours

背面 (解答)

Actions performed by an object (think of them like subroutines) often by altering attributes.

#7
正面 (问题)

What is a class?

背面 (解答)

A class is a blueprint/template for an object and it defines the attributes and behaviours (methods) of objects in that class. These attributes are defined as the instance variables within a class.

#8
正面 (问题)

Sections of a class

背面 (解答)

• Name of the class • The attributes: all the information shared with any object of this class type. Like variables. • The methods/behaviours: the code associated with the class that allows you to change/access its attributes. Like subroutines

#9
正面 (问题)

Information hiding

背面 (解答)

As a general rule, attributes/instance variables are defined as private whereas most methods are defined as public. This allows other classes to access the methods but not see/change the attributes. Therefore objects need to use messages to interact with with another object’s state.

#10
正面 (问题)

What is a constructor? What does the spec say about constructors?

背面 (解答)

A constructor initialises the attributes within a class. Constructors are procedures with the name new. E.g def __init__ new (someAttributes).

#11
正面 (问题)

Instantation

背面 (解答)

An instance of a class - the creation of an object They have the name new when object is created. E.g my_dog = new Dog(someArguments)

#12
正面 (问题)

Access the attribute “name” from the object my_dog. Then print it

背面 (解答)

print(my_dog.name)

#13
正面 (问题)

Make the dog bark using the “bark” method. Then print

背面 (解答)

print(my_dog.bark())

#14
正面 (问题)

Messages

背面 (解答)

Messages can either be getter or setter messages. In some languages, getter messages are written as functions (returning the value you wanted to get) wheras setter messages are written as procedures (usually used to change the state of the object).

#15
正面 (问题)

Summary of OOP

背面 (解答)

Each object belongs to a class All objects of the same class have the same structure and methods but they have their own data Objects belonging to a class are instances of that class.

#16
正面 (问题)

Encapsulation

背面 (解答)

This is where an object encapsulates its states (value of attributes/instance variables) and its behaviours/methods into a single entity so the attributes/behaviours of one object can’t affect those of the other.

#17
正面 (问题)

Inheritance

背面 (解答)

Classes can inherit attributes and methods from a parent class. A child in OOP is known as a subclass and a parent class is known as a superclass

#18
正面 (问题)

A car class inherits the attributes of a vehicle class. What would this look like?

背面 (解答)

class Vehicle: def __init__new(self,doors,price): self.doors = doors self.price = price class Car(Vehicle): def __init__new(self,doors,price,wheels): super().__init__(doors,price) #super.new(doors,price) self.wheels = wheels

#19
正面 (问题)

Polymorphism

背面 (解答)

Polymorphism enables methods to do different things based on the object they are acting upon. An example is overriding.

#20
正面 (问题)

Overriding

背面 (解答)

Overriding: defining a method with the same name and formal argument types as a method inherited from a superclass.

#21
正面 (问题)

Polymorphism example

背面 (解答)

class Animal: def __init__ new (self,sound): self.sound = sound def makeSound(self): pass class Dog(Animal) def__init__ new (self,sound): super().__init__ new (sound) def makeSound(self): return f”The Dog goes {self.sound}”