|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
|
|
#1
|
|||
|
|||
|
Basic OOP Question
So I am trying to get this OOP down and I am following along in this tutorial.
There are three files used when compiling, they are: CreateObjectDemo.java Code:
public class CreateObjectDemo {
public static void main(String[] args) {
// create a point object and two rectangle objects
Point origin_one = new Point(23, 94);
Rectangle rect_one = new Rectangle(origin_one, 100, 200);
Rectangle rect_two = new Rectangle(50, 100);
// display rect_one's width, height, and area
System.out.println("Width of rect_one: " + rect_one.width);
System.out.println("Height of rect_one: " + rect_one.height);
System.out.println("Area of rect_one: " + rect_one.area());
// set rect_two's position
rect_two.origin = origin_one;
// display rect_two's position
System.out.println("X Position of rect_two: " + rect_two.origin.x);
System.out.println("Y Position of rect_two: " + rect_two.origin.y);
// move rect_two and display its new position
rect_two.move(40, 72);
System.out.println("X Position of rect_two: " + rect_two.origin.x);
System.out.println("Y Position of rect_two: " + rect_two.origin.y);
}
}
Point.java Code:
public class Point {
public int x = 0;
public int y = 0;
// a constructor!
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Rectangle.java Code:
public class Rectangle {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
this(new Point(0, 0), w, h);
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the rectangle
public int area() {
return width * height;
}
}
Where I am having a problem is with the constructors in Rectangle.java. The constructors do not use the keyword "this", like: this.origin = p; this.width = w; this.height = h; The Point constructor uses "this" why doesn't the Rectangle constructors use "this"? Also, what is going on in with the third Rectangle constructor. It looks like it is simply calling the fourth constructor, is that true? Any help would be greatly appreciated. |
|
#2
|
|||||
|
|||||
|
RE: Basic OOP Question
Quote:
Using "this" is optional in this case since there are no variables in the function name origin, width, or height, so the class scope is assumed. Quote:
The Point class must use this because x and y are defined both in the function scope (as parameters) and in the class scope (as member variables). Quote:
That's exactly the case. The third is simply a short-hand version of the fourth. |
|
#3
|
|||
|
|||
|
RE: Basic OOP Question
Thanks for the help, that makes perfect sense.
|
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Programming Theory > Basic OOP Question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|