007 Class(類)

SilentLittleCat發表於2020-10-10

類(Class)

  • Object:類例項後的物件
  • Object members:成員
  • Constructors:建構函式
  • Static class members:靜態類成員
  • Assignments:賦值
  • Inheritance and subclasses:繼承和子類
  • this and super:指標
  • Data hiding and encapsulation
  • Constant class properties:常量類屬性
  • Abstract classes and virtual methods:抽象類和虛擬方法
  • Class scope resolution operator:類範圍解析運算子
  • Parameterized classes:引數化類
program class_t;
  class packet;
     // members in class
     integer size;
     integer payload [];
     integer i;
     // Constructor
     function new (integer size);
       begin
         this.size = size;
         payload = new[size];
         for (i=0; i < this.size; i ++) begin
           payload[i] = $random;
         end 
       end 
     endfunction
     // Task in class (object method)
     task print ();
       begin
         $write("Payload : ");
         for (i=0; i < size; i ++) begin
           $write("%x ",payload[i]);
         end
         $write("\n");
       end 
     endtask
     // Function in class (object method)
     function integer get_size();
       begin
         get_size = this.size;
       end
     endfunction
   endclass

   packet pkt;

   initial begin
     pkt = new(5);
     pkt.print();
     $display ("Size of packet %0d",pkt.get_size());
   end

endprogram

其實類似C++,具體的後續再補充

相關文章