嗨,大家好,希望所有编程爱好者都做得很好。 今天,我们将讨论在开发软件时每个人都应了解的基本编程设计原理和概念。 好吧,软件开发并不容易。 如果编程方式不正确,编程/编码可能是一项艰巨的任务。 希望你们会喜欢这篇文章。 因此,不浪费时间就让我们开始吧。 [我将在这里使用JavaScript,但是您可以使用任何您喜欢的语言! ]
在这篇文章中,我们将讨论:
1.类和对象
2.构造函数
3.静态方法
4.递归
5.循环
6.糟糕
7.库/模块/包(都表示相同的意思)
类和对象:
类是创建对象的蓝图。 它具有成员函数和成员变量。 对象是class的实例。 可以在对象的帮助下访问类成员函数和变量。
构造函数:
这是一种特殊的方法,用于初始化新创建的对象,并在对象分配内存后立即调用。
class Person {
constructor (name){
console .log( 'Hello from' + ' ' + name);
}
}
const p = new Person( 'karan' ); // Output Hello from karan
静态方法/静态函数
它们确实是我使用的最重要的方法之一。 他们属于阶级本身。 可以在没有类的实例(对象)的情况下使用它们。 它们不修改类的行为,但是在要查看某些数据等时功能非常强大。只需在函数的前面添加静态内容即可。
递归:
递归是一种函数调用自身的技术。 它们是称为终止条件的终止条件,该条件终止了递归操作。
class MathematicalOperation {
constructor (){
console .log( 'Mathmatetical operation class' );
}
square(x){
return x*x;
}
factorial(x){
if (x== 0 || x== 1 ){
return 1 ;
}
return x* this .factorial(x -1 );
}
static available_methods(){
console .log( 'This class has two methods. Square and factorial.' );
}
}
const m = new MathematicalOperation; // output Mathmatetical operation class
console .log(m.square( 5 )); // output 25
console .log(m.factorial( 5 )); // output 120
MathematicalOperation.available_methods(); // his class has two methods. Square and factorial.
循环:
编程语言中的循环是一项功能,可在某些条件评估为真时,促进重复执行一组指令。
a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
// For of Loop
for ( let i of a){
console .log(i);
}
// While loop
let j = 0 ;
while (j < 10 ){
console .log(a[j]);
j++;
}
OOPS(面向对象设计)
使用类和对象设计程序是一种方法论或范式。 OOPS的主要组成是封装,抽象,继承和多态性。 让我们仔细看看它们-
封装:将有关实体的所有内容封装在类内。 即。 将函数和数据成员放在类中。
抽象 :抽象是哎呀一个非常重要的概念。 它只是意味着您将重要的事情隐藏在类内部,并且仅提供要通过对象显示的必要或相关信息。 (它隐藏了实现的细节)
继承:这是一个类获取子类属性的概念。 我们可以扩展此子类,并使用基类/父类中已定义的方法。 继承的主要目的是提供可重用的代码并帮助您实现DRY
class Person {
constructor (name,gender){
this .name = name;
this .gender = gender;
}
getPersonName(){
return this .name;
}
getPersonGender(){
return this .gender;
}
}
class Male extends Person {
constructor (name){
super (name, "male" );
}
}
class Female extends Person {
constructor (name){
super (name, "female" );
}
}
// creating objects ...
const m = new Male( "karan" );
const f = new Female( "Anna" );
// checking results ...
console .log(m.getPersonGender()); // male
console .log(m.getPersonName()); // karan
console .log(f.getPersonGender()); // female
console .log(f.getPersonName()); // Anna
多态性:通常表示采取多种形式的能力。 通常,多态有2种类型-编译时间和运行时间。
1。 编译时间 :函数重载是编译时间多态性的一个示例。 函数名称相同,参数数据类型不同。
2. 运行时间: 函数覆盖是运行时多态性的一个示例。 让我们看一个例子。
class Overriding {
print(){
console .log( 'this is print function' );
}
print(){
console .log( 'this will override the above print function' );
}
}
const override = new Overriding();
override.print(); // output : this will override the above print function
库/包/模块(都表示相同的意思):
它只是包裹或编写以重复工作的一个函数或函数列表。 通常,SDE可以与库一起使用,也可以创建自己的库。 自动化电镀代码。[最重要]。 每个程序员都应该知道如何创建一个库,否则他将花费大量的时间一次又一次地编写相同的代码……。
就是这个帖子。 我希望你喜欢它 。 再次感谢您抽出宝贵时间阅读本文。 再见 保重 并保持编码
From: https://hackernoon.com/7-programming-concepts-everyone-should-know-with-code-s028v305x