基類與派生類的轉換
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace chap4_3
{
class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public double Salary { get; set; }
public Employee(string name, int age, double salary)
{
this.Name = name;
this.Age = age;
this.Salary = salary;
}
public virtual void Disp()
{
Console.WriteLine("基類方法");
Console.WriteLine("{0},{1},{2}",this.Name,this.Age,this.Salary);
}
}
class Manager : Employee
{
public double Bonus { get; set; }
public Manager(string name, int age, double salary, double bonus):base(name,age,salary)
{
this.Bonus = bonus;
}
public new void Disp()
{
Console.WriteLine("繼承類方法");
Console.WriteLine("{0},{1},{2}", this.Name, this.Age, this.Salary+this.Bonus);
}
}
class Program
{
static void Main(string[] args)
{
Manager mng = new Manager("zxf", 38, 4000, 1000);
Employee emp = mng;
emp.Disp(); //如果前面是new關鍵字,此時呼叫的是基類的方法;如果是override,呼叫的是派生類的方法
Console.ReadLine();
}
}
}
相關文章
- 繼承 基類與派生類繼承
- 《C++ Primer》派生類到基類轉換的可訪問性C++
- C# 繼承 子類(派生類) 父類(基類)C#繼承
- c#之基類派生類base_小記C#
- c# 基類派生類成員方法訪問C#
- C++派生類物件訪問基類的protected成員C++物件
- 泛型類派生子類泛型
- c#之基類派生類base_小記(2)C#
- 類的繼承和派生繼承
- TreeView 的派生類: TreeViewEx 實現 NodeShowToolTip、NodeDoubleClick 事件 (轉)View事件
- 字串與日期型別轉換的工具類字串型別
- C++,繼承,基類和派生類指標間賦值等知識C++繼承指標賦值
- 類的轉換函式函式
- [C++]繼承和派生類C++繼承
- C++派生類的拷貝構造C++
- 顧客類的派生(C#程式碼)C#
- 類轉json的基類實現JSON
- Date轉換工具類
- 漫談QWidget及其派生類(二)
- WPF 控制元件類派生關係控制元件
- CDC的四個派生類CClientDC,CPaintDC,CWindowDC,CMetaFileDclientAI
- 父類和子類之間的轉換問題
- java工具類之編碼轉換工具類Java
- C#中介面、基類與類C#
- 資料類新轉換
- Cacheable 類轉換異常
- 型別轉換工具類型別
- 進位制轉換的工具類
- YTU-OJ-Problem J: B3 指向基類的指標訪問派生類的成員函式指標函式
- java 類基礎(轉)Java
- 最簡單的編寫基於執行緒的程式碼的方法之一:派生執行緒類(轉)執行緒
- LocalDateTime工具類(常用轉換)LDA
- 物件與XML之間互相轉化的一個基類物件XML
- ADO資料與XML資料間的轉換的類(ASP實現) (轉)XML
- C#基礎:多型:基類可以定義並實現虛(virtual)方法,派生類可以重寫(override)這些方法...C#多型IDE
- 【Java】基本資料、包裝類間轉換與處理Java
- [Java] 基本資料、包裝類間轉換與處理Java
- HTML元素的分類,特性以及相互轉換HTML