-
[SystemVerilog] 클래스 3 상속개발/SystemVerilog 2022. 1. 24. 00:11
객체지향의 가장 기본적인 상속
시스템베리로그에서 클래스 상속은 extends 키워드를 사용한다.
하나의 클래스만 상속 가능하다.
class frame; logic [4:0] addr; logic [7:0] payload; bit parity; function new (input int addr, payload); this.addr = addr; this.payload = payload; parity = ^{addr, payload}; endfunction endclass // extended class class tagclass extends frame; static int framecount = 0; int tag; function new(); tag = ++framecount; endfunction endclass
상속을 통해서 자식 클래스 (tagclass)에 새로운 속성과 메서드를 추가할 수 있고, 부모 클래스 (frame) 의 메서드를 오버라이드 할 수 있다.
자식클래스를 new 메서드로 생성하면 자동으로 부모클래스도 생성이 된다.
super 키워드는 부모클래스를 참조하는 키워드이다.
해당 키워드로 자식클래스에서 부모클래스에 있는 속성과 메서드를 접근할 수 있다.
class frame; logic [4:0] addr; logic [7:0] payload; bit parity; function new (input int addr, payload); this.addr = addr; this.payload = payload; parity = ^{addr, payload}; endfunction endclass class tagclass extends frame; static int framecount = 0; int tag; function new(input int addr, payload); super.new(addr, payload) tag = ++framecount; endfunction endclass
new 메서드에서 super.new() 사용하여 인자를 넘겨주면서 부모클래스를 생성한다.
참고로 시스템베릴로그에선 조부모클래스를 초기화 하기 위해 super.super.new() 이런식으론 쓸 수 없다.
만약 조부모클래스가 있다면, 손자 클래스에서 super.new() 를 사용하여 부모클래스에 인자들을 넘겨주고
다시 부모클래스에서 super.new() 를 통해 조부모 클래스에 인자들을 넘겨서 초기화 해야한다.
'개발 > SystemVerilog' 카테고리의 다른 글
[SystemVerilog] 클래스 3 다형성2 (0) 2022.01.24 [SystemVerilog] 클래스 3 다형성 (0) 2022.01.24 [SystemVerilog] 클래스 1 (0) 2022.01.17 [SystemVerilog] 데이터 타입 5 string (0) 2022.01.17 [SystemVerilog] 인터페이스 2 (0) 2022.01.16