Java Lecture4 OVR

download Java Lecture4 OVR

of 27

  • date post

    08-Nov-2014
  • Category

    Documents

  • view

    31
  • download

    3

Embed Size (px)

description

java

Transcript of Java Lecture4 OVR

Java 2

this super ( ) . .

1

class Classname { static classVariable objectVariable }

Classname.classVariable Objectname.objectVariable

. .

returnType methodName(type1 arg1, type2 arg2, ) { return returnTypeVariable; }

returnType = void return Classname.methodname. .

2

int[] makeRange(int lower, int upper) { return }

. .

class RangeClass { int[] makeRange(int lower, int upper) { int arr[] = new int[ (upper - lower) + 1 ]; for (int i = 0; i < arr.length; i++) { arr[i] = lower++; } return arr; } ...

. .

3

public static void main(String arguments[]) { int theArray[]; RangeClass theRange = new RangeClass(); theArray = theRange.makeRange(1, 10); System.out.print("The array: [ "); for (int i = 0; i < theArray.length; i++) { System.out.print(theArray[i] + " "); } System.out.println("]"); } } ?

. .

(by value) . (by reference) . ( ) ! . .

4

class PassByReference { int onetoZero(int arg[]) { int count = 0; for (int i = 0; i < arg.length; i++) { if (arg[i] == 1) { count++; arg[i] = 0; } } return count; } ...

. .

public static void main(String arguments[]) { int arr[] = { 1, 3, 4, 5, 1, 1, 7 }; PassByReference test = new PassByReference(); int numOnes; System.out.print("Values of the array: [ "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); ...

. .

5

numOnes = test.onetoZero(arr); System.out.println("Number of Ones = " + numOnes); System.out.print("New values of the array: [ "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); } ?

. .

! public java.lang.reflect (. Java 2 SDK). .

6

import java.lang.reflect.*; import java.util.Random; class SeeMethods { public static void main(String[] arguments) { Random rd = new Random(); Class className = rd.getClass(); Method[] methods = className.getMethods(); for (int i = 0; i < methods.length; i++) { System.out.println("Method: " + methods[i]); } } }

. .

this

( ) this this : this object here !

. .

7

this

class Car { String licensePlate = ""; // e.g. "New York 543 A23" double speed = 0.0; // in kilometers per hour double maxSpeed; = 120.0; // in kilometers per hour // accelerate to maximum speed // put the pedal to the metal void floorIt() { this.speed = this.maxSpeed; } ...

. .

this

void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } } ...

. .

8

this

class CarTest4 { public static void main(String[] args) { Car c = new Car(); c.licensePlate = "New York A45 636"; c.maxSpeed = 123.45; System.out.println(c.licensePlate + " is moving at " + c.speed + " kilometers per hour."); ...

. .

this

for (int i = 0; i < 15; i++) { c.accelerate(10.0); System.out.println(c.licensePlate + " is moving at " + c.speed + " kilometers per hour."); } } } ...

. .

9

this

New New New New New New New New New New New New New New New New York York York York York York York York York York York York York York York York A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 A45 636 636 636 636 636 636 636 636 636 636 636 636 636 636 636 636 is is is is is is is is is is is is is is is is moving moving moving moving moving moving moving moving moving moving moving moving moving moving moving moving at at at at at at at at at at at at at at at at 0.0 kilometers per hour. 10.0 kilometers per hour. 20.0 kilometers per hour. 30.0 kilometers per hour. 40.0 kilometers per hour. 50.0 kilometers per hour. 60.0 kilometers per hour. 70.0 kilometers per hour. 80.0 kilometers per hour. 90.0 kilometers per hour. 100.0 kilometers per hour. 110.0 kilometers per hour. 120.0 kilometers per hour. 123.45 kilometers per hour. 123.45 kilometers per hour. 123.45 kilometers per hour.

. .

, , . .

10

valueOf String

public static public static public static public static int count) public static public static public static public static public static

String String String String

valueOf(boolean b) valueOf(char c) valueOf(char[] data) valueOf(char[] data, int offset,

String valueOf(double d) String valueOf(float f) String valueOf(int i) String valueOf(Object obj) String valueOf(long l) . .

import java.awt.Point; class MyRect { int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; MyRect buildRect(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; return this; } . .

11

MyRect buildRect(Point topLeft, Point bottomRight) { x1 = topLeft.x; y1 = topLeft.y; x2 = bottomRight.x; y2 = bottomRight.y; return this; } MyRect buildRect(Point topLeft, int w, int h) { x1 = topLeft.x; y1 = topLeft.y; x2 = (x1 + w); y2 = (y1 + h); return this; }

. .

void printRect(){ System.out.print("MyRect: "); } public static void main(String arguments[]) { MyRect rect = new MyRect(); System.out.println("Calling buildRect with coordinates 25,25, 50,50:"); rect.buildRect(25, 25, 50, 50); rect.printRect(); System.out.println("***");

. .

12

System.out.println("Calling buildRect with points (10,10), (20,20):"); rect.buildRect(new Point(10,10), new Point(20,20)); rect.printRect(); System.out.println("***"); System.out.print("Calling buildRect with 1 point (10,10),"); System.out.println(" width (50) and height (50):"); rect.buildRect(new Point(10,10), 50, 50); rect.printRect(); System.out.println("***"); } }

. .

buildRect ?

.. MyRect floatfloat buildRect(Point topLeft, int w, int h) return 12.6F;

!

,float buildRect(Point topLeft, int w, int h, float jjj) return 12.6F;

!

. .

13

(constructor) new , this . .

class Person { String name; int age; Person(String n, int a) { name = n; age = a; } void printPerson() { System.out.print("Hi, my name is " + name); System.out.println(". I am " + age + " years old."); } ...

. .

14

public static void main (String arguments[]) { Person p; p = new Person("Luke", 50); p.printPerson(); System.out.println("----"); p = new Person("Laura", 35); p.printPerson(); System.out.println("----"); } }

. .

class MyCircle { int x, y, radius; MyCircle(int xPoint, int yPoint, radiusLength) { this.x = xPoint; this.y = yPoint; this.radius = radiusLength; } MyCircle(int xPoint, int yPoint) { this(xPoint, yPoint, 1); } }

. .

15

import java.awt.Point; class MyRect2 { int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; MyRect2(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } ...

. .

MyRect2(Point topLeft, Point bottomRight) { x1 = topLeft.x; y1 = topLeft.y; x2 = bottomRight.x; y2 = bottomRight.y; } MyRect2(Point topLeft, int w, int h) { x1 = topLeft.x; y1 = topLeft.y; x2 = (x1 + w); y2 = (y1 + h); } ...

. .

16

void printRect() { System.out.print("MyRect: "); } public static void main(String arguments[]) { MyRect2 rect; System.out.println("Calling MyRect2 with coordinates 25,25 50,50:"); rect = new MyRect2(25, 25, 50,50); rect.printRect(); System.out.println("***"); ...

. .

System.out.print("Calling MyRect2 with 1 point (10,10)"); System.out.println(" width (50) and height (50):"); rect = new MyRect2(new Point(10,10), 50, 50); rect.printRect(); System.out.println("***"); } } ?

. .

17

extends . .

class PrintClass { int x = 0; int y = 1; void printMe() { System.out.println("x is " + x + ", y is " + y); System.out.println("I am an instance of the class " + this.getClass().getName()); } }

main() ! . .

18

class PrintSubClass extends PrintClass { int z = 3; public static void main(String arguments[]) { PrintSubClass obj = new PrintSubClass(); obj.printMe(); } }

main() ! PrintSubClass PrintClass , . . .

class PrintSubClass2 extends PrintClass { int z = 3; void printMe() { System.out.println("x is " + x + ", y is " + y + ", z is " + z); System.out.println("I am an instance of the class this.getClass().getName()); } public static void main(String arguments[]) { PrintSubClass2 obj = new PrintSubClass2(); obj.printMe(); }}

"+

printMe() printMe PrintSubClass. printMe , printMe

. .

19

super

this , super (superclass!) . .

super

import java.awt.Point; class NamedPoint extends Point { String name; NamedPoint(int x, int y, String name) { super(x,y); this.name = name; } public static void main (String[] arguments) { NamedPoint np = new NamedPoint(5, 5, "SmallPoint"); System.out.println("x is " + np.x); System.out.println("y is " + np.y); System.out.println("Name is " + np.name); } }

. .

20

(error handling). .

finalize() H finalize() java.lang.object

protected void finalize() throws Throwable

Java . .

21

, ,

private ( ) protected ( ) public ( )

public int MyMethod(int x, int