In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use virtual method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Method is a class subprogram, including function and task.
When you assign a subclass directly to the parent class, and then call the method in the parent class, the method in the parent class is actually called. Unless the method is declared by virtual, the subclass can override the method in the parent class.
This is the polymorphism feature of systemverilog object-oriented language.
The SystemVerilog method declared with the keyword virtual is called virtual methods.
Virtual Functions template:
Virtual function function_name;//Function definitionendfunction
Virtual Tasks template:
Virtual task task_name;//task definitionendtask
In example 1, the method inside the parent class is declared without the virtual keyword, and b_c.display () invokes the method of the parent class after assignment.
Class base_class
Function void display; $display ("Inside base_class"); endfunction
Endclass
Class extended_class extends base_class
Function void display; $display ("Inside extended class"); endfunction
Endclass module virtual_class; initial begin base_class Banco; extended_class eBay
EExtracc = new (); biconc = etherec
B_c.display (); endendmodule
Output:
Inside base_class
In example 2, the virtual keyword is added to the method declaration within the parent class, and b_c.display () invokes the method of the subclass after the assignment.
Class base_class
Virtual function void display; $display ("Inside base_class"); endfunction
Endclass
Class extended_class extends base_class
Virtual function void display; $display ("Inside extended class"); endfunction
Endclass module virtual_class; initial begin base_class Banco; extended_class eBay
EExtracc = new (); biconc = etherec
B_c.display (); endendmodule
Output:
Inside extended class
Pure virtual method
Virtual method: this is a method that can be inherited in a derived class and can be overridden in a derived class.
Pure virtual method: this is the virtual method declared in Virtual Class (classes that can be extended but cannot be instantiated, only as base or prototype classes). You must have a concrete implementation of this method in a derived class.
Pure virtual method can only be a prototype or template.
There cannot be any implementation within pure virtual method.
Pure virtual method does not allow the use of "end" keywords, such as endfunction / endtask.
Virtual class base_class; pure virtual function void display;endclass
Class extended_class extends base_class; virtual function void display; $display ("Inside extended class"); endfunctionendclass
Module virtual_class; initial begin extended_class eRecc; eRecc = new (); e_c.display (); endendmodule
If you have a Virtual Class with pure virtual method, you must implement it in a subclass. But for virtual method, it may or may not be overridden.
That's all for the content of "how to use virtual method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.