Wątki

[ Pobierz całość w formacie PDF ]
.Following is an example:class Token{public virtual string Name( ) {.}}abstract class Force: Token{public abstract override string Name( );}Abstract Methods Can Override Base Class MethodsDeclared As OverrideOverriding a base class method declared as override forces a further derivedclass to provide its own method implementation and makes the originalimplementation of the method unavailable.Following is an example:class Token{public virtual string Name( ) {.}}class AnotherToken: Token{public override string Name( ) {.}}abstract class Force: AnotherToken{public abstract override string Name( );} 50 Module 10: Inheritance in C#This page intentionally left blank. Module 10: Inheritance in C# 51Quiz: Spot the Bugsclassclass FirstFirst{{111publicpublic abstract void Method( );abstract void Method( );}}abstractabstract class Secondclass Second{{22public 2public abstract void Method( ) { }abstract void Method( ) { }}}interfaceinterface IThirdIThird{{voidvoid Method( );Method( );3}} 33abstractabstract class Third: IThirdclass Third: IThird{{}}In this quiz, you can work with a partner to spot the bugs in the code on theslide.To see the answers to this quiz, turn the page. 52 Module 10: Inheritance in C#AnswersThe following bugs occur in the code on the slide:1.You can only declare an abstract method in an abstract class.The C#compiler traps this bug as a compile-time error.You can fix the code byrewriting it as follows:abstract class First{public abstract void Method( );}2.An abstract method cannot declare a method body.The C# compiler trapsthis bug as a compile-time error.You can fix the code by rewriting it asfollows:abstract class Second{public abstract void Method( );}3.The C# compiler traps this as a compile-time error.An abstract class mustprovide for the implementation of all methods in interfaces that itimplements in much the same way as a concrete class.The main differenceis that when you use an abstract class this can be achieved directly orindirectly.You can fix the code by rewriting it as follows:abstract class Third: IThird{public virtual void Method( ) {.}}Alternatively, if you do not want to implement the body of Method in anabstract class, you can declare it abstract and thus ensure that a derived classwill implement it:abstract class Third: IThird{public abstract void Method( );} Module 10: Inheritance in C# 53Lab 10: Using Inheritance to Implement an InterfaceObjectivesAfter completing this lab, you will be able to:Define and use interfaces, abstract classes, and concrete classes.Implement an interface in a concrete class.Know how and when to use the virtual and override keywords.Define an abstract class and use it in a class hierarchy.Create sealed classes to prevent inheritance.PrerequisitesBefore working on this lab, you must be able to:Create classes in C#.Define methods for classes.Estimated time to complete this lab: 75 minutes 54 Module 10: Inheritance in C#Exercise 1Converting a C# Source File into a Color Syntax HTML FileFrameworks are extremely useful because they provide an easy-to-use, flexiblebody of code.Unlike a library, which you use by directly calling a method, youuse a framework by creating a new class that implements an interface.Theframework code can then polymorphically call the methods of your class bymeans of the interface operations.Hence, a well-designed framework can beused in many different ways, unlike a library method, which can only be used inone way.ScenarioThis exercise uses a pre-written hierarchy of interfaces and classes that form aminiature framework.The framework tokenizes a C# source file and stores thedifferent kinds of tokens in a collection held in the SourceFile class.AnITokenVisitor interface with Visit operations is also provided, which incombination with the Accept method of SourceFile allows every token of thesource file to be visited and processed in sequence.When you visit a token,your class can perform whatever processing it requires by using that token.An abstract class called NullTokenVisitor has been created that implements allthe Visit methods in ITokenVisitor by using empty methods [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • mikr.xlx.pl
  • Powered by MyScript