📜  OOPS 中的消息、聚合和抽象类

📅  最后修改于: 2022-05-13 01:54:56.050000             🧑  作者: Mango

OOPS 中的消息、聚合和抽象类

面向对象编程系统(OOPS)是许多编程语言的基本概念。它是一种基于包含方法和数据的对象的范式。这个概念用于使编程成为一个类和对象模型,其行为类似于真实世界的场景。在本文中,我们将了解 OOPS 范式中的消息、聚合、组合和抽象类。

消息传递:就计算机而言,消息传递是进程之间的通信。它是面向对象编程和并行编程中使用的一种通信形式。 Java中的消息传递就像从一个线程向另一个线程发送一个对象,即消息。当线程没有共享内存并且无法共享监视器或信号量或任何其他共享变量进行通信时使用它。以下是消息传递技术的主要优点:

  1. 这个模型比共享内存模型更容易实现。
  2. 实现此模型以构建并行硬件要容易得多,因为它可以容忍更高的通信延迟。

在 OOP 中,有很多方法可以实现消息传递技术,例如通过构造函数传递消息、通过方法传递消息或传递不同的值。以下是值的消息传递技术的简单实现:

Java
// Java program to demonstrate
// message passing by value
 
import java.io.*;
 
// Implementing a message passing
// class
public class MessagePassing {
 
    // Implementing a method to
    // add two integers
    void displayInt(int x, int y)
    {
        int z = x + y;
        System.out.println(
            "Int Value is : " + z);
    }
 
    // Implementing a method to multiply
    // two floating point numbers
    void displayFloat(float x, float y)
    {
        float z = x * y;
        System.out.println(
            "Float Value is : " + z);
    }
}
class GFG {
 
    // Driver code
    public static void main(String[] args)
    {
        // Creating a new object
        MessagePassing mp
            = new MessagePassing();
 
        // Passing the values to compute
        // the answer
        mp.displayInt(1, 100);
        mp.displayFloat((float)3, (float)6.9);
    }
}


Java
// Java program to demonstrate
// an aggregation
 
// Implementing the address
// class
class Address {
    int strNum;
    String city;
    String state;
    String country;
 
    // Constructor of the address
    // class
    Address(int street, String c,
            String st, String count)
    {
        this.strNum = street;
        this.city = c;
        this.state = st;
        this.country = coun;
    }
}
 
// Creating a student class
class Student {
    int rno;
    String stName;
 
    // HAS-A relationship with the
    // Address class
    Address stAddr;
    Student(int roll, String name,
            Address addr)
    {
        this.rno = roll;
        this.stName = name;
        this.stAddr = addr;
    }
}
 
class GFG {
 
    // Driver code
    public static void main(String args[])
    {
 
        // Creating an address object
        Address ad
            = new Address(10, "Delhi",
                          "Delhi", "India");
 
        // Creating a new student
        Student st
            = new Student(96, "Utkarsh", ad);
 
        // Printing the details of
        // the student
        System.out.println("Roll no: "
                           + st.rno);
 
        System.out.println("Name: "
                           + st.stName);
 
        System.out.println("Street: "
                           + st.stAddr.strNum);
 
        System.out.println("City: "
                           + st.stAddr.city);
 
        System.out.println("State: "
                           + st.stAddr.state);
 
        System.out.println("Country: "
                           + st.stAddr.country);
    }
}


Java
// Java program to demonstrate the
// abstract class
 
// Implementing the abstract class
// Car
abstract class Car {
 
    // A normal method which contains
    // the details of the car
    public void details()
    {
        System.out.println(
            "Manufacturing Year: 123");
    }
 
    // The name of the car might change
    // from one car to another. So,
    // implementing an abstract method
    abstract public void name();
}
 
// A class Maserati which
// extends the Car
public class Maserati extends Car {
 
    // Naming the car
    public void name()
    {
        System.out.print(
            "Maserati!");
    }
 
    // Driver code
    public static void main(String args[])
    {
        Maserati car = new Maserati();
        car.name();
    }
}


输出:
Int Value is : 101
Float Value is : 20.7

聚合:这是一种特殊方式的关联形式。聚合是严格的定向关联(即),单向关联,表示类之间的HAS-A关系。此外,在聚合中,结束两者之间的一个类不会影响另一个类。它通常表示为弱关联,而组合则表示为强关联。在组合中,父实体拥有子实体,这意味着没有父实体就不会存在子实体,也不能直接访问子实体。而在关联中,父实体和子实体可以独立存在。让我们举个例子来理解这个概念。让我们学习一个学生课程和一个地址课程。地址类代表学生地址。由于每个学生都有一个地址,因此它有一个关系。但是,地址完全独立于学生。如果一些学生离开学校或学院,他或她仍然有一个地址。这意味着该地址可以在没有学生的情况下独立生存。因此,这是一个聚合。以下是上述示例的实现:

Java

// Java program to demonstrate
// an aggregation
 
// Implementing the address
// class
class Address {
    int strNum;
    String city;
    String state;
    String country;
 
    // Constructor of the address
    // class
    Address(int street, String c,
            String st, String count)
    {
        this.strNum = street;
        this.city = c;
        this.state = st;
        this.country = coun;
    }
}
 
// Creating a student class
class Student {
    int rno;
    String stName;
 
    // HAS-A relationship with the
    // Address class
    Address stAddr;
    Student(int roll, String name,
            Address addr)
    {
        this.rno = roll;
        this.stName = name;
        this.stAddr = addr;
    }
}
 
class GFG {
 
    // Driver code
    public static void main(String args[])
    {
 
        // Creating an address object
        Address ad
            = new Address(10, "Delhi",
                          "Delhi", "India");
 
        // Creating a new student
        Student st
            = new Student(96, "Utkarsh", ad);
 
        // Printing the details of
        // the student
        System.out.println("Roll no: "
                           + st.rno);
 
        System.out.println("Name: "
                           + st.stName);
 
        System.out.println("Street: "
                           + st.stAddr.strNum);
 
        System.out.println("City: "
                           + st.stAddr.city);
 
        System.out.println("State: "
                           + st.stAddr.state);
 
        System.out.println("Country: "
                           + st.stAddr.country);
    }
}
输出:
Roll no: 96
Name: Utkarsh
Street: 10
City: Delhi
State: Delhi
Country: India

抽象类:抽象是 OOPS 范式中使用的一种技术,它只向用户显示相关的细节,而不是在屏幕上显示不必要的信息,有助于降低程序的复杂性和理解的努力。每种 OOPS 实现的语言都有不同的实现方式,但具有相同的隐藏不相关数据的概念。抽象类是Java中实现抽象的一种方式。在Java中,抽象类是使用abstract关键字声明的,可以具有抽象方法和普通方法,但是普通类不能在其中声明抽象方法。抽象类要么有定义,要么由扩展类实现。让我们举个例子来理解为什么要实现抽象。在此示例中,我们将创建在特定年份制造的汽车。在特定年份可以生产许多汽车。但是汽车的属性不会改变。所以,这里的汽车名称是抽象的,其余属性是不变的。以下是上述示例的实现:

Java

// Java program to demonstrate the
// abstract class
 
// Implementing the abstract class
// Car
abstract class Car {
 
    // A normal method which contains
    // the details of the car
    public void details()
    {
        System.out.println(
            "Manufacturing Year: 123");
    }
 
    // The name of the car might change
    // from one car to another. So,
    // implementing an abstract method
    abstract public void name();
}
 
// A class Maserati which
// extends the Car
public class Maserati extends Car {
 
    // Naming the car
    public void name()
    {
        System.out.print(
            "Maserati!");
    }
 
    // Driver code
    public static void main(String args[])
    {
        Maserati car = new Maserati();
        car.name();
    }
}
输出:
Maserati!