Encapsulation and Classes
Classes let you model the things your program works with, keeping their data and behavior together. You will define classes, create objects, write constructors, and protect data with properties.
Updated
Working with Objects in C#
- Working with Objects in C#: an overview (Novice): Object-oriented programming models software on the things around us, objects that each know some facts and do some jobs.
- What is an Object? (Novice): Every object has a responsibility; finding the objects in a problem, and the job of each, is the first step in designing a program.
- Defining an Object (Apprentice): An object's responsibilities split into what it knows (its state) and what it does (its behavior), and a class is the blueprint that describes both.
- Writing the Class in C# (Apprentice): Turn a class design into C# with private fields and public methods, then create objects with new and call their methods.
- What is a Constructor? (Apprentice): A constructor runs when an object is created and gives it the starting values it needs; use this to tell a field from a parameter of the same name.
Properties in C#
- Fields vs Properties (Apprentice): A property looks like a field from outside but runs code inside, so a class can let others read and set a value while it checks every change.
- Auto-Properties and Read-Only Properties (Apprentice): Write short auto-properties, make a property read-only to the outside with private set or get-only, and calculate a property from others.
- Methods on a Class (Apprentice): Write instance methods that use and change an object's state safely, keep its rules inside the class, and know when a method should be static.
Exercise: Classes
- Exercise: Classes (Apprentice): Write six classes from a specification (Rectangle, Circle, MathTest, Printer, Elevator and Airplane), choosing each property's access, and let the tests check them.