Object-Oriented Programming/Properties
Appearance
This lesson introduces object properties, attributes, and fields.
Objectives and Skills
[edit | edit source]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 | edit source]- Wikipedia: Property (programming)
- Wikipedia: Mutator method
- Wikipedia: Constructor (object-oriented programming)
Multimedia
[edit | edit source]C#
[edit | edit source]JavaScript
[edit | edit source]Python
[edit | edit source]- YouTube: Introduction to Python 3 property() function and @property decorator
- YouTube: Python OOP Tutorial 6: Property Decorators - Getters, Setters, and Deleters
- YouTube: Python Constructors, __init__(), self, Class and Instance Objects Explained Concisely
Examples
[edit | edit source]Temperature |
---|
+ celsius: float + fahrenheit: float |
+ Temperature(celsius: float = None, fahrenheit: float = None) + to_celsius(fahrenheit: float) + to_fahrenheit(celsius: float) |
Activities
[edit | edit source]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_inches(meters: float) - pounds_to_kilograms(pounds: float) |
- Extend the BMI class from the previous lesson. Add properties for BMI, feet, inches, kilograms, meters, and pounds. If your language supports it, add a constructor that uses optional parameters for feet, inches, kilograms, meters, and/or pounds as named parameters. The BMI property should be read-only.
- Update the main program to use class properties instead of calculate methods.
- 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 | edit source]- 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. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls, yet the interposition of method calls "under the hood" allows for data validation, active updating (e.g., of GUI elements), or implementation of what may be called "read-only fields".[2]
- By using property() method, we can modify our class and implement the value constraint without any change required to the client code. So that the implementation is backward compatible.[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]
- A constructor (abbreviated: ctor) is a special type of subroutine or method that is used to initialize newly created objects of a class or struct, often accepting arguments which the constructor uses to set member variables or attributes. There are several types of constructors which vary in function and also by language, some of which include: default constructors, copy constructors, parameterized constructors, etc. [9]
Key Terms
[edit | edit source]- constructor
- A special method used to initialize the data members of a newly created object (instance of a class).[10]
- getter
- Also known as an accessor, a getter is a method used to return the value of the private member variable.[11]
- property
- A type of class member that can expose fields.[12]
- read-only field
- A field that is capable of being read but not written.[13]
- setter
- Also known as a mutator method, a setter is a method used to control changes to a variable.[14]
See Also
[edit | edit source]C#
[edit | edit source]JavaScript
[edit | edit source]Python
[edit | edit source]- Programiz: Python @property: How to Use it and Why?
- Machine Learning Plus: Python @property Simple Explanation and Use-Case Example
- Python-Course: Properties vs. Getters and Setters
References
[edit | edit source]- ↑ Microsoft: Software Development Fundamentals Exam Details
- ↑ Wikipedia: Property (programming)
- ↑ "Python | property() function". GeeksforGeeks. 2018-09-21. Retrieved 2020-09-10.
- ↑ Wikipedia: Property (programming)
- ↑ Wikipedia: Property (programming)
- ↑ Wikipedia: Mutator method
- ↑ Wikipedia: Mutator method
- ↑ Wikipedia: Mutator method
- ↑ Wikipedia: Constructor (object-oriented programming)
- ↑ Wikipedia: Constructor (object-oriented programming)
- ↑ Wikipedia: Property (programming)
- ↑ Wikipedia: Property (programming)
- ↑ Wikipedia: Property (programming)
- ↑ Wikipedia: Property (programming)