TeachJava! 2003.ppt
《TeachJava! 2003.ppt》由会员分享,可在线阅读,更多相关《TeachJava! 2003.ppt(60页珍藏版)》请在麦多课文档分享上搜索。
1、TeachJava! 2003,Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Neal Hororwitz, Peter Centgraf,From C+ To Java,C+ and Java have similar syntax. Dont be misled! Profoundly different semantics: high-level objects vs. bytes in memory Profoundly different programming models: Java is
2、object-oriented (OO) C+ is object-based (OB) in normal usage,Encouraging Note,Writing OO programs in Java is remarkably easyprovided we recognize that it is fundamentally different than writing OB programs in C+. There is little common conceptual ground, other than syntactic conventions, beween OO p
3、rograming (OOP) in Java and OB programming in C+,Guiding Vision,Program design in Java is data-directed. Design the data abstractions first; they will determine the structure of the code. In OOP circles, this data design process is often called object-modeling. Common data abstractions are codified
4、as design patterns.,Secondary Theme,DrJava, our lightweight, reactive environment for Java, facilitates active learning; with DrJava learning Java is a form of exploration. DrJava is not a toy; DrJava is developed using DrJava. It includes everything that we believe is important and nothing more.,Wh
5、at Is an Object?,Collection of fields representing the properties of a conceptual or physical object. Collection of operations called methods for observing and changing the fields of the object. These fields and methods often called the members of the object.,Example: Phone Directory,Task: maintain
6、a directory containing the office address and phone number for each person in the Rice Computer Science Dept.Each entry in such a directory has a natural representation as an object with three fields containing a persons name address phone number represented as character strings.,Summary of Entry fo
7、rmat,Fields:String nameString addressString phoneMethods: String getName() String getAddress() String getPhone(),Java Method Invocation,A Java method m is executed by sending a method call o.m()to an object o, called the receiver. The method m must be a member of o. The code defining the method m ca
8、n refer to the receiver using the keyword this.,Finger Exercise,In the DrJava programming environment, open the program file Entry.java, compile it, and type the following statements in the Interactions pane:Entry e = new Entry(“Corky“,“DH 3104“,“x 6042“); e.getName()e.getPhone(),Java Expressions,Ja
9、va supports essentially the same expressions over primitive types (int, float, double, boolean) as C+. Notable differences: boolean is a distinct type from int no unsigned version of integer types explicit long type,Finger Exercise,Evaluate the following: -5 + 3 -(5 + 3) 5 % 3 5./3. 5 / 0 5./0. 5 6.
10、,Finger Exercise cont.,72. - 32. * 1.8 (72. - 32.) * 1.8 72. - 30. - 12. 72. - (30. - 12.),Java Statements,Essentially the same form as in C+: assignment, if, while, for, return, But well-written Java programs consist primarily of assignment, if, and return statements (with smattering of for). Focus
11、 on assignment and if for most of the week.,Assignment,Restricted form of assignment: variable definitiontype var = exp; Exampleint x = 5;,Finger Exercise,int x = 5; x*x double d = .000001; double dd = d*d; dd dd*dd 1. + dd 1. + dd*dd,Finger Exercise cont.,Evaluate: int x = 7; if (x = 5) y = 0; else
12、 y = 10; yDid you get the behavior that you expected? Repeat the exercise with corrected syntax. Evaluate: boolean switch1 = (x = 7); switch1Repeat the exercise with corrected syntax.,Classes: Object Templates,A Java program is a collection of classes. A class is an object template consisting of a c
13、ollection of membersfields and methods plus a constructor, which is a special method used to initialize objects when created.,Example: Class Entry,class Entry /* fields */String name; String address;String phone;/* constructor */Entry(String n, String a, String p) this.name = n;this.address = a;this
14、.phone = p; /* getters */String getName() return this.name; String getAddress() return this.address; String getPhone() return this.phone; ,Finger Exercise,In the Definitions pane of DrJava, enter the Java program defining the Entry class. In the Interactions pane, evaluate the following program text
15、: Entry e = new Entry(“Corky“, “DH 3104“, “x 6042“); e.getName() e.getAddress() e.getPhone() Save your program for future use in a file named Entry.java.,Terminology,The methods that return the fields of a class are called getters. The special method with the same name as the class is called the con
16、structor. The constructor is executed when a new instance (object) of the class is created using new.,Java Data Types,Two fundamental categories Primitive types: int, boolean, double, float, char, long, short, byte (first three are most common) Object types: all class instances (objects) belong to o
17、bject types, which are disjoint from the primitive types Values of primitive type (e.g., true, 0) are not objects,Object Types,Organized in a strict hierarchy with the universal type Object at the top. Every class C except Object has an immediate superclass, which is the parent of C in the hierarchy
18、. In a class definition (like our Entry example), the default superclass is Object.A descendant in the class hierarchy is called a subclass. B is a subclass of A iff A is a superclass of B. Entry is a subclass of Object; Object is a superclass of Entry.,null,There is a special value null of object t
19、ype on which method invocation utterly fails. null is a reference to nothing; the method invocationnull.m()always generates a NullPointerException aborting execution.,Example: the String class,The String class is built-in to Java, just like Object. Finger Exercise: evaluate String s = “Corky“; Objec
20、t o = s; o o = s String t = “Cork“ + “y”; t = s s.length() o = null; o.length() Morals: multiple copies of the same String may exist; do not use = to test String equality. Do not use null to represent legitimate data values.,Object Types cont.,Each subclass C inherits (includes) all of the members o
21、f its superclass. The declared members of C augment the inherited members with one exception: if C declares a method m with exactly the same name and types as an inherited method, then the new definition of m overrides (replaces) the inherited definition.,Inheritance from Object,The Object class has
22、 several members that its children inherit. They include the methods public String toString() which gives a String representation for the object. public boolean equals(Object o) which compares this to o,Example of Overriding,class Entry /* fields */ String name; String address;String phone;/* constr
23、uctor */Entry(String n, String a, String p) this.name = n;this.address = a;this.phone = p; /* accessors */String getName() return this.name; String getAddress() return this.address; String getPhone() return this.phone; /* overridden methods */public String toString() return “ + name + address + phon
24、e + “; ,Finger Exercise,Open your Entry class into the DrJava Interactions pane. Compile your program and evaluate:Entry e = new Entry(“Corky“, “DH 3104“, “x 6042“); e Add the definition of toString() from the previous slide to your Entry class. Compile your program and evaluate:Entry e = new Entry(
25、“Corky“, “DH 3104“, “x 6042“); e,Adding a Method Definition,class Entry /* return this if its name matches keyName; otherwise return null indicating failure */Entry match(String keyName) if (keyName.equals(this.name) return this; else return null Finger Exercise: Add this method definition to your E
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- TEACHJAVA2003PPT
