Hỏi đáp

Interfaces In C++ ( Abstract Class C# Là Gì, What Is A C++ Abstract Class – viettingame

Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user.Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).

Đang xem: Abstract class c# là gì

The abstract keyword is used for classes and methods:Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).Abstract method: can only be used in an abstract class, and it does not have a toàn thân. The toàn thân is provided by the derived class (inherited from).An abstract class can have both abstract and regular methods:

abstract class Animal { public abstract void animalSound(); public void sleep() { Console.WriteLine(“Zzz”); }}

Animal myObj = new Animal(); // Will generate an error (Cannot create an instance of the abstract class or interface “Animal”)
To access the abstract class, it must be inherited from another class. Let”s convert the Animal class we used in the Polymorphism chapter to an abstract class.

Xem thêm: game trực tuyến 3d

Remember from the Inheritance chapter that we use the : symbol to inherit from a class, and that we use the override keyword to override the base class method.


// Abstract classabstract class Animal{ // Abstract method (does not have a toàn thân) public abstract void animalSound(); // Regular method public void sleep() { Console.WriteLine(“Zzz”); }}// Derived class (inherit from Animal)class Pig : Animal{ public override void animalSound() { // The toàn thân of animalSound() is provided here Console.WriteLine(“The pig says: wee wee”); }}class Program{ static void Main(string args) { Pig myPig = new Pig(); // Create a Pig object myPig.animalSound(); // Call the abstract method myPig.sleep(); // Call the regular method }}Try it Yourself »

Why And When To Use Abstract Classes and Methods?

To achieve security – hide certain details and only show the important details of an object.

Xem thêm: game vo lam chi mong trực tuyến

Note: Abstraction can also be achieved with Interfaces, which you will learn more about in the next chapter.





Play trò chơi


Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Top TutorialsHTML TutorialCSS TutorialJavaScript TutorialHow To TutorialSQL TutorialPython TutorialW3.CSS TutorialBootstrap TutorialPHP TutorialJava TutorialC++ TutorialjQuery Tutorial
Top ReferencesHTML ReferenceCSS ReferenceJavaScript ReferenceSQL ReferencePython ReferenceW3.CSS ReferenceBootstrap ReferencePHP ReferenceHTML ColorsJava ReferenceAngular ReferencejQuery Reference
Top ExamplesHTML ExamplesCSS ExamplesJavaScript ExamplesHow To ExamplesSQL ExamplesPython ExamplesW3.CSS ExamplesBootstrap ExamplesPHP ExamplesJava ExamplesXML ExamplesjQuery Examples
Web CoursesHTML CourseCSS CourseJavaScript CourseFront End CourseSQL CoursePython CoursePHP CoursejQuery CourseJava CourseC++ CourseC# CourseXML CourseGet Certified »


Về Viettingame.com

Viettingame.com - Chuyên trang web tổng hợp những thông tin hữu ích trên internet như thông tin về game, tin tổng hợp
Xem tất cả các bài viết của Viettingame.com →

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *