methodname(Rectangle_第1页
methodname(Rectangle_第2页
methodname(Rectangle_第3页
methodname(Rectangle_第4页
methodname(Rectangle_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、OOP Mega RecapClasses, objects, aliasing, methods, arrays, stringsObjects and classesObjects are “large” variablesGroup together multiple smaller primitive variablesObject variables themselves are aliases to the objectThe object itself is separate; its creation is a separate stepThe type of an objec

2、t is programmer definedcalled a classclass.type = variable;class = objectAccess member variablesmember variables using the . (dot) symbol.OOP Principle #0: Encapsulation (data hiding)Aliasing and Object ConstructionAn object variable doesnt contain the data itself!Object variables are hence called r

3、eference reference variablesvariablesThey referrefer to the objects present in memoryMultiple object variables can refer to the samesame object in memory!Object variable declaration: ;Assignment (=) creates aliases (not copies)Object creation = newnew ()();“new ()” is an expressiontype = class-namev

4、alue = alias to the newly created objectMethods (non-static)Can work on member variables of the objectA method is called “on an object”.method_name()The code inside a method can implicitly use member variablesThe member variables of the given object are usedStatic vs Non-static methodsstatic void me

5、thod_name(int parameter) /statements No object context to work with (simple)static void method_name(Rectangle r, int parameter) Explicit object context to work with (indirect, cumbersome)void method_name(int parameter)/ access member variablesImplicit object contextThe ConstructorA special methodNam

6、ed after the class name.Takes input parameters; no return typeno return type.Used to initialize an object at creation time.class RectangleRectangle(int w, int h)/statements;Multiple methods and recursionMethods of a class can call each other directlyEach implicitly use the same object contextsame ob

7、ject context.Recursive calls also implicitly use the same object same object contextcontext.The this keywordA special read-onlyread-only variable visible inside methodsAlias to the object context in useOrganizing and Reusing classesDependency relationship between classesMotivated by the dependency b

8、etween methodsHow should we re-use classes?Wrong Answer: Use copy and paste Trivial Answer: use objects of some classes inside other classesOOP Answer: Inheritance of classesParent-Child like relationship between classeseg. Parallelogram and Rectangle classesOOP feature #1:InheritanceDefine a new (“

9、child”) class based on a “parent” classThe new class has an implicit copy of the old class in itAdd member variables and re-define (override) some methodsUse the “super” alias to access parent class methodsMainly for calling parents constructor and overridden methodsImplicit copy of parents member v

10、ariablesThe child class gets a copy of the parents member variablesUse them freely as if they were defined in the child class itselfImplicit presence of parents methodsFreely use parents methodsThey work on the copy of parents member variables in the child classInheritance:Java syntaxclass extends /

11、 declarations for child class/ access parent version of overridden methodsuper.()/ access parents constructor/ must be first statement in child constructorsuper();Note: a child class can have a sub-child class and so on The “Object” classIts the parent class of allall classesImagine Java automatically adding an “extends Object”methods in ObjectString toString()returns a string rep

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论