面向对象编程中最重要的概念之一就是继承,继承使无涯教程可以用另一个类来定义一个类,这使创建和维护应用程序变得更加容易,这也提供了重用代码功能和快速实现时间的机会。

创建类时,程序员可以指定新类应继承现有类的成员,而不必编写全新的数据成员和成员函数。此现有类称为基类,而新类称为子类。

基类和子类

子类继承基类的成员,并拥有自己的成员,子类使用 inherit 关键字定义,如下所示-

type MyDerived(...) =
   inherit MyBase(...)

在F#中,一个类最多可以有一个直接基类,如果未使用 inherit 关键字指定基类,则该类隐式继承自对象(Object)。

type Person(name) =
   member x.Name = name
   member x.Greet() = printfn "Hi, I'm %s" x.Name

type Student(name, studentID : int) =
   inherit Person(name)
   let mutable _GPA = 0.0
   member x.StudentID = studentID
   member x.GPA
      with get() = _GPA
      and set value = _GPA <- value

type Teacher(name, expertise : string) =
   inherit Person(name)

   let mutable _salary = 0.0
   member x.Salary
      with get() = _salary
      and set value = _salary <- value
   member x.Expertise = expertise

//using the subclasses
let p = new Person("Mohan")
let st = new Student("Zara", 1234)
let tr = new Teacher("Mariam", "Java")

p.Greet()
st.Greet()
tr.Greet()

编译并执行程序时,将产生以下输出-

Hi, I'm Mohan
Hi, I'm Zara
Hi, I'm Mariam

覆盖方法

F#中的方法默认情况下不可覆盖。

要覆盖派生类中的方法,您必须使用 abstractdefault 关键字将方法声明为可覆盖,如下所示:

type Person(name) =
   member x.Name=name
   abstract Greet : unit -> unit
   default x.Greet()=printfn "Hi, I'm %s" x.Name

现在,可以在派生类中覆盖Person类的 Greet 方法。以下示例演示了这一点-

type Person(name) =
   member x.Name = name
   abstract Greet : unit -> unit
   default x.Greet() = printfn "Hi, I'm %s" x.Name

type Student(name, studentID : int) =
   inherit Person(name)

   let mutable _GPA = 0.0

   member x.StudentID = studentID
   member x.GPA
      with get() = _GPA
      and set value = _GPA <- value
   override x.Greet() = printfn "Student %s" x.Name

type Teacher(name, expertise : string) =
   inherit Person(name)
   let mutable _salary = 0.0
   member x.Salary
      with get() = _salary
      and set value = _salary <- value

   member x.Expertise = expertise
   override x.Greet() = printfn "Teacher %s." x.Name

//使用子类
let p = new Person("Mohan")
let st = new Student("Zara", 1234)
let tr = new Teacher("Mariam", "Java")

//default Greet
p.Greet()

//Overriden Greet
st.Greet()
tr.Greet()

编译并执行程序时,将产生以下输出-

Hi, I'm Mohan
Student Zara
Teacher Mariam.

抽象类

您不能创建抽象类的,因为该类尚未完全实现。

[<AbstractClass>]
type Person(name) =
   member x.Name = name
   abstract Greet : unit -> unit

type Student(name, studentID : int) =
   inherit Person(name)
   let mutable _GPA = 0.0
   member x.StudentID = studentID
   member x.GPA
      with get() = _GPA
      and set value = _GPA <- value
   override x.Greet() = printfn "Student %s" x.Name

type Teacher(name, expertise : string) =
   inherit Person(name)
   let mutable _salary = 0.0
   member x.Salary
      with get() = _salary
      and set value = _salary <- value
   member x.Expertise = expertise
   override x.Greet() = printfn "Teacher %s." x.Name

let st = new Student("Zara", 1234)
let tr = new Teacher("Mariam", "Java")

//Overriden Greet
st.Greet()
tr.Greet()

编译并执行程序时,将产生以下输出-

Student Zara
Teacher Mariam.

参考链接

https://www.learnfk.com/fsharp/fsharp-inheritance.html