310 likes | 496 Views
Addison Wesley is an imprint of. Chapter 6. Advanced Classes. Updated: 3/22/2011. Contents. 6.1 Structures 6.2 Components 6.3 Unit Testing 6.4 Events 6.5 Inheritance. 6.1 Structures. Container for variables, properties, methods, and events lightweight class
E N D
Chapter 6 Advanced Classes Updated: 3/22/2011
Contents • 6.1 Structures • 6.2 Components • 6.3 Unit Testing • 6.4 Events • 6.5 Inheritance
6.1 Structures • Container for variables, properties, methods, and events • lightweight class • A structure variable holds its own data • unlike a class instance, which contains a reference • The New operator is only required when calling a constructor with parameters
Copying and Comparing Structures • Assignment operator (=) copies all structure fields • Equals method compares the contents of structure fields • Sample Structure declaration: • Structure Point • Public Property X As Integer • Public Property Y As Integer • End Structure
6.2 Components • A .NET assembly is a basic unit of deployment that is compiled into a DLL file • A component is a collection of related classes that belong to a single assembly • aka class library • When possible, a component should be reusable • General types: • interface components • code components
Tutorial 6-1 • Creating a Component and Referencing it From Another Application • RegistrationLib component • contains a Student class • You create a second application that references the RegistrationLib component • uses an Imports statement: • Imports RegistrationLib
Tutorial 6-2 • Adding an Advisor Class to the RegistrationLib Component • Middle tier class • Determines the maximum number of credits that a student can take, based on various criteria
Tutorial 6-3 • Using the Advisor and Student Classes • The Registration UI application calls methods in the RegistrationLib component • Advisor and Student classes
6.3 Unit Testing • Continuous software testing • doesn't leave testing to the end of the project • Automated tests • easier to repeat than manual tests • Written at the same time as the application code • May be run again at any time • (regression test)
Two Testing Paradigms • White box testing • tester can view the application source code • Black box testing • tester can only see the relation between inputs and outputs. Cannot see source code.
Unit Testing Basics • Each unit test is designed to test one particular code unit of an application. • When a unit test fails, it stops executing and returns immediately. • Unit tests do not run in any predetermined sequence. • Unit tests are executed by a utility program known as a test engine.
Unit Testing in .NET • Microsoft.VisualStudio.TestTools.UnitTesting namespace • Steps: • Create a set of classes to be tested. • Create a Visual Studio test project. • Add unit tests to the test project. • Run the Visual Studio testing tool.
Unit Testing Attributes • TestClass – identifies a class that contains unit tests • TestMethod – identifies a method that performs a unit test
Tutorial 6-4 • Creating a Unit Test Project • Test the IntArray class • method that finds the largest value in an array <TestClass()> _ Public Class IntArrayTest <TestMethod()> _ Public Sub GetLargestTest() Dim target As IntArray = New IntArray() target.Data = {40, 16, 12, 22, 0, -33} Dim expected As Integer = 40 Dim actual As Integer = target.GetLargest Assert.AreEqual(expected, actual) End Sub End Class
Tutorial 6-5 • Creating More Unit Tests for the IntArray Class • Rearrange the order of the integers • Fix errors as they are found when running the tests • Include a variety of test values, including an empty array
Assert Class Methods • The Assert class provides methods to compare expected and actual values in unit tests • Commonly used Assert methods: • AreEqual, AreNotEqual, AreSame, AreNotSame, Fail, IsFalse, IsTrue, IsNull, IsNotNull
Tutorial 6-6 • Testing the Advisor.MaxCredit Method • Example of testing an existing application • RegistrationLib component • New tests:
6.4 Events • Provide a signaling system for messages that must be broadcast from an object to other objects that are listening for the messages. • A delegate is a template that describes the return type and parameter list for a related group of event handler methods • An object declared using the WithEvents qualifier can raise events at runtime
Tutorial 6-7 • The WeatherStation Events Application • Demonstrates the raising and handling of events for a simulated weather station • User interface:
6.5 Inheritance • Inheritance refers to a parent-child relationship between two classes • A derived class builds on attributes and behaviors of a base class • attributes = properties • behaviors = methods and events
Multiple Derived Classes • Any number of derived classes can inherit from the same base class:
Heroes and Villains Example • Classes from a computerized role-playing game When a derived object is constructed, its base class constructors execute before the object’s own constructor executes. When a Hero is constructed, the compiler automatically calls the default constructor for Person.
Assigning Object References • Object references can be assigned upward in the inheritance hierarchy from a derived type to a base type: • Dim P As Person = New Hero("Aquaman", "Swims") • Assigning a base type to a derived type always requires the use of a downward cast operation • Example: • Dim P As New Person("Joe") • Dim Z As Hero = CType(P, Hero)
Overriding and Overloading • To override a method is to replace a base class method with a derived class method having the same signature. • To overload a method is to create a new method having the same name as an existing method in the same class or a base class. The new method must have a different parameter list.
Tutorial 6-8 • Student Inheritance Application • Creates a collection of these types: • Student (undergraduate students) • GradStudent (graduate students)
Key Terms • Assert.AreEqual Method • Assert.AreNotEqual Method • Assert class • assertion • attribute name • automated test • base class • black box testing • class library • continuous software testing • delegate • component • derived class • downward cast handle an event inheritance Inherits keyword .NET assembly overload a method override a method Overrides keyword raise an event regression testing test engine test project unit test upward cast white box testing