Java Creating Object

    • Java Creating Object

      JavaScript Kaynak Kodu

      1. package constructortypes;
      2. /**
      3. *
      4. * @author chtbs
      5. */
      6. public class Point {
      7. public int x = 0;
      8. public int y = 0;
      9. //constructor
      10. public Point(int a, int b) {
      11. x = a;
      12. y = b;
      13. }
      14. }
      15. package constructortypes;
      16. /**
      17. *
      18. * @author chtbs
      19. */
      20. public class Rectangle {
      21. public int width = 0;
      22. public int height = 0;
      23. public Point origin;
      24. // four constructors
      25. public Rectangle() {
      26. origin = new Point(0, 0);
      27. }
      28. public Rectangle(Point p) {
      29. origin = p;
      30. }
      31. public Rectangle(int w, int h) {
      32. origin = new Point(0, 0);
      33. width = w;
      34. height = h;
      35. }
      36. public Rectangle(Point p, int w, int h) {
      37. origin = p;
      38. width = w;
      39. height = h;
      40. }
      41. // a method for moving the rectangle
      42. public void move(int x, int y) {
      43. origin.x = x;
      44. origin.y = y;
      45. }
      46. // a method for computing the area of the rectangle
      47. public int getArea() {
      48. return width * height;
      49. }
      50. }
      51. package constructortypes;
      52. /**
      53. *
      54. * @author chtbs
      55. */
      56. public class Constructortypes {
      57. /**
      58. * @param args the command line arguments
      59. */
      60. public static void main(String[] args) {
      61. // TODO code application logic here
      62. Rectangle rec1=new Rectangle();
      63. Rectangle rec2=new Rectangle(5,5);
      64. Point p=new Point(5,5);
      65. Rectangle rectOne = new Rectangle(p,100, 200);
      66. System.out.println("rec2 alan:"+rec2.getArea());
      67. System.out.println("point:"+Point.class+" "+Point.class);
      68. System.out.println("rectOne height "+rectOne.height+" rectOne width: "+rectOne.width+" rectOne point: "+rectOne.origin );
      69. System.out.println("Rectone alan:"+rectOne.getArea());
      70. }
      71. }
      Hepsini Göster