当前位置:网站首页>什么是多范式编程语言,其中的“多范式”是什么意思?
什么是多范式编程语言,其中的“多范式”是什么意思?
2022-06-29 18:10:00 【金士顿】
转载地址:https://www.zhihu.com/question/20428688#:~:text=%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80%E4%B8%AD,%E8%8C%83%E5%BC%8F%E6%9D%A5%E6%92%B0%E5%86%99%E7%A8%8B%E5%BA%8F%E3%80%82
编程范式是指编程时的指导思想。放在编程语言里,则代表了这个语言的设计方向,即语言是为了便于遵循某种,或某些思想编程而设计的。
多范式编程语言中的多范式,是指这个语言支持使用者采用多种不同的编程范式来撰写程序。
C语言是过程式编程语言;Java、C#是面向对象式编程语言;Haskell是函数式编程语言;而C++则是多范式的编程语言。
几个栗子:
过程式:[C/C++]
#define COUNT_OF(ARR) (sizeof(ARR) / sizeof(*(ARR)))
int square(int x)
{
return x * x;
}
int main(void)
{
int numbers[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int results[COUNT_OF(numbers)];
int i = 0;
for (; i < COUNT_OF(numbers); ++i)
{
results[i] = square(numbers[i]);
}
return 0;
}
面向对象:[C#]
interface CalcI
{
int Square(int x);
}
class Calculate : CalcI
{
public int Square(int x)
{
return x * x;
}
}
class Iteration
{
public int[] ForEach(int[] arr, CalcI cc)
{
int[] results = new int[arr.Length];
for (int i = 0; i < arr.Length; ++i)
{
results[i] = cc.Square(arr[i]);
}
return results;
}
}
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[] {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] results;
Calculate cc = new Calculate();
Iteration it = new Iteration();
results = it.ForEach(numbers, cc);
}
}
泛型:[C++]
template <typename T>
auto square(T x)
{
return x * x;
}
template <typename T, std::size_t N, typename F>
auto array_map(const T(& arr)[N], F fr)
{
std::array<T, N> res;
for (size_t i = 0; i < N; ++i)
{
res[i] = fr(arr[i]);
}
return res;
}
int main(void)
{
int numbers[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto results = array_map(numbers, square<std::remove_extent_t<decltype(numbers)>>);
return 0;
}
函数式:[F#]
let square x = x * x
let numbers = [1 .. 10]
let squares = List.map square numbers
模板元:[C++]
template <size_t X>
struct square
{
enum : size_t {
value = X * X };
};
template <size_t... N>
struct array
{
enum : size_t {
count = sizeof...(N) };
};
template <typename Arr, template <size_t> class F>
struct array_map;
template <template <size_t...> class Arr, size_t... N, template <size_t> class F>
struct array_map<Arr<N...>, F>
{
using type = Arr<F<N>::value...>;
};
int main(void)
{
using numbers = array<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>;
using results = array_map<numbers, square>::type;
return 0;
}
可以看到,C++非常灵活,并不拘泥于某一种特定的编程方法论。
一个编程语言属于某种类型,并不代表这个语言只能用那种思想编程。比如C语言,虽然是过程式,但我们仍然可以利用struct抽象出class的概念,用函数指针和算法模拟面向对象里的多态特征,只是会非常麻烦。而多范式语言则可以在它所支持的这些范式之间自由切换,不会有勉强的生涩感。当然,代价就是多范式语言一般相对复杂一些。
边栏推荐
- jdbc_相關代碼
- 通过 hosts文件配置本地域名
- shell教程之循环语句for,while,until用法
- MySQL数据库每日备份并定时清理脚本
- Workflow module jar package startup error: liquibase – waiting for changelog lock
- 6.29 simulation summary
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- Servlet student management system (Mengxin hands-on version)
- Goldfish rhca memoirs: do447 build advanced job workflow -- create job template survey to set work variables
- Mysql database daily backup and scheduled cleanup script
猜你喜欢

Adobe Premiere基础-时间重映射(十)

garbage collector

Amazing pandaverse:meta "borderless, to activate fashion attributes in the new journey of 2.0

How to use idea?

踩坑记:JSON.parse和JSON.stringify

Adobe Premiere基础-不透明度(蒙版)(十一)

山东大学项目实训(六)点击事件展示折线图

第02章_MySQL的数据目录

Adobe Premiere foundation - time remapping (10)

markdown知识轻轻来袭
随机推荐
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
[tcapulusdb knowledge base] tcapulusdb system user group introduction
Markdown knowledge comes gently
Goldfish rhca memoirs: do447 building advanced job workflow -- using fact cache to improve performance
美法官裁定,被控掩盖黑客行为的Uber前安全主管必须面对欺诈指控
jdbc认识上手
Failed to allocate graph: myriad device is not opened
Find the maximum XOR value in the sequence given a number (01 Dictionary)
Usage of BeanUtils property replication
Sword finger offer 34 Path DFS method for binary tree neutralization
Shell tutorial circular statements for, while, until usage
JDBC knowledge
[how the network is connected] Chapter 3 explores hubs, switches and routers
Adobe Premiere foundation - opacity (matte) (11)
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
VMware installation esxi
Adobe Premiere基础-不透明度(混合模式)(十二)
jdbc_ Related codes
How do I add SmartArt to slides in PowerPoint?