[Salesforce] Apex - Extend Class(상속)

__Dev_1·2023년 10월 24일
0

Salesforce

목록 보기
12/15
post-thumbnail

💡 Extends Class?

  • 기존에 만들어진 클래스에 필요한 기능을 추가하거나, 수정해서 새로운 클래스로 확장시킴.
    어디에서나 쓸 수 있는 기능을 가진(공통기능) class를 만들고, 이걸 여기저기 끼워넣어서 때에 따라 필요한 기능만 추가해서 사용한다.



💡 상속(Extend) 의 개념.

  • 클래스의 확장으로 만들어진 새로운 클래스는 원본 클래스의 변수와 함수를 모두 넘겨 받음.
부모클래스(Super Class)상속자식 클래스 ( Sub Class)
변수, 함수를 넘겨줌extends부모로 부터 넘겨받아 새로 생성.

💡 How to Extends Class

  • override 키워드를 사용하여 기존 virtual 메서드를 재정의를 한다.
  • virtual 메서드를 override 하면 기존 메서드에 대해 다른 구현을 제공한다.

💡 Extends Class 구현

// virtual class
public virtual class Shape {

    private Integer length = 10; // to hole length of rectangle
    private Integer breadth = 20; // to hold breadth of rectangle
    private Integer radius = 10; 
    
    public virtual void area(){
        System.debug('Area of: ');
    }
    
    // Get Length
    public virtual Integer getLength(){
        return length;
    }
    
    // Get breadth
    public virtual Integer getBreadth(){
        return breadth;
    }
    
    public virtual Integer getRadius(){
        return radius;
    } 
}
// Shape 로부터 상속받음
public class Rectangle extends Shape {
	
    //shape 로 상속을 받아서 override 로 재정의 함.
    public override void area(){
        Integer answer = getLength() * getBreadth();
        System.debug('Area of rectangle' + answer);
    }
}

public class Circle extends Shape{

    public override void area(){
        Decimal answer = 3.14 * getRadius();
        System.debug('Area of circle:' + answer);
    }
}
🍒 호출.
public static void salesforceMethodtest(){
   Rectangle rect = new Rectangle();
   rect.area();

   Circle circ = new Circle();
   circ.area();
}
profile
메모장 :)

0개의 댓글