Bài giảng An Introduction to Computer Science Using Java - Chapter 10 Classes and Methods IV: Static Methods and Variables

Tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 10 Classes and Methods IV: Static Methods and Variables: Chapter 10 Classes and Methods IV: Static Methods and VariablesLecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:introduce class variables and class methodsclass variables have only one instance and are not contained in any objectclass methods have no receiver and can only access class variablesdiscuss overloading for class methodsintroduce Java interfaces which specify object behaviordiscuss the use of methods to modularize large programsillustrate process of stepwise program refinementClass VariablesAssociated with a class, not class instanceOnly one instance of each class variable, not one instance per objectCreated by adding the modifier static to the variables declarationAllows variables to be modified once for all class objectsCan be accessed by class methodsClass MethodsMethods declared with static modifierCan be defined in any classNot associated with an objectIs no...

ppt14 trang | Chia sẻ: honghanh66 | Lượt xem: 825 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 10 Classes and Methods IV: Static Methods and Variables, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 10 Classes and Methods IV: Static Methods and VariablesLecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:introduce class variables and class methodsclass variables have only one instance and are not contained in any objectclass methods have no receiver and can only access class variablesdiscuss overloading for class methodsintroduce Java interfaces which specify object behaviordiscuss the use of methods to modularize large programsillustrate process of stepwise program refinementClass VariablesAssociated with a class, not class instanceOnly one instance of each class variable, not one instance per objectCreated by adding the modifier static to the variables declarationAllows variables to be modified once for all class objectsCan be accessed by class methodsClass MethodsMethods declared with static modifierCan be defined in any classNot associated with an objectIs not invoked by sending it as a message to an objectThe class method f in the class C is invoked using the notation f.C( )It has no receiver and cannot refer to instance variablesIt can refer to and manipulate class variablesClasses with No Instance Variables or MethodsSome classes only contain class variables and methodsSince these classes have no instance variables they need no constructorsProvide a convenient way of packaging collections of useful methodsSome collections like Math define symbolic constants that are unchangablepublic static final double E = public static final double PI = methods are all declared to be public and staticthese methods operate only on built-in data typesClasses without Instance MethodsClass ClassnameClient {// Author, date, expalnation public static void main (String[ ] args { Classname variable = new Classname( ); variable, method( ); }} Sformat ClassPurely static class taken from CSLibContains a number of overloaded methodsOverloaded methodsshare the same namediffer in their argument number or typesprintr is an example of an overloaded method in Sformat Modular DevelopmentTop-down approachproblem is viewed from the top and written in outline formthe outline is refined by looking at each section in greater detailStepwise Refinementthe process of adding detail to successive sections until the constituent parts become apparent and can be writtenthe debugging needs of each component dictate the order in which they are written InterfacesDeclared like classes using the word interface instead of classMust contain method declarations without bodiesMay contain symbolic constantsUML equivalent of Java interface is indicated by an italicized class name and a dashed lineExample:interface interface_name { definitions of symbollic constants and declarations of abstract methods}Implementing Interface MethodsClasses that contain concrete definitions of the abstract interface methods indicate this in their header declarationsThis serves as a contract whereby the class declares it intends to implement every method form the interfaceExample:public class classname implements interface-name {}Implementing Multiple InterfacesClasses can implement more than one interfaceExample:class C implements I1, I2 {}Like class definitions interface definitions are stored in files have the .java extensionPackages can contain both interfaces and classesDefining Uniform ConstantsInterfaces can be used ro give definitions of symbolic constants used in several classesMakes it easier to keep symbolic constants in sync between classesExample:public interface Direction { int NORTH=0, EAST=1, SOUTH=3, WEST=3;}

Các file đính kèm theo tài liệu này:

  • pptchapter10_125.ppt
Tài liệu liên quan