Fortran Algol Pascal Modula-2 BCPL C Simula SmallTalk C++ Ada Java C#
C Fortran
5.1
message A B
5.2
1 class Vehicle subclass Car object mycar
public class Vehicle extends Object{ public int WheelNum = 4; //...// } import Vehicle; public class Car extends Vehicle{ public int DoorNum = 4 ; public String EngineType = "V6" ;...// } import java.io.*; import Car; public class Me{ public static void main(string args[]){ Car mycar = new Car(); System.out.printlin(" "+mycar.wheelnum); System.out.printlin(" "+mycar.enginetype); } }
Circle radius Graphical notation for classes findarea new Circle() new Circle() circle1: Circle radius = 2... circlen: Circle radius = 5 Graphical notation for objects
2 class Circle { double radius = 1.0; } double findarea() { return radius*radius*3.14159; }
5.3
class Car { int color_number; int door_number; int speed; } void brake() { } void speedup() { }; void slowdown() { }
5.4 ClassName objectname; Example: Car mycar; Circle mycircle; objectname = new ClassName(); Example: mycar = new Car(); mycircle = new Circle();
ClassName objectname = new ClassName(); Example: Car mycar = new Car(); Circle mycircle = new Circle();
5.5 Primitive type int i = 1 i 1 Object type Circle c c reference Created using new Circle(5) c: Circle radius = 5
Primitive type assignment i = j Object type assignment c1 = c2 Before: After: Before: After: i 1 i 2 c1 c1 j 2 j 2 c2 c2 c1: Circle c2: Circle radius = 5 radius = 9
Primitive type assignment i = j Object type assignment c1 = c2 Before: After: Before: After: i 1 i 2 c1 c1 j 2 j 2 c2 c2 c1: Circle radius = 5 c2: Circle radius = 9
5.6 static final static
Notation: +: public variables or methods -: private variables or methods underline: static variables or metods Memory radius is an instance variable, and numofobjects is a class variable Circle -radius -numofobjects +getradius +setradius +getnumofobjects +findarea instantiate instantiate circle1:circle -radius = 1 -numofobjects = 2 circle2:circle -radius = 5 -numofobjects = 2 1 radius 2 5 radius numofobjects
Example numofobjects Circle Circle private double radius private static int numofobjects public double getradius( ) public void setradius(double newradius) public static int getnumofobjects( ) public double findarea( ) TestInstanceAndClassVariable Run
5.7 : objectname.data mycircle.radius : objectname.method mycircle.findarea()
Example 5.1 Circle TestCircle Run
5.8 void new
5.8 Circle(double r) { radius = r; } Circle() { radius = 1.0; } mycircle = new Circle(5.0);
Example 5.2 Circle TestCircleWithConstructors Run
5.9 Example 5.3 TestPassingObject Run
mycircle n main printareas n mycircle main 5 mycircle Circle radius=1 printareas times c 5
5.10 Java public. private. getter and setter
Example 5.4 private Circle getradius and setradius TestCircleWithPrivateModifier Run
5.11 Java { { }}
class A{ int x; int f() { int a=5; x=a; // x } int g() { int y; y=a;// aga } }
class Tom{ int x=98,y; void f() { int x=3; y=x; //y 3 98 } }
5.12 this super this this this super Java super
Example 5.6 this super class A { int a; A(int new_value) { a = new_value; } } class B extends A { int b, c; B() { this(2, 3); } B(int new_b, int new_c) { super(1); b = new_b; c = new_c; } }
class Demo { public static void main(string[] args) ) { B obj_b = new B(); System.out.println("a= = "+obj_b.a + "; b= "+obj_b.b + "; c= "+ obj_b.c); } } a= 1; b= 2; c= 3
5.13 5.13.1 5.13.2 5.13.3
5.13.1 Student 5.60 * Course 0.3 1 Faculty 3 5 60
5.13.1 Student 5.60 * Course 0.3 1 Faculty 3 5 60
5.13.1 Student 5.60 * Course 0.3 1 Faculty 3 5 60
5.13.1 Student 5.60 * Course 0.3 1 Faculty 3 5 60
5.13.1 Student 5..60 * Course 0..3 1 Faculty 3 5 60
5.13.1 Student 5..60 * Course 0..3 1 Faculty 3 5 60
5.13.2 (has-a) (part-of) (own) (employedby) Magazine * 1 Publisher * * Consultant
5.13.2 (has-a) (part-of) (own) (employedby) Magazine * 1 Publisher * * Consultant
5.13.3 (is-a) Person Student Faculty
5.7 5.14 1. 2. 3. 4.
Example 5.6 1 Name 1 Borrower Address 1 Mortgage
Example 5.6 The following is a test program that uses the classes Name, Address, Borrower, and Mortgage. BorrowMortgage Run
5.15 Java API and Core Java classes java.lang JavaObject String Math Number SystemJava java.awt java.applet applet
5.15 Java API and Core Java classes, cont. java.io java.util java.net
5.15 Java API and Core Java classes, cont. java.awt.image bitmap. java.awt.peer GUI Others: java.sql java.rmi
5.16 The String Class Javajava.lang String "student100", "China" C
5.16 The String Class String String s; = new String = s = new String ( "student" ); s = "student" String s = new String ( "student" ); String s = "student
5.16 The String Class String tom = "my name is tom"; String jane = "my name is jane"; tom.equals(jane) false String s = " I am"; String s2 = "a Chinese"; String s3, s4; s3 = s + s2; s4 = s + 24; 24
5.16 The String Class String s = "I am a Chinese"; String subs; subs = s.substring (7);//Chinese subs = s.substring (0,8);//I am a C length() String s = "student"; int len=s.length(); endindex beginindex, endindex
5.16 The String Class String String s = "I am a Chinese"; s.indexof("chinese" );// 7 s.indexof('a'); //2 Do not use message[0] s.charat(index) index 0 s.length()-1
5.17 The StringBuffer Class StringBuffer String.. StringBuffer String..
StringBuffer public StringBuffer() 16. public StringBuffer(int length) length. public StringBuffer(String str) string string 16.
StringBuffer StringBuffer StringBuffer strbuf = new StringBuffer(); strbuf.append("welcome"); strbuf.append(' to Java'); sb1.append( this is a book. ); sb1.insert(6,30); // int,char,double,string sb1.setcharat(0, a );
5.18 The StringTokenizer Class Constructors StringTokenizer(String s, String delim, boolean returntokens) delims StringTokenizer returntokens true
Class Constructors StringTokenizer(String s, String delim) delims StringTokenizer StringTokenizer(String s) s StringTokenizer
The StringTokenizer Class Methods boolean hasmoretokens() true String nexttoken() String nexttoken(string delim) delim public int counttokens() StringTokenizer
? this