📜  继承与多态之间的区别

📅  最后修改于: 2021-05-30 14:41:44             🧑  作者: Mango

遗产:
继承是在其中创建一个新类的继承,新类继承已存在的类的属性。它支持代码可重用性的概念,并减少了面向对象编程中代码的长度。

继承的类型有:

  1. 单继承
  2. 多级继承
  3. 多重继承
  4. 混合继承
  5. 层次继承

继承示例:

C++
#include "iostream"
using namespace std;
  
class A {
    int a, b;
  
public:
    void add(int x, int y)
    {
        a = x;
        b = y;
        cout << (a + b) << endl;
    }
};
  
class B : public A {
public:
    void print(int x, int y)
    {
        add(x, y);
    }
};
  
int main()
{
    B b1;
    b1.print(5, 6);
}


Java
class A {
    int a, b;
    public void add(int x, int y)
    {
        a = x;
        b = y;
        System.out.println("addition of a + b is:" + (a + b));
    }
}
class B extends A {
    public void sum(int x, int y)
    {
        add(x, y);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        B b1 = new B();
        b1.sum(5, 6);
    }
}


C++
#include "iostream"
using namespace std;
  
class A {
    int a, b, c;
  
public:
    void add(int x, int y)
    {
        a = x;
        b = y;
        cout << "addition of a+b is:" << (a + b) << endl;
    }
  
    void add(int x, int y, int z)
    {
        a = x;
        b = y;
        c = z;
        cout << "addition of a+b+c is:" << (a + b + c) << endl;
    }
  
    void print()
    {
        cout << "Class A's method is running" << endl;
    }
};
  
class B : public A {
public:
    void print()
    {
        cout << "Class B's method is running" << endl;
    }
};
  
int main()
{
    A a1;
  
    // method overloading (Compile-time polymorphism)
    a1.add(6, 5);
  
    // method overloading (Compile-time polymorphism)
    a1.add(1, 2, 3);
  
    B b1;
  
    // Method overriding (Run-time polymorphism)
    b1.print();
}


Java
class A {
    int a, b, c;
  
    public void add(int x, int y)
    {
        a = x;
        b = y;
        System.out.println("addition of a+b is:" + (a + b));
    }
  
    public void add(int x, int y, int z)
    {
        a = x;
        b = y;
        c = z;
        System.out.println("addition of a+b+c is:" + (a + b + c));
    }
  
    public void print()
    {
        System.out.println("Class A's method is running");
    }
};
  
class B extends A {
    public void print()
    {
        System.out.println("Class B's method is running");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        A a1 = new A();
  
        // method overloading (Compile-time polymorphism)
        a1.add(6, 5);
  
        // method overloading (Compile-time polymorphism)
        a1.add(1, 2, 3);
  
        B b1 = new B();
  
        // Method overriding (Run-time polymorphism)
        b1.print();
    }
}


输出:

addition of a+b is:11 

在这里,类B是继承基类A的属性( add方法)的派生类。

多态性:
多态是我们可以以多种形式或方式执行任务的方式。它适用于功能或方法。多态性允许对象决定在编译时以及在运行时实现哪种形式的函数。

多态的类型有:

  1. 编译时多态(方法重载)
  2. 运行时多态(方法覆盖)

多态的示例:

C++

#include "iostream"
using namespace std;
  
class A {
    int a, b, c;
  
public:
    void add(int x, int y)
    {
        a = x;
        b = y;
        cout << "addition of a+b is:" << (a + b) << endl;
    }
  
    void add(int x, int y, int z)
    {
        a = x;
        b = y;
        c = z;
        cout << "addition of a+b+c is:" << (a + b + c) << endl;
    }
  
    void print()
    {
        cout << "Class A's method is running" << endl;
    }
};
  
class B : public A {
public:
    void print()
    {
        cout << "Class B's method is running" << endl;
    }
};
  
int main()
{
    A a1;
  
    // method overloading (Compile-time polymorphism)
    a1.add(6, 5);
  
    // method overloading (Compile-time polymorphism)
    a1.add(1, 2, 3);
  
    B b1;
  
    // Method overriding (Run-time polymorphism)
    b1.print();
}

Java

class A {
    int a, b, c;
  
    public void add(int x, int y)
    {
        a = x;
        b = y;
        System.out.println("addition of a+b is:" + (a + b));
    }
  
    public void add(int x, int y, int z)
    {
        a = x;
        b = y;
        c = z;
        System.out.println("addition of a+b+c is:" + (a + b + c));
    }
  
    public void print()
    {
        System.out.println("Class A's method is running");
    }
};
  
class B extends A {
    public void print()
    {
        System.out.println("Class B's method is running");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        A a1 = new A();
  
        // method overloading (Compile-time polymorphism)
        a1.add(6, 5);
  
        // method overloading (Compile-time polymorphism)
        a1.add(1, 2, 3);
  
        B b1 = new B();
  
        // Method overriding (Run-time polymorphism)
        b1.print();
    }
}

输出:

addition of a+b is:11
addition of a+b+c is:6
Class B's method is running 

继承与多态之间的区别:

S.NO Inheritance Polymorphism
1. Inheritance is one in which a new class is created (derived class) that inherits the features from the already existing class(Base class). Whereas polymorphism is that which can be defined in multiple forms.
2. It is basically applied to classes. Whereas it is basically applied to functions or methods.
3. Inheritance supports the concept of reusability and reduces code length in object-oriented programming. Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).
4. Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance. Whereas it can be compiled-time polymorphism (overload) as well as run-time polymorphism (overriding).
5. It is used in pattern designing. While it is also used in pattern designing.
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”