카테고리 없음

자바에서 객체 선언

webpage 2024. 11. 29. 12:04

---자바에서 객체선언---
class Person { 
  String name = "홍길동";  String address="서울시 서대문구"; int age = 32;
  int showAge() { return this.age; }
  void showTitle(String title) { System.out.println(title); }
  void showAddress() { System.out.println(this.address); }
  void showName() { System.out.println(this.name); }
}
class Operation {
  int x = 0; int y = 0;
  void doIt(int su1, int su2) { 
     int result1 = this.add(su1,su2);  int result2 = this.sub(su1,su2);
     int result3 = this.add(su1,su2);  int result4 = this.div(su1,su2);
     System.out.println("덧셈의 결과:"+result1); System.out.println("뺄셈의 결과:"+result2);
     System.out.println("곱셈의 결과:"+result3); System.out.println("나눗셈의 결과:"+result4);
  }
  int div(int s1, int s2) { this.setValue(s1, s2); return this.x / this.y;  }
  int mul(int s1, int s2) { this.setValue(s1, s2); return this.x * this.y;  }
  int sub(int n1, int n2) { this.setValue(n1,n2); return this.x - this.y;  }
  int add(int n1, int n2) { this.setValue(n1,n2); return this.x + this.y; } 
  void setValue(int n1, int n2) { this.x = n1; this.y = n2; }
}