
Untitled
By: a guest on
Sep 7th, 2012 | syntax:
None | size: 0.67 KB | hits: 9 | expires: Never
// Use of 'this' operator
// class Rectangle2 shouldn't be declared public
class Rectangle2 {
int length,breadth;
// 'this' is equivalent to self in python
void show(int length,int breadth) {
this.length = length;
this.breadth = breadth;
}
// because I have 'this'ed length and breadth above, I am not passing any
// parameters for the method 'area' below
int area() {
return (length * breadth);
}
}
public class This {
public static void main(String[] args) {
Rectangle2 rect = new Rectangle2();
rect.show(2,3);
int area = rect.area();
System.out.println("Area --> "+area);
}
}