Java中的訊息、聚合和抽象類
在當代計算機程式設計實踐中,程式語言通常將面向物件程式設計系統 (OOPS) 作為其基礎。這種正規化將方法與資料融合在一起,為開發人員帶來了有益的結果。採用 OOPS 使程式設計師能夠建立準確的類和物件模型,透過有效地複製現實生活場景,實現無縫執行。
本文將學習 OOPS 正規化中的訊息、聚合和抽象類。
什麼是訊息?
在計算機方面,訊息傳遞是指程序之間的通訊。資料的傳輸是並行程式設計和麵向物件程式設計實踐中一種有效的通訊方式。具體使用 Java 時,跨不同執行緒傳送訊息的過程與共享物件或訊息的過程非常相似。與共享監視器、訊號量或類似變數(在沒有協作儲存機制的情況下,執行緒互動可能存在障礙)不同,這種方法證明非常有用。訊息傳遞方法可以在 OOPs 中透過多種方式執行,包括透過建構函式、方法或傳送各種值。
訊息轉發技術的關鍵優勢如下:
與共享記憶體正規化相比,它更易於實現。
因為這種方法對更大的連線延遲具有很高的容忍度。
將其用於建立並行硬體要容易得多。
語法
public class MessagePassing {
// body
}
示例
// 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
}
示例
// 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
什麼是抽象類?
抽象是在 OOPS 正規化中使用的一種方法,它透過僅向用戶顯示相關資訊而不是螢幕上的無關資訊來降低程式的複雜性和理解難度。儘管實現方式有所不同,但在每個面向物件程式設計系統實現的語言中,隱藏無用資料的思想都是相同的。在 Java 中實現抽象的一種方法是使用抽象類。Java 允許在類中宣告抽象方法和常規方法,但是抽象方法不能在常規類中表達。抽象類要麼有定義,要麼擴充套件類實現它們。
語法
abstract class A{}
示例
// 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 是許多程式語言的基本概念。它是一個基於物件的正規化,該物件包含方法和資料。訊息傳遞是在面向物件程式語言以及並行程式設計中使用的一種通訊形式。聚合從獨特的意義上說是一種關聯形式,並且是嚴格定向的關聯。抽象是在面向物件程式語言中使用的一種技術,它只向用戶顯示相關細節。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP