Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Methods (OOP)

What is a method?

  • Methods are fundamental in object-oriented programming (OOP) and are functions associated with objects or classes that define the behaviour and actions that objects can perform

  • There are two main types of methods:

    • A function

      • A function performs a task and returns a value

    • A procedure

      • A procedure performs a task buts does nor return a value

  • For example, there are many different types of aircraft which all differ in design, but all require a ‘take off’ and ‘landing’ method

  • An example of an aircraft class with both attributes and methods is shown below:

Example class for "aircraft" containing several methods

Example class for “aircraft” containing several methods

  • Each object that is created from the aircraft class will have:

    • A manufacturer value to determine the company that created the aircraft

    • A model name for the type of aircraft

    • A value for passenger capacity to determine how many people it can carry

    • A speed value to determine its maximum speed

  • Any objects that are created for the aircraft class also have access to the following Methods:

    • A take off method to get the plane airborne

    • A land method to land the plane safely

    • A bank left method to allow the plane to turn to the left

    • A bank right method to allow the plane to tur to the right

  • As you can imagine, for an aircraft there would be many more methods that could be implemented such emergency landing and altitude cruising actions

  • Once objects have been created, they can use the methods from within their class by using the dot (.) notation

  • As these methods are associated with objects, they are called instance methods

  • For example if an object has been created as below:

    • jumboJet = new aircraft (“Boeing”, ”747”, 416, 547)

  • It can use methods by doing the following:

    • Objectname.Methodname:

    • For example: jumboJet.Takeoff()

Examiner Tips and Tricks

  • In some OOP languages there are both instance methods and static methods

    • Instance methods

      • Instance methods are associated with individual instances (objects) of a class. They operate on the specific data and properties of an object (see above on the Jumbo Jet object example)

    • Static methods

      • Static methods are associated with a class itself and can be called without creating an instance (object) of the class

Public and private methods

  • When declaring methods, it is important to determine how they can be accessed:

Public Methods

Private Methods

Accessible and can be invoked by any code within the same class or from any external classes

Private methods are only accessible within the same class and cannot be invoked by external code or other classes

Changes to public methods may have an impact on other parts of the codebase, so they should be carefully designed, documented, and backward compatible whenever possible

Changes to private methods have a localized impact since they are only used internally within the class, providing flexibility to modify or refactor them without affecting other parts of the program

Public methods are used when you want to provide access to certain functionalities or behaviours of an object or class to other parts of your program

Private methods are used when you have internal implementation details that should not be accessed or used by external code. They are meant to be used only within the class itself for organizing and managing the code internally

Examiner Tips and Tricks

  • If a method does not specify the keyword public or private then its default value is set to public.

Programming methods (OOP)

How do you program a method?

Pseudocode

pseudocode for creating methods

Pseudocode for creating methods

 

Java

//Creating a public method called BankLeft public void bankLeft() { // Code to make the aircraft bank left } //Creating a private Method called BankLeft public void bankLeft() { // Code to make the aircraft bank left }

Python

#Creating a public method called BankLeft def bank_left(self): # Code to make the aircraft bank left #Creating a private method called BankLeft
  • In Python, there is no explicit access modifier for methods like “private” in Java

  • By convention, methods with a single leading underscore _ are considered private and should not be accessed directly from outside the class

_def _bank_left(self): # Code to make the aircraft bank left
  • It’s important to note that even though the method is marked as private, it can still be accessed and invoked from outside the class

  • However, as a rule, other developers should treat it as private and avoid accessing it directly

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注