您的位置:首页 > 编程语言 > C#

C#入门9.10——类的继承

2016-08-20 11:48 316 查看
面向对象的三个基本特征是封装、继承、多态。

三个类

父类Clerk:_name,Name;_department,Department

子类Sales:_name,Name;_department,Department;_salesTarget,SalesTarget

子类Technical Support:_name,Name;_department,Department;_satisfactionRate,SatisfactionRate

对于类而言,所谓继承,就是子类包含父类的数据结构和行为方式,包括字段、属性、方法和事件。

尽管在子类本身的定义中没有包含这些定义,但仍然可以使用这些父类成员。

在类的继承中,被继承的类叫基类或父类,继承的类交派生类或子类

当一个类从另一个类派生出来时,派生类就自然具有基类数据成员、属性成员和方法成员等,基类定义中这些成员的代码,已不需要在派生类定义中重写,在派生类的定义中,只需编写基类定义中所不具有的代码即可。

目的:

1.提高了代码的重用性。

2.提高程序设计的效率。

3.为程序设计中的特别需要提供了编写代码的自由空间,从而提高了已有程序设计成果的可拓展性。

类继承注意的规则:

1.单根性

2.传递性(查看类图)右键解决方案下面的方案名,查看,类图。

3.派生类定义与基类同名的成员,则覆盖基类成员。

4.派生类自然继承基类的成员,但不能继承基类的构造函数的成员。(需要用:base来传递)

如果用户希望一个类不被作为基类使用,那么就必须用sealed关键字。(sealed class Test)

唯一的限制是抽象类不能作为封闭的类使用,因为抽象类的本质决定它们必须被作为基类使用。

封闭类的作用是防止意外的派生操作。具体地说,因为编译器确定这个类没有任何派生类,所以可以将封闭类实例上的虚拟函数成员调用转换为非虚拟调用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{
class TechnicalSupport : Clerk
{
private double _satisfactionRate;

public double SatisfactionRate
{
get
{
return _satisfactionRate;
}

set
{
_satisfactionRate = value;
}
}
public TechnicalSupport(string name, string department, double satisfactionRate) : base(name, department)
{
this.SatisfactionRate = satisfactionRate;
}
public void tsSayHello()
{ Console.WriteLine("大家好,我是{0}的{1},我的服务满意率为{2}", this.Department, this.Name, this.SatisfactionRate); }
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{
//继承于Clerk
class Sales : Clerk
{
/* private new string _name;
//new关键字可以用来隐藏基类中同名的成员。
//new第二个用法:实例化对象*/
private int _saleTarget;

public int SaleTarget
{
get
{
return _saleTarget;
}

set
{
_saleTarget = value;
}
}

/*
方法1:
public Sales(string name, string department, int salesTarget)
{
this.Name = name;
this.Department = department;
this.SaleTarget = salesTarget;
}*/

public Sales(string name, string department, int salesTarget) : base(name, department)
{
this.SaleTarget = salesTarget;
}
public void sSayHello()
{
Console.WriteLine("大家好,我是{0}的{1},我的销售目标是{2}", this.Department, this.Name, this.SaleTarget);
}
}
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{

class Clerk
{
private string _name;
//private由于私有,不能被子类访问
public string Name
{
get
{
return _name;
}

set
{
_name = value;
}
}
private string _department;
public string Department
{
get
{
return _department;
}

set
{
_department = value;
}
}
//构造函数
//子类中不能继承父类中的构造函数,但是会默认的调用父类中的无参构造函数
//两种方法
//1.在父类中再写一个无参的构造函数,在每个子类当中都需要再进行一次构造函数的重写与各个字段的初始化
//2.使用关键字:base()
/*方法1:public Clerk()
{

}*/
public Clerk(string name ,string department)
{
this.Name = name;
this.Department = department;
}
public void cSayHello()
{
Console.WriteLine("大家好,我是{0}的{1}",this.Department,this.Name);
}

}

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{

class Program
{
static void Main(string[] args)
{
Clerk zsClerk = new Clerk("张三","人事部");
Sales zsSales = new Sales("张三","人事部",2500);
TechnicalSupport zsTC = new TechnicalSupport("张三", "人事部", 0.98);
Console.ReadKey();
}
}
}


在类的继承中,继承构造函数时有两种方法。

1. public text(int a,int b,int c) : base (a,b) { }

2. public text(int a,int b,int c) { } 然后在父类中写一个无参的构造函数,方法1中的:base (a,b) { } 就相当于父类中无参的构造函数。

补:

1.如何实现继承。2.继承中的构造函数。3.重载、重写、隐藏在继承中是如何实现的。

http://wenku.baidu.com/link?url=buS9hbbxBRaikofz6zlqy6gfyJERT8HX9PgHw2xmFmlSHivjHs8nHWloMtM_36Mu6ez4G7a1t9HegxfqRwOpnpdtT_j-KY792U1_tYkzeKq



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: