click below
click below
Normal Size Small Size show me how
Chapter 4
Introduction to Classes, Objects, Methods, and strings
Term | Definition |
---|---|
Method | Describes the mechanisms that actually perform its tasks. Hides from user the complex tasks that it performs, just as the accelerator pedal of a car hides from user the complex mechanisms of making it go faster. Performing tasks in apps requires methods |
Class | House(among other things) a method. In a class, you provide one or more methods that are designed to perform the classes tasks |
Object of Class | Just as someone has to build a car from its engineering drawings before you can actually drive it, you must build an object of a class before you can make an app perform the tasks the class describes |
Messages | When you drive a car, pressing its gas pedal sends a message to the car to perform a task - go faster. Similarly, you send messages to an object - each message is known as a method call and tells the called method of the object to perform its task |
Attributes | An object has attributes that are carried with the object as it's used in an app. These attributes are specified as part of the object's class. Attributes are specified by the class's instance variables |
Properties of an Object | You do not need to have access to an object's instance variables in order to use them. You should use the properties of an object. Properties contain get accessors for reading the values of variables, and set accessors for storing values into them |
Keyword void | Known as the method's return type. Indicates that a method will nor return any information to its calling method when its completes a task. |
static Method | Is special because it can be called without first creating an object of the class in which the method is declared |
Extensible Language | Each new class you create becomes a new type in C# that can be used to declared variables and create objects. New class types will be accessible to all classes in the same project; this is one reason why C# is known as an extensible language. |
1. UML Class Diagram | A diagram in which each class is modeled in a class diagram as a rectangle with three components. The top compartment contains the name of the class centered horizontally in boldface type. |
2. UML Class Diagram | The middle compartment contains the class's attributes, which correspond to instance variables and properties in C#. Bottom compartment contains the class's operations, which correspond to method in C#, and lists parameters within parentheses of operation |
Public Visibility Symbol(+) | A plus sign in is placed in front of the operation name in the bottom compartment of a UML Class Diagram to indicate that the operation is a public operation in the UML |
Parameter | Represent additional information a method needs to perform its task. A method call supplies values - called arguments - for each of the method's parameters |
Parameter List | When you declare a method, you must specify in the method's declaration whether it requires data to perform its task. To do so you place additional information in the method's parameter list, which is located in the parentheses that follow the method name |
UML Class Diagram with Parameters | The UML class diagram models a parameter by listing parameter name, followed by colon and the parameter type in the parentheses following the operation name, all in the bottom compartment of the diagram |
Global Namespace | Any classes that are not explicitly placed in a namespace are implicitly placed in the so-called global namespace |
Fully Qualified Class Name | Using the full namespace and class name. For example, using System.Console.WriteLine("Please enter the course name:");, instead of the using directive system. Most C# programmers prefer to use using directives |
Local Variables | Variables declared in the body of a method. Can be used only in that method. When method terminates, the values of its local variables are lost |
Fields | Attributes are represented as variables in a class declaration. Such variables are called fields and are declared inside a class declaration bu outside the bodies of the class's method declarations |
Instance - Variable Declarations | Most instance-variables declaration are preceded with the keyword private. Keyword private is an access modifier. Variables, properties, or methods declared with access modifier private are accessible only to member of the class in which they're declared |
Declaring Access Modifiers for Instance Variables, Methods, and Properties | Precede every field and method declaration with an access modifier. Generally, instance variables should be declared private and method and properties should be declared public. |
Implicitly Declared Classes | If the access modifier is omitted before a member of a class, the member is implicitly declared private. |
Information Hiding(Encapsulation) | Declaring instance variables with access modifier private |
Accessors | Accessors handle the details of returning and modifying data. |
Properties | Properties contain accessors and follow same naming conventions as methods and classes. A property declaration can contain a get accessor, a set accessor, or both. We name each property with capitalized name of the instance variable that it manipulates |
get Accessor | Enables a client to read the value of private instance variables. Begins with the identifier get and its body is delimited by braces. The accessor's body contains a return statement, which consists of keyword return followed by an expresssion |
set Accessor | Enables a client to modify a private instance variable. Begins with identifier set and its body is delimited by braces. Allows you to set the value of the instance variable that the property is manipulating to its contextual keyword named value |
1. Using a Property as a Variable | After defining a property, you can use it like a variable in your code. You can assign a value to a property using the =(assignment) operator which executes the property's set accessor to set the value of the corresponding instance variable. |
2. Using a Property as a Variable | Similarly, referencing the property to use its value executes the code in the property's get accessor to obtain the corresponding instance variable's value |
Default Initial Value | A value provided by C# when you do not specify the initial value. Unlike local variables, which are not automatically initialized, every field has a default initial value |
1. UML Class Diagram with a Property | Model properties in the UML as attributes-the property is listed as a public attribute-as indicated by the plus(+) sign-preceded by the word "property" in guillemets(<< and >>). Indicates type of property by placing colon and type after property name. |
2. UML Class Diagram with a Property | The get and set accessors of the property are implied, so they're not listed in the UML diagram |
Stereotypes | Using descriptive words in guillemets in the UML. Helps distinguish properties from other attributes and operations |
1. UML Class Diagram with Instance Variable | Although not required, in some cases, you may find it necessary to model the private instance variable of a class. Like properties, instance variables are attributes of a class and are modeled in the middle compartment of the class diagram. |
2. UML Class Diagram with Instance Variable | Instance variables are represented as attributes by listing the attribute name, followed by a colon and attribute type. To indicate that an attribute is private, a class diagram lists the private visibility symbol, a minus sign(-), before attribute name |
1. Difference Between Providing a Property with get and set Accessors and Making Corresponding Instance Variable Public | Although providing a property with get and set accessors may appear to e the same as making its corresponding instance variable public, this is not the case. A public instance variable can be read or written by an property or method the program. |
2. Difference Between Providing a Property with get and set Accessors and Making Corresponding Instance Variable Public | If an instance variable is private, the client code can access the instance variable only indirectly through the class's non-private properties or methods. This allows the lass to control the manner in which the data is set or returned. |
3. Difference Between Providing a Property with get and set Accessors and Making Corresponding Instance Variable Public | For example, get and set accessors can translate between the format of the data stored in the private instance variable and the format of the data preferred by the client |
Data-Validation | Scrutinize attempts to modify the instance variable's value to ensure that the value it receives represents a valid format. A property's set accessor can provide data validation |
Manipulating Data Within a Class Via the Class's Properties | Properties of a class should also be used by the class's own method to manipulate the class's private instance variables, even though the methods can directly access the private instance variables. This provides a more robust class that's easier to maint |
1. Automatically Implemented Properties(Auto - Implemented Properties) | When the get accessors simply returns a private instance variable's value and the set accessor siimply assigns a value to the instance variable(no other logic appears in the accessors), C# provides automatically implemented properties. |
2. Automatically Implemented Properties(Auto - Implemented Properties) | With an auto-implemented property, the C# compiler creates a private instance variable as well as the get and set accessors for returning and modifying the private instance variable. |
3. Automatically Implemented Properties(Auto - Implemented Properties) | Unlike a user - defined property, an auto - implemented property must have both a get and set accessor. This enables you to implement the property trivially, which is handy when you're first designing the class. |
4. Automatically Implemented Properties(Auto - Implemented Properties) | If you later decide to include other logic in the get or set accessors, you can simply modify the property's implementation. Here is an example of how to use an auto - implemented property... public string CourseName { get; set; } |
Code Snippets | The IDE has a feature called code snippets that allows you to insert predefined code templates into your source code. To get a list of all available code snippets, type Ctrl + k, Ctrl + x. This displays the Insert Snippet window in the code editor |
Value Types vs. Reference Types | Types in C# are divided into two categories - value types and reference types. |
Value Types | C#'s simple types(like int and double) are all value types. A variable of a value type simply contains a value of that types. Ex: int count = 7; |
Reference Types | A reference - type variable(or reference) contains the address of a location in memory where the data referred to by that variable is stored. Such a variable is said to reference an object in the program. Ex: GradeBook myGradeBook = new GradeBook(); |
Reference - Type Instance Variables Initialization | Reference-type instance variables are initialized by default to value null. string is reference type. String variable with value null are not empty strings, represented by "" or string.empty. Value null represents a reference that does not refer to object |
1. Invoke | A client of an object must use a variable that refers to the object to invoke(call) the object's methods and access the object's properties. |
2. Invoke | Ex: myGradeBook.CourseName = Console.ReadLine(); uses the reference myGradeBook to set the course name by assigning a value to property CourseName. This sends a message to the GradeBook object to invoke the CourseName property's set accessor. |
3. Invoke | This message includes as an argument the value read from the user's imput that CourseName's set accessor requires to perform its task. The set accessor uses this information to set the courseName instance variable |
1. Construtor | Can be used to initialize an object of a class when the objects is created. C# requires a constructor call for every object that's created. The new operator call the class's constructor to perform the initialization. |
2. Constructor | The constructor call is indicated by the class name, followed by parentheses. The compiler provides a public default constructor with no parameters in any class that does not explicitly define a constructor, so every class has a constructor. |
Public Default Constructor | The compiler provides a public default constructor with no parameters in any class that does not explicitly define a constructor, so every class has a constructor. The default constructor does not modify the default values of the instance variables |
1. Custom Initializations with Constructors | When you declare a class, you can provide your own constructor to specify custom initializations for objects of you class. For example, consider GradeBook myGradeBook = new GradeBook("Computer Science");. |
2. Custom Initiializations with Constructors | In this case, the argument, "Computer Science" is passed to the GradeBook object's constructor and used to initialize the CourseName. Each time you create a new GradeBook object, you can provide a different course name. |
1. Using Custom Constructors | Constructors normally declared public. If class does not explicitly define constructor, class's instance variables are initialized to default values - 0 for numeric types, false for bool and null for reference types. |
2. Using Custom Constructors | If you declare any constructors for a class, C# will not create a default constructor for that class |
UML Class Diagram with Constructor | UML models constructors in 3rd compartment with the word "constructor" between guillemets(<< and >>) before constructors name and variable name and type in parentheses after it. Listed first in compartment. Ex... +<<constructor>> GradeBook(name : string) |
C#'s Three Simple Types for Storing Real Numbers | C# provides three simply types for storing realy number - float, double, and decimal. |
Floating-Point Types | Types float and double. Primary difference between them and decimal is that decimal variables store a limited range of real number precisely and floating-point variables store only approximations of real number, but across a much greater range of values |
1. Real-Number Precision and Storage Requirements | Variables of type float represent single-precision floating-point numbers and have seven significant figures. Variables of type double represent double-precision floating point numbers. |
2. Real-Number Precision and Storage Requirements | These require twice as much storage as float variables and provide 15-16 significant figs - approximately double the precision of float variables. Variables of type decimal require twice as much storage as double and provide 28-29 significant digits |
Floating-Point Literals | Most programmers represent floating-point numbers with type double. C# treats all real numbers typed in source code(such as 7.33 and 0.0975) as double values by default.Such values in the source code are known as floating-point literals |
Decimal Literals | To type a decimal literal, you must type the letter "M" or "m" (which stands for "money") at the end of a real number. For example, 7.33M is a decimal literal rather than a double. |
Format Specifier | Formats strings. Ex: {0:C}. The : after the 0 indicates that the next character represents a format specifier, and the C format specifier after : specifies a monetary amount(C is for currency). Using this format, 50 displays as $50.00. |
Format Specifier C or c. Ex: {0:C} or {0:c} | Formats the string as currency. Displays an appropriate currency symbol($ in the U.S.) next to the number. Separates digit with an appropriate separator character(comma in the U.S.) and sets the number of decimal places to two by default |
Format Specifier D or d. Ex: {0:D} or {0:d} | Formats the string as a whole number(integer types only). Displays number as an integer |
Format Specifier N or n. Ex: {0:N} or {0:n} | Formats the string with a thousands separator and a default of two decimal places |
Format Specifier E or e. Ex: {0:E} or {0:e} | Formats the number using scientific notation with a default of six decimal places |
Format Specifier F or f. Ex: {0:F} or {0:f} | Formats the string with a fixed number of decimal places(two by default) |
Format Specifier G or g. Ex: {0:G} or {0:g} | Formats the number normally with decimal places or using decimal notation, depending on context. If a format item does not contain a format specifier, format G is assumed implicitly |
Format Specifier X or x. Ex: {0:X} or {0:x} | Formats the string as hexadecimal |
Local Variables and Initialization | A local variable can be used only in the method in which it's declared. The compiler does not allow a local variable's value to be read until it's initialized. |
Convert Method ToDecimal | Extracts a decimal value from a string |
Declaring get and set Accessors with Different Access Modifiers | To do so, one accessor must implicitly have the same access as the property and the other must be declared with a more restrictive access modifier than the property. Private is more restrictive than public |
1. Difference Between Local and Instance Variables | A local variable is declared in the body of a method and can be used only in the method in which it's declared. An instance variable is declared in a class's but not in the body of any of the class's members. |
2. Difference Between Local and Instance Variables | Every object(instance) of a class has a separate copy of the class's instance variable. Also, instance variables are accessible to all members of the class |
Difference Between a Parameter and an Argument | A parameter represents additional information that a method requires to perform its task. Each parameter required by a method is specified in method's declaration. An argument is the actual value that's passed to a method parameter when a method is called |