Golang 创建抽象类

Golang 创建抽象类

在本文中,我们将学习如何使用Golang编程创建一个抽象类。

抽象类 - 一个受限类被称为抽象类,它不能用来创建一个对象来访问抽象类,它必须从另一个类继承。

Go接口缺乏字段并禁止在其中定义方法。任何类型必须实现每个接口方法才能属于该接口类型。在GO中有情况下具有方法的默认实现和默认字段是有用的。在学习如何实现抽象类之前,让我们首先了解抽象类的先决条件:

  • 抽象类中应该有默认字段。
  • 抽象类中应该有默认方法。
  • 不能直接创建抽象类的实例。

示例

一个接口(抽象接口)和一个结构体将被组合在一起(抽象具体类型)。它们一起可以提供抽象类的特性。请参考下面的程序:

package main

import "fmt"
// fmt package allows us to print anything on the screen

// Define a new data type "Triangle" and define base and height properties in it
type Triangle struct {
   base, height float32
}

// Define a new data type "Square" and assign length as a property to it.
type Square struct {
   length float32
}

// Define a new data type "Rectangle" and assign length and width as properties to it
type Rectangle struct {
   length, width float32
}

// Define a new data type "Circle" and assign radius as a property to it
type Circle struct {
   radius float32
}

// defining a method named area on the struct type Triangle
func (t Triangle) Area() float32 {
   // finding the area of the triangle
   return 0.5 * t.base * t.height
}

// defining a method named area on the struct type Square
func (l Square) Area() float32 {
   // finding the area of the square
   return l.length * l.length
}

// defining a method named area on the struct type Rectangle
func (r Rectangle) Area() float32 {
   // finding the area of the Rectangle
   return r.length * r.width
}

// defining a method named area on the struct type Circle
func (c Circle) Area() float32 {
   // finding the area of the circle
   return 3.14 * (c.radius * c.radius)
}

// Define an interface that contains the abstract methods
type Area interface {
   Area() float32
}

func main() {
   // Assigning the values of length, width and height to the properties defined above
   t := Triangle{base: 15, height: 25}
   s := Square{length: 5}
   r := Rectangle{length: 5, width: 10}
   c := Circle{radius: 5}

   // Define a variable of type interface
   var a Area

   // Assign to the interface a variable of type "Triangle"
   a = t
   // printing the area of triangle
   fmt.Println("Area of Triangle", a.Area())

   // Assign to the interface a variable of type "Square"
   a = s
   // printing the area of the square
   fmt.Println("Area of Square", a.Area())

   // Assign to the interface a variable of type "Rectangle"
   a = r
   // printing the area of the rectangle
   fmt.Println("Area of Rectangle", a.Area())

   // Assign to the interface a variable of type "Circle"
   a = c
   // printing the area of the circle
   fmt.Println("Area of Circle", a.Area())
}

输出

Area of Triangle 187.5
Area of Square 25
Area of Rectangle 50
Area of Circle 78.5

描述

  • 首先,我们导入了fmt包。

  • 然后,我们在Triangle、Circle、Square和Rectangle下定义了一些抽象类,并在其中定义了属性。

  • 接下来,我们定义了与这些抽象类对应的一些方法。

  • 然后,我们创建了一个包含一个抽象方法的Area接口。

  • 调用main()函数。

  • 分别将长度、宽度和高度赋值给图形,以求得其面积。

  • 定义一个接口变量a。

  • 将三角形、正方形、矩形和圆分别赋值给抽象变量,并将它们的面积打印在屏幕上。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程