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

WBOY
WBOY 转载
2023-08-24 08:29:11 226浏览

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

在当代计算机编程实践中,通常的做法是将面向对象编程系统(OOPS)作为编程语言的基础。这种范式将方法与数据结合在一起,为开发人员带来了有益的结果。采用OOPS可以使程序员创建一个准确的类和对象模型,通过有效地复制现实生活场景来实现无缝工作。

在这篇文章中,了解有关OOPS范例中的消息、聚合和抽象类。

什么是消息?

在计算机领域中,消息传递指的是进程之间的通信。数据传输是并行编程和面向对象编程实践中一种高效的通信方式。在使用Java时,跨不同线程发送消息与共享对象或消息的过程密切相关。与共享监视器、信号量或类似变量不同,这种方法在没有协作存储机制的情况下可能存在线程交互的可能障碍;这种方法非常有用。消息传递方法可以通过构造函数、方法或发送各种值的方式在面向对象编程中执行。

消息转发技术的主要优势如下:

  • 与共享内存模式相比,这种模式的实现要简单得多。

  • 因为这种方法对更高的连接延迟具有很高的容忍度。

  • 将其实施为并行硬件的过程要简单得多。

语法

public class MessagePassing {
   // body
}

Example

// Java program to demonstrate message passing by value

import java.io.*;
public class MessagePassing {
   void displayInt(int x, int y) {
      int z = x + y;
      System.out.println("Int Value is : " + z);
   }

   void displayFloat(float x, float y) {
      float z = x * y;
      System.out.println("Float Value is : " + z);
   }
}
class Variable {

   public static void main(String[] args) {
      MessagePassing mp= new MessagePassing();
      mp.displayInt(1, 100);
      mp.displayFloat((float)3, (float)6.9);
   }
}

输出

Int value is : 101
Float value is : 20.7

什么是聚合?

在独特的意义上,这是一种关联类型。聚合是一种单向的有向关系,准确地表达了类之间的HAS-A关系。此外,当两个类被聚合时,终止其中一个对另一个没有影响。与组合相比,它经常被指定为一种弱关系。相比之下,父类拥有子实体,这意味着子实体不能直接访问,也不能没有父对象而存在。相反,在关联中,父实体和子实体都可以独立存在。

语法

class Employee {
   int id;
   String name;
   Address address;   // Aggregation
   // body
}

Example

// Java program to demonstrate an aggregation

public class Address {
   int strNum;
   String city;
   String state;
   String country;

   Address(int street, String c, String st, String count) {
      this.strNum = street;
      this.city = c;
      this.state = st;
      this.country = coun;
   }
}
class Student {
   int rno;
   String stName;

   Address stAddr;
   Student(int roll, String name,
      Address address)
   {
      this.rno = roll;
      this.stName = name;
      this.stAddr = address;
   }
}

class Variable {
   public static void main(String args[]) {

      Address ad= new Address(10, "Bareilly", "UP", "India");
      Student st= new Student(1, "Aashi", ad);
      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: 1
Name: Aashi
Street: 10
City: Bareilly
State: UP
Country: India

什么是抽象类?

抽象是面向对象编程范式中使用的一种方法,它通过仅向用户显示相关信息而不是屏幕上的无关信息来减少程序复杂性和理解工作量。尽管实现方式有所不同,但隐藏无用数据的思想在每个面向对象编程系统实现的语言中都是相同的。在Java中实现抽象的一种技术是使用抽象类。Java允许在类中声明抽象方法和常规方法,但抽象方法不能在常规类中表达。抽象类要么有定义,要么由扩展类实现。

语法

abstract class A{}

Example

// Java program to demonstrate the abstract class

abstract class Car {

   public void details() {
      System.out.println("Manufacturing Year: 123");
   }

   abstract public void name();
}
public class Maserati extends Car {
   public void name() {
      System.out.print("Maserati!");
   }
   public static void main(String args[]){
      Maserati car = new Maserati();
      car.name();
   }
}

输出

Maserati!

结论

OOPS是许多编程语言的基本概念。它是一种基于包含方法和数据的对象的范例。消息传递是面向对象编程语言和并行编程中使用的一种通信形式。聚合是一种独特意义上的关联形式,且是严格的方向关联。抽象是一种在面向对象编程语言中使用的技术,它只向用户显示相关细节。

以上就是Java中的消息、聚合和抽象类的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除