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

Encapsulation (OOP)

What is encapsulation?

  • In A Level Computer Science, encapsulation refers to the practice of grouping data (attributes) and methods (functions) within a class

  • Using encapsulation ensures that data remains secure and is not accidentally modified or misused by controlling access to them using access modifiers (e.g., public, private)

  • It also helps to organize code by keeping related data and methods together within an object

  • Encapsulation promotes code reusability, which means the same object or class can be used in different parts of a program without rewriting the code

  • Encapsulation uses a concept called “Abstraction” which reduces complexity by hiding the implementation details of the object, making it easier to understand and work with

  • Programmers can use methods and classes from other parts of the program without having to understand how that it has been constructed internally

Encapsulation in classes

  • Private variables are only accessible within the class itself, and external code cannot access them directly.

  • Encapsulation hides how things work inside a class from the outside. External code can interact with the class using public methods without needing to understand its internal details

Encapsulation in classes

Encapsulation in classes

Encapsulation in methods

Encapsulation in methods

Encapsulation in methods

Examiner Tips and Tricks

  • When determining whether a method or attribute is public or private, if neither keyword appears, then assume it is public

Programming encapsulation (OOP)

What is a get method?

  • The “get” method is a common naming convention for a type of method that is used to retrieve the value of an object’s private attributes (instance variables)

  • These methods are also known as “getter” methods or “accessor” methods

  • The main purpose of a get method is to provide controlled access to the internal state (data) of an object without allowing direct modification

  • Attributes are often declared as private to achieve encapsulation, so cannot be accessed directly from outside the class

  • External code can use the public get methods to read the values of these private attributes (also known as instance variables)

Pseudocode

Pseudocode for a get method

Pseudocode for a get method

Java

//creating a class called NumberUpdater public class NumberUpdater { //setting the private attribute to the value of 10 private int number = 10; //creating a get method that will return the value store in the number attribute public int getNumber() { return number; } }

Python

#creating a class called NumberUpdater class NumberUpdater: #setting the private attribute to the value of 10 def __init__(self): self.__number = 10 #creating a get method that will return the value stored in the number attribute @property def number(self): return self.__number

What is a set method?

  • The “set” method is a common naming convention for a type of method that is used to set the value of object’s private instance variables

  • Setter methods, also known as “setter” methods or “mutator” methods

  • Generally additional code is written within the method to allow controlled access by enforcing certain conditions or validations before updating the attribute

Pseudocode

 

Pseudocode for a "setter" method

Pseudocode for a “setter” method

 Java

//creating a class called NumberUpdater public class NumberUpdater //setting attribute value to 10 private int oldnumber = 10; //creating a set method public void setNumber(int newnumber) { //if the new number is less than zero if (newnumber < 0) { //oldnumber is kept the same oldnumber = oldnumber; } else { //oldnumber is updated with the new number value oldnumber = newnumber; } } } 

Python

//creating a class called NumberUpdater class NumberUpdater: //setting attribute value to 10 def __init__(self): self.__oldnumber = 10 //creating a set method def setNumber(self, newnumber): //if the new number is less than zero if newnumber < 0: //oldnumber is kept the same self.__oldnumber = self.__oldnumber else: //oldnumber is updated with the new number value self.__oldnumber = newnumber

Worked Example

A computer game is being designed that will include different vehicles. A prototype for the game is being developed using object‑oriented programming.

The class Vehicle stores data about the vehicles. Each vehicle has an identification name, a maximum speed, a current speed and a horizontal position. The value IncreaseAmount is added to the current speed each time the vehicle increases its speed.

Class diagram for "Vehicle" showing attributes: ID, MaxSpeed, CurrentSpeed, IncreaseAmount, HorizontalPosition, and methods: Constructor, Get/Set functions.

Write program code for the get methods GetCurrentSpeed(),

GetIncreaseAmount(), GetMaxSpeed() and GetHorizontalPosition() [3]

Answer

  • 1 get function header (and end where appropriate) with no parameter … [1 mark]

  • …returning attribute (without overwriting) [1 mark]

  • 3 further correct get methods [1 mark]

Example program code:

VB.NET

Function GetCurrentSpeed() Return CurrentSpeed
End Function
Function GetIncreaseAmount() Return IncreaseAmount
End Function
Function GetHorizontalPosition() Return HorizontalPosition
End Function
Function GetMaxSpeed() Return MaxSpeed
End Function

Java

public Integer GetCurrentSpeed(){ return CurrentSpeed;
}
public Integer GetIncreaseAmount(){ return IncreaseAmount;
}
public Integer GetHorizontalPosition(){ return HorizontalPosition;
}
public Integer GetMaxSpeed(){ return MaxSpeed;
}

Python

def GetCurrentSpeed(self): return self.__CurrentSpeed
def GetIncreaseAmount(self): return self.__IncreaseAmount
def GetHorizontalPosition(self): return self.__HorizontalPosition
def GetMaxSpeed(self): return self.__MaxSpeed

Responses

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