什么是算法中的抽象数据类型,我们为什么需要它们?
What are Abstract data types in algorithms and why do we need them?
我还想引用一个我读过但无法理解的来源。
We all know by default, all primitive datatypes (int, float etc) support basic operations such as addition and subraction. The system provides the implementation for the primitive datatypes. For userdefined types we need to define operations. The implementation of these algorithms is done when we use them.
That means, user defined datatypes are defined along with their operations.
To simplify the process of solving problems, we combine dataStructures along with their operations and call it as AbstractDataType.
任何人都可以解释一个很好的例子以及现实世界的背景吗?
为什么你需要它们?
来源 Narasimha Karumanchi 的书数据结构和算法
好问题!
在应用程序基础架构和设计方面,抽象数据类型对于拥有清晰易懂的 classes 和数据模型层次结构很有用。
正如您要求的实际示例一样,以任何使用数据库的应用程序为例:比如在线商店。
原始数据类型当然有用,比如说我们用double来存储价格,用ints来存储商品数量,用string来显示和存储商品名称/描述,但是我们如何处理basket/cart呢?最简单和最推荐的方法是通过单独的 class 创建购物车的抽象表示,例如 shoppingCart,它将包含其属性:
class shoppingCart{
int numOfItems;
double totalPrice;
List<Product> products;
etc.
}
此外,我们需要 classes 来表示产品和类别,例如:
class Product{
double price;
string name;
etc.
}
将您创建的任何抽象数据类型视为您自己的 对象 ,以及它们自己的 方法 等等。
我们将使用抽象 classes 来定义扩展它们的某些子classes 的一般行为,例如:abstract class Vehicle 和 classes Car , 扩展车辆的自行车。
结构将是:
abstract class vehicle{
public void drive();
public int getNumberOfWheels();
}
这样我们可以确保 Car 和 Bike 的所有实例都将实现摘要中列出的 2 个方法 class vehicle:
class car extends vehicle{
//we have to implement the 2 methods and provide an implementation for it
}
另一位开发人员想要创建一种新型车辆(比如 "plane"),因此他现在可以看到他 有 实现这 2 个方法,无论什么。
希望能帮到你
我还想引用一个我读过但无法理解的来源。
We all know by default, all primitive datatypes (int, float etc) support basic operations such as addition and subraction. The system provides the implementation for the primitive datatypes. For userdefined types we need to define operations. The implementation of these algorithms is done when we use them. That means, user defined datatypes are defined along with their operations.
To simplify the process of solving problems, we combine dataStructures along with their operations and call it as AbstractDataType.
任何人都可以解释一个很好的例子以及现实世界的背景吗? 为什么你需要它们?
来源 Narasimha Karumanchi 的书数据结构和算法
好问题!
在应用程序基础架构和设计方面,抽象数据类型对于拥有清晰易懂的 classes 和数据模型层次结构很有用。 正如您要求的实际示例一样,以任何使用数据库的应用程序为例:比如在线商店。 原始数据类型当然有用,比如说我们用double来存储价格,用ints来存储商品数量,用string来显示和存储商品名称/描述,但是我们如何处理basket/cart呢?最简单和最推荐的方法是通过单独的 class 创建购物车的抽象表示,例如 shoppingCart,它将包含其属性:
class shoppingCart{
int numOfItems;
double totalPrice;
List<Product> products;
etc.
}
此外,我们需要 classes 来表示产品和类别,例如:
class Product{
double price;
string name;
etc.
}
将您创建的任何抽象数据类型视为您自己的 对象 ,以及它们自己的 方法 等等。
我们将使用抽象 classes 来定义扩展它们的某些子classes 的一般行为,例如:abstract class Vehicle 和 classes Car , 扩展车辆的自行车。 结构将是:
abstract class vehicle{
public void drive();
public int getNumberOfWheels();
}
这样我们可以确保 Car 和 Bike 的所有实例都将实现摘要中列出的 2 个方法 class vehicle:
class car extends vehicle{
//we have to implement the 2 methods and provide an implementation for it
}
另一位开发人员想要创建一种新型车辆(比如 "plane"),因此他现在可以看到他 有 实现这 2 个方法,无论什么。
希望能帮到你