1 / 31

Addison Wesley is an imprint of

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

Download Presentation

Addison Wesley is an imprint of

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Addison Wesley is an imprint of

  2. Chapter 6 Advanced Classes Updated: 3/22/2011

  3. Contents • 6.1 Structures • 6.2 Components • 6.3 Unit Testing • 6.4 Events • 6.5 Inheritance

  4. 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

  5. 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. 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

  7. 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

  8. 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

  9. Tutorial 6-3 • Using the Advisor and Student Classes • The Registration UI application calls methods in the RegistrationLib component • Advisor and Student classes

  10. 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)

  11. 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.

  12. 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.

  13. 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.

  14. Unit Testing Attributes • TestClass – identifies a class that contains unit tests • TestMethod – identifies a method that performs a unit test

  15. 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

  16. 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

  17. 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

  18. Tutorial 6-6 • Testing the Advisor.MaxCredit Method • Example of testing an existing application • RegistrationLib component • New tests:

  19. 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

  20. Tutorial 6-7 • The WeatherStation Events Application • Demonstrates the raising and handling of events for a simulated weather station • User interface:

  21. 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

  22. Examples of Inheritance

  23. Multiple Derived Classes • Any number of derived classes can inherit from the same base class:

  24. Accessing Base Class Members

  25. Access Modifiers

  26. 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.

  27. 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)

  28. 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.

  29. Tutorial 6-8 • Student Inheritance Application • Creates a collection of these types: • Student (undergraduate students) • GradStudent (graduate students)

  30. 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

  31. The End

More Related