Chapter 7 : Class Flashcards
What is a class?
- The blueprint for an object
What does a class specifies ? ( 2 )
- Fields
- Methods
What can be created from the class?
- Object
What does the objected created from a class called?
- Instance of the class
How to create a class?
class className
{
Member declaration(s)
}
What is a constructor?
- Special method automatically executed when an object is created
How to create a object?
className objectName - new constructor
- Coin myCoin = new Coin();
- myCoin - Identifie that references an object of the Coin class
- new - creates an instance of the Coin class
- = - assigns the reference that was returend from the new operator to the myCoin identifie
Where to create class? ( 2 )
- Save the class declaration in a separate .cs file
- Add it inside Form.cs file
What is a property?
- A class member that holds a piece of data about an object
What is a backing field?
- A variable that stores a value assigned to the property which the backing fields is associated with
What is the access method of backing field?
- private
- Protect it from accidental corruption
What does it do when a backing vield is public?
- Accessible directly by code outside the class without the need for accessors
What does the get accessor do?
- It is a method that returns the property’s value because it has a return statement
What does the set accessor do?
- It is a method gets the value stored in the backing field and assigns that value to the property
When is get accessor executed?
- It is executed whenever the property is read
- It has an implicit parameter named value
When is set accessor is executed?
- Whenever a value is assigned to the property
What is a read-only property and how to set a read-only property?
- A read-only property can be read, but it cannot be meodified
- To ser a read-only property, simply do not write a set accessor for the property
What is a parameterized constructor?
- A constructor that accepts arguments
What is overloaded methods?
- Multiple versions of the same method
How does the compiler know which method to call?
- Relies on the signarutre of a method which consists of the methods’s name, the data type, and argument kind of the method’s parameter
When is a method is overloaded?
- Means that multiple methods in the same class have the same name but use different types of parameters
Can constructors be overloaded?
- Yes
- Compiler will find the matching constructor automatically
- public BankAccount() { } // parameterless constructor
public BankAccount(decimal startingBalance) { } // overloaded
public BankAccount(double startingBalance) { } // overloaded
How to I identify the classes? ( 3 )
- Get a written description of the problem domain
- Identify all the nouns ( pronouns and noun phrases ) in the description . Each of these is a potential class
- Refine the list to include only the class that are relavant to the problem
What am I going to do once the class have been identified?
- Identify each class’s responsibilities
- The things that the class is responsible for knowing -> variable, arrary, list
- The actions that the class is responsible for doing
How to display a form?
ErrorForm myErrorForm = new ErrorForm();
myErrorForm.ShowDialog();
How to access labelText from different class?
GreetingsForm greetingsForm = new GreetingForm();
greetingsForm.messageLabel.Text = “Good day!”
greetingsForm.ShowDialog();
What does a modal form do?
- Must be closed or hidden before we can continue working with the rest of the application
What does a modeless form do?
- Allows the user to switch focus to another form while it is displayed
- Does not have to close a modeless form to switch focus to another form
What is the Flow of Executions for modal form?
- Statement below ShowDialog method won’t be execute until the modal form is closed
statement;
statement;
messageForm.ShowDialog();
statement;
statement; // not executed
statement;
What is the Flow of Executions of modeless form?
- Statement below ShowDialog method will execute immediately after the modeless from is displayed
statement;
statement;
messageForm.Show();
statement;
statement; // executed
statement;