Chapter 3 Implementing Classes.ppt
《Chapter 3 Implementing Classes.ppt》由会员分享,可在线阅读,更多相关《Chapter 3 Implementing Classes.ppt(98页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 3 Implementing Classes,Introduction,In the previous chapter, we saw how to use objects Declare an object Book aBook; Create an object aBook = new Book(“Beloved”,“Toni Morrison”); Call methods on an object aBook.getAuthor();,Introduction,Remember our goal is to write programs that accomplish
2、something useful How do objects fit in? Encapsulation EfficiencyWe can also make our own types of objects,Example Program,public class Example public static void main(String args)Path p = new Path(3,4);System.out.println(“The distance is ”+ p.calcDistance()+ “ miles and the”+ “ angle is ”+ p.calcAng
3、le(); ,Example Program,This is great, except there is no Path class in the Java API The program would be easy if we had that type of objectSo well just write a Path class ourselves,Black Boxes,What is a black box?Why do we call it a black box?What are some instances of a black box?,Black Boxes,A bla
4、ck box works magically we press a button and something happensWe dont know how it actually works Encapsulation: the hiding of unimportant details,Black Boxes,Not everything is hidden in a black box We can input some numbers There is some sort of outputThere are ways to interact with it, but its all
5、done on the outside Interaction possibilities are well defined,Encapsulation,What is the right concept for each particular black box? Concepts are discovered through abstraction Abstraction: taking away inessential features, until only the essence of the concept remains How much does a user really n
6、eed to know?,Example: Cars,Black boxes in a car: transmission, electronic control module, etc,Example: Car,Users of a car do not need to understand how black boxes work Interaction of a black box with outside world is well-defined Drivers interact with car using pedals, buttons, etc. Mechanic can te
7、st that engine control module sends the right firing signals to the spark plugs For engine control module manufacturers, transistors and capacitors are black boxes magically produced by an electronics component manufacturer,Example: Car,Encapsulation leads to efficiency: Mechanic deals only with car
8、 components (e.g. electronic control module), not with sensors and transistors Driver worries only about interaction with car (e.g. putting gas in the tank), not about motor or electronic control module,Example: Doorbell,What can we do to a doorbell?What output do we get from a doorbellDo we actuall
9、y know how a doorbell works?Do we need to?,Encapsulation,In Object-oriented programming (OOP) the black boxes we use in our programs are objects String is a black box Rectangle is a black box,Software Design,Encapsulation,Old times: computer programs only manipulated primitive types such as numbers
10、and characters Gradually programs became more complex, vastly increasing the amount of detail a programmer had to remember and maintain,Encapsulation,Solution: Encapsulate routine computations to software black boxes Abstraction used to invent higher-level data types In object-oriented programming,
11、objects are black boxes,Encapsulation,Encapsulation: Programmer using an object knows about its behavior, but not about its internal structure In software design, you can design good and bad abstractions / objects; understanding what makes good design is an important part of the education of a softw
12、are engineer,Abstraction,Who designs objects you use? Other programmersWhat do these objects contain? Other objects Lesson: Multiple levels of abstraction Almost always, you will be designing an abstraction while simultaneously using another one,Review,Until now, youve only used objects Analogous to
13、 engineer who puts final parts together in carNow you will learn how to design objects Analogous to designing the parts of the carThis is a complex process,Designing a Class,Recall that a class is a template for objects Its the cookie cutter, not the cookiesYou need to decide: What does the black bo
14、x need to do? (methods) What does the black box need to know? (data),Designing a Class,Remember: you are designing a class that another programmer could use. Make it easy to understand Design your class as a black box,Methods,The first thing a class requires is a list of methods what should the blac
15、k box do? For our Path example: calcDistance calcAngle setX setY getX getY Which methods are accessors and which are mutators?,Methods,access specifier (eg public) return type (eg String or void) method name (eg deposit) list of parameters (eg New value for x) method body in braces ,Methods,Examples
16、: public double calcDistance() . public int getX() . public void setY(int newY) .,Methods: Syntax,accessSpecifier returnType methodName(parameterType parameterName,.) method body Example:public double calcDistance() . . . Purpose: To define the behavior of a method,Methods,Notes: Methods always have
17、 parentheses The return type is what type of output the black box will give the user Parameters are what types of input the user needs to provide The method body is a sequence of instructions,Constructors,Recall that we need to be able to create objects in order to use them The set of instructions t
18、hat does is this a constructor Note: Constructor name = Class namepublic Path() / body-filled in later ,Constructors,Constructor body is executed when a new object is created Statements in constructor body will set up the object so it can be used A constructor initializes all data members How does t
19、he compiler know which constructor to call?,Constructors vs. Methods,Constructors are a specialization of methods Goal: to set the internal data of the object to a valid state 2 major differences All constructors are named after the class Therefore all constructors of a class have the same name No r
20、eturn type is EVER listed!,Constructors: Syntax,accessSpecifier ClassName(parameterType parameterName, . . .) constructor body Example: public Path(int x, int y) . . . Purpose: To define the behavior of a constructor,Public Interface,The public constructors and methods of a class form the public int
21、erface of the class.public class Path / data fields-filled in later / Constructors public Path() / body-filled in later ,public Path(int initX, int initY) / body-filled in later / Methods public double calcDistance() / body-filled in laterpublic int getX() / body-filled in later public void setY() /
22、 body-filled in later / more methods ,Class Definition Syntax,accessSpecifier class ClassName fields constructorsmethods Example: public class Path public Path(int initY, int initY) . public double calcDistance() . . . . Purpose:To define a class, its public interface, and its implementation details
23、,Review,Public methods and constructors provide the public interface to a class They are how you interact with the black boxOur class is simple, but we can do many things with it Notice we havent defined the method body yet, but know how we can use it,Javadoc Comments,Part of creating a well-defined
24、 public interface is always commenting the class and method behaviorsThe HTML pages from the API are created from special comments in your program called javadoc commentsPlaced before the class or method /*/,Javadoc Comments,Begin with /* Ends with */ Put * on lines in between as convention, makes i
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER3IMPLEMENTINGCLASSESPPT
