Object-Oriented Programming/Properties

From Wikiversity
Jump to navigation Jump to search

This lesson introduces object properties, attributes, and fields.

Objectives and Skills[edit]

Objectives and skills for this lesson include:[1]

  • Understand the fundamentals of classes
    • Understand properties, methods, events, and constructors
    • How to create a class
    • How to use classes in code

Readings[edit]

  1. Wikipedia: Property (programming)
  2. Wikipedia: Mutator method

Multimedia[edit]

Examples[edit]

Temperature
+ celsius: float
+ fahrenheit: float
+ Temperature(celsius: float = None,
    fahrenheit: float = None)
+ to_celsius(fahrenheit: float)
+ to_fahrenheit(celsius: float)

Activities[edit]

BMI_Calculator
+ bmi: float {read only}
+ feet: float
+ inches: float
+ kilograms: float
+ meters: float
+ pounds: float
+ BMI(feet: float = 0, inches: float = 0,
    kilograms: float = 0, meters: float = 0,
    pounds: float = 0)
+ calculate_metric(kilograms: float, meters: float)
+ calculate_us(pounds: float, feet: float, inches: float)
- feet_to_inches(feet: float)
- inches_to_meters(inches: float)
- kilograms_to_pounds(kilograms: float)
- meters_to_feet_and_inches(meters: float)
- pounds_to_kilograms(pounds: float)
  1. Extend the BMI class from the previous lesson. Add properties for BMI, feet, inches, kilograms, meters, and pounds. Add a constructor that optionally accepts feet, inches, kilograms, meters, and/or pounds as named parameters. The BMI property should be read-only.
  2. Update the main program to use class properties instead of calculate methods.
  3. Avoid global variables by passing parameters and returning results. Include program, class, and method documentation consistent with the documentation standards for your selected programming language.

Lesson Summary[edit]

  • A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method.[2]
  • Property reads and writes are usually translated to getter and setter method calls.[3]
  • Some object-oriented languages, such as Java and C++, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead.[4]
  • In most languages, properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a read-only or an uncommon write-only property.[5]
  • A mutator method is a method used to control changes to a variable. They are also widely known as setter methods.[6]
  • Often, a setter is accompanied by a getter—also known as an accessor—which returns the value of the private member variable.[7]
  • Member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.[8]

Key Terms[edit]

getter
Also known as an accessor, a getter is a method used to return the value of the private member variable.[9]
property
A type of class member that can expose fields.[10]
read-only field
A field that is capable of being read but not written.[11]
setter
Also known as a mutator method, a setter is a method used to control changes to a variable.[12]

See Also[edit]

References[edit]