在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C++/ D語言抽象類
類的訪問修飾符
D語言運算符
D語言邏輯運算符
類指針
D語言元組
D語言指針
D語言模塊
D語言sizeof運算符
D語言混合類型
D語言封裝
D語言條件編譯
類的靜態(tài)成員
D語言do...while循環(huán)
D語言結構體
重載
D語言字符串-String
D語言決策語句
D語言接口
D語言for循環(huán)
D語言switch語句
D語言關聯(lián)數(shù)組
D語言范圍
D語言枚舉Enums
契約式編程
D語言并發(fā)
D語言開發(fā)環(huán)境設置
D語言別名
D語言常值
D語言常量
D語言函數(shù)
D語言if嵌套語句
D語言循環(huán)
D語言概述,D語言是什么?
D語言運算符優(yōu)先級
D語言continue語句
D語言異常處理
D語言break語句
D語言if...else語句
D語言類和對象
類繼承
D語言字符
D語言教程
D語言關系運算符
比較操作符重載
構造函數(shù)和析構函數(shù)
D語言抽象類
D語言if語句
D語言賦值運算符
D中算術運算符
D語言類成員函數(shù)
D語言位運算符
D語言變量
D語言數(shù)據類型
D語言文件I/O
D語言數(shù)組
一元運算符重載
D語言嵌套switch語句
D語言基本語法
二元運算符重載
this指針
D語言聯(lián)合體
D語言模板
D語言嵌套循環(huán)
D語言while循環(huán)

D語言抽象類

抽象是指能夠使一個抽象類在面向對象編程的能力。抽象類是不能被實例化。該類別的所有其他功能仍然存在,及其字段、方法和構造函數(shù)以相同的方式被訪問的。不能創(chuàng)建抽象類的實例。

如果一個類是抽象的,不能被實例化,類沒有多大用處,除非它是子類。這通常是在設計階段的抽象類是怎么來的。父類包含子類的集合的通用功能,但父類本身是過于抽象而被單獨使用。

抽象類

使用abstract關鍵字來聲明一個類的抽象。關鍵字出現(xiàn)在類聲明的地方class關鍵字前。下面顯示了如何抽象類可以繼承和使用的例子。

import std.stdio;
import std.string;
import std.datetime;

abstract class Person
{
   int birthYear, birthDay, birthMonth;
   string name;
   int getAge()
   {
       SysTime sysTime = Clock.currTime();
       return sysTime.year - birthYear;
   }
}

class Employee : Person
{
   int empID;
}

void main()
{
   Employee emp = new Employee();
   emp.empID = 101;
   emp.birthYear = 1980;
   emp.birthDay = 10;
   emp.birthMonth = 10;
   emp.name = "Emp1";
   writeln(emp.name);
   writeln(emp.getAge);
}

當我們編譯并運行上面的程序,我們會得到下面的輸出。

Emp1
34

抽象函數(shù)

類似的函數(shù),類也可以做成抽象。這種功能的實現(xiàn)是不是在同級類中給出,但應在繼承的類與抽象函數(shù)的類來提供。上述例子是用抽象的功能更新,并在下面給出。

import std.stdio;
import std.string;
import std.datetime;

abstract class Person
{
   int birthYear, birthDay, birthMonth;
   string name;
   int getAge()
   {
       SysTime sysTime = Clock.currTime();
       return sysTime.year - birthYear;
   }
   abstract void print();
}

class Employee : Person
{
   int empID;

   override void print()
   {
      writeln("The employee details are as follows:");
      writeln("Emp ID: ", this.empID);
      writeln("Emp Name: ", this.name);
      writeln("Age: ",this.getAge);
   }
}

void main()
{
   Employee emp = new Employee();
   emp.empID = 101;
   emp.birthYear = 1980;
   emp.birthDay = 10;
   emp.birthMonth = 10;
   emp.name = "Emp1";
   emp.print();
}

當我們編譯并運行上面的程序,我們會得到下面的輸出。

The employee details are as follows:
Emp ID: 101
Emp Name: Emp1
Age: 34