'this'에 해당되는 글 3건

  1. 2012.07.28 this, super키워드
  2. 2012.07.28 제한자, 캡슐화, 변수, this
  3. 2012.07.28 this

* super
1. 부모의 생성자 호출 시 사용(중요함, )
-생성자는 class와 이름이 같아야 하므로 부모class의 생성자는 상속이 안된다.
단, 자식 class의 생성자에서 부모의 생성자 호출은 가능.
- super([argument, ...]);
->생성자 구현부의 첫 명령어로 와야한다. (this도 생성자 구현부의 첫 명령어로 와야함으로 this와 super는 함께 쓸수 없다. )
->생성자 구현부에 this()나 super() 코드가 없으면 super();<-부모의 no-argument 생성자 호출 하는 코드가 자동으로 들어간다.
2. 부모의 객체를 참조하는 변수(중요도가 약간 떨어짐)

* this
1. overloading 된 생성자 호출(몰라도 프로그램 짜는데 이상이 없음, 프로그램 코딩을 다해주면됨. 코드 라인을 줄여주는 역할)
->생성자 구현부의 첫 명령어로 와야한다.
2.현재 일하는 메소드의 객체 - local

* 메소드 overriding(메소드 재정의) :  하위 class에서 부모 class에 정의된 메소드의 구현을 재정의 하는것. (자기만의 특징을 넣어서 다시 구현하고 싶을 때 메소드를 다시 정의하는 것.)
- 규칙 : 부모 안에 있는 메소드를 자식에서 똑같이 만들어서 내용만 바꾸는 것이다.
             전제 :  상속관계
      1. 이름, return type, 매개변수가 같아야 한다.
      2. 접근 제한자의 범위가 부모와 같거나 더 넓어야 한다. (만약 접근제한자의 범위가 부모가 protected면 자식은 protected나 public 이어야 한다.만약 접근제한자의 범위가 부모가 public이면 자식은 public이어야한다.단, 부모가 private면 상속해서 받아 올 수 없기때문에 오버라이딩이 아니다.)
* 메소드 overloading - 다른 메소드인데 인자와 타입이 다르면 한 클레스 내에서 사용 가능한 것.

*exception 관련 규칙.


* 2012-3-14

- 상속 :
부모클래스 :  자식클래스의 공통적인 내용을 추려서 새로 만들어진 클래스
(추상적이다.)

* super - 생성자호출 -> 부모의 생성자를 부름
             - 객체의 참조변수
      - 생성자 구현 첫 명령에 와야 한다.
      - 하위 class의 객체에서 부모 class의 객체를 참조하는 참조변수.
      - overriding한 메소드에서 부모에 정의된 원본메소드 호출시 사용.
      사용 : super.부모의 member-변수나, 메소드

          public
넓다       |
          protected
              |
좁다   private

*
Project : day16
package : overriding.test
class : Superclass
    SubClass

package overriding.test;

public class SuperClass {
 

 public void methodA(int i){
  System.out.println("SuperClass.methodA() : "+i);
 }
}


----------------------


package overriding.test;

public class SubClass extends SuperClass {
 /*
 //overriding - return type이 틀림
 public int methodA(int i){
  System.out.println("SubClass.methodA(int) : "+i);
 }
 //overriding = 접근제한자가 부모보다 좁아서 틀림.
 protected void methodA(int i){
  System.out.println("SubClass.methodA(int) :"+i);
 }
 */
 //overriding = 정상
 public void methodA(int i){
  System.out.println("SubClass.methodA(int) :"+i);
 }
 
 //overloading
 public void methodA(){
  System.out.println("SubClass.methodA()");
 }
 //overloading
 public void methodA(String str){
  System.out.println("SubClass.methodA(String) : "+str);
 }
 public static void main(String[] args) {
  SubClass sc  = new SubClass();
  sc.methodA(10);
 }
}


------------------------------

* 도형 예제

package : overriding.figure
TestShape - main()
Rectangle -width, height
Circle - radius, PI
Triangle - width, height

Shape - public void calculate Area(){}


ex :

package overriding.test;

import overriding.figure.Circle;
import overriding.figure.Rectangle;
import overriding.figure.Triangle;

public class TestShape {

 public static void main(String[] args) {
  Circle c = new Circle(3);
  c.calculateArea();
  
  Rectangle r = new Rectangle(10,20);
  r.calculateArea();
  
  Triangle t = new Triangle(10,20);
   t.calculateArea();

 }

}

----------------------

package overriding.figure;

public class Shape {
 public void calculateArea(){}
}

-----------------------

package overriding.figure;

public class Rectangle extends Shape{
 private double width;
 private double height;
 
 public Rectangle(double width, double height) {
  this.width = width;
  this.height = height;
 }

 public void calculateArea(){
  System.out.println(" 사각형의 넓이는 : "+width*height);
 }

 
}

-------------------------

package overriding.figure;

public class Circle extends Shape{
 private double radius;
 
 public Circle(double radius) {
  this.radius = radius;
 }

 public void calculateArea(){
  
  System.out.println(" 원의 넓이는 : "+radius*radius*Math.PI);
 }
 
}


------------------------------

package overriding.figure;

public class Triangle extends Shape{
 private double width;
 private double height;
 public Triangle(double width,double height){
  this.width = width;
  this.height = height;
 }
 public void calculateArea(){
  
  System.out.println(" 삼각형의 넓이는 : "+width*height/2);
 }

 
}

'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글

의존관계(dependency)  (0) 2012.07.28
final변수  (0) 2012.07.28
상속, 생성자  (0) 2012.07.28
싱글턴 패턴  (0) 2012.07.28
static 예  (0) 2012.07.28
Posted by 조은성
,

1.제한자-①접근제한자(값을 변경, 조회)- 접근 범위를 정해줌.(ex :  public, protected, packagefriendly(아무것도 쓰지 않을경우), private)
         ②보통 제한자(final(한번 값을 넣으면 변경할 수 없음을 제한))


2.private에 접근하기 위해서 변경하는 메소드와 조회하는 메소드를 지원한다.(set(int a), get())

3. 캡슐화(은닉성, 정보 하이딩 기법)
- instance 변수의 접근제한자는 private로 하여 직접 접근하지말고 변수의 값을 변경하는 setter와 조회할수 있는
  getter메소드를 public으로 제공하라.
- setter구문 :  public void set변수명(매개변수 :  변수 type)  int age; public void setAget(int a){}
- getter구문 : public 변수 type get변수명(){}, public int getAget(){}
- boolean : 변수명을 is로 시작하라.

4. 변수

-Local variable : 메소드 안에 선언한 변수.(지역변수 Method variable)(매개변수도 local변수)
 ->사용범위 : 메소드 수행시. 선언부 실행~메소드 종료.
 ->외부에서 호출(접근)불가.
 ->실행 stack(excute stack) 메모리 영역에 저장.  stack 구조는 FILO구조를 가진다.( 먼저들어간게 나중에 나온다.)
 ->묵시적 초기화가 없다.
*객체는 heap영역에 저장.

*변수 = 값
① value(10,20,"ABC")
② 연산(10+20)
③ 변수 int i = 20;
        int j = i;
④ 메소드 호출문 int j = sum(10,20);//리턴값

*Local 변수는 자신이 선언되어 있는 블럭 내에서만 사용가능하다.
ex :

void go(){
 int i = 10;
 {
  System.out.println(i);(가능)
  int j = 20;
 }
 System.out.println(j);(에러)
}

 

5. 데이터 저장

- (실행)stack - 실행 중인 메소드의 local 변수(로컬변수에서 변수는 사용이 끝나면 없어 지므로 (실행) 스택이라는 말을 쓴다.)
- heap  - 객체가 저장.

6. this : 이것(①overloading constructor(생성자),
               ②현재 일하는 메소드, 생성자를 소유한 객체를 가리키는 Local 변수)
          -> 현재 일하고 있는 객체의 주소값을 가리킨다.
          -> instance 메소드나 생성자 호출 시 실행 stack 영역에 가장 먼저 올라간다.

 

'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글

if문  (0) 2012.07.28
Data type, 연산자, 형변환  (0) 2012.07.28
객체지향(oop)이라면 알아야할 개념  (0) 2012.07.28
this  (0) 2012.07.28
오버로딩(overloading)  (0) 2012.07.28
Posted by 조은성
,

* this([값,...]);
->생성자에서 overloading 된 다른 생성자 호출.
-생성자 구현부의 첫번째 실행문으로 들어와야 한다.
ex : 
-안되는 경우
Student(){
System.out.println("aaaa");
         this(10);
 }
-----------------------
-되는 경우
Student(){
this(10);
System.out.println("aaaa");
        
}
-----------------------

-this()의 사용
Student(String sid,String n,int a){
 studentId = sid;
 name = n;
 age = a;
}

Student(String sid,String n, int a, int g, int sy){
 /*
 studentId = sid;
 name = n;
 age = a;
 */
 this(sid,n,a);                    <----------------------this()는 이렇게 사용한다.(코드를 좀더 짧게 하는 거지 몰라도 된다).
//만약 new Student(sid,n,a);를 쓰게 되면 새로운 객체를 새로 하나 만든다는 것이고 this(sid,n,a);를 하게 되면 내부적으로 생성자를 불러만 오는 것이다.
 grade = g;
 schoolYear = sy;
}

 

Posted by 조은성
,