国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
編譯時多態(tài)性示例
執(zhí)行編譯時多態(tài)性的算法
執(zhí)行編譯時多態(tài)性的語法
遵循的方法
方法1:使用數(shù)字參數(shù)執(zhí)行編譯時多態(tài)性
con_str方法的使用
示例4
輸出
數(shù)據(jù)類型方法的使用
序列參數(shù)方法的使用
方法2:render()方法的使用
結(jié)論
首頁 Java java教程 在Java中編譯時間多態(tài)性

在Java中編譯時間多態(tài)性

Feb 07, 2025 am 11:39 AM
java

Compile Time Polymorphism in Java

Java中的多態(tài)性是指Java環(huán)境中對象的一種能力聲明。它允許我們以不同的方式執(zhí)行相同的過程。Java中存在兩種多態(tài)性:

  • 編譯時多態(tài)性方法
  • 運行時多態(tài)性方法

今天,我們將討論使用方法重載和運算符重載的編譯時多態(tài)性。

編譯時多態(tài)性示例

這是一個示例:

void ARBRDD() { ... }
void ARBRDD(int num1 ) { ... }
void ARBRDD(float num1) { ... }
void ARBRDD(int num1 , float num2 ) { ... }
//顯示(char a)的值
//顯示(char a, char b)的值
//顯示(float a, float b)的值
//顯示(int a, int b)的值
//顯示(int a, float b)的值
//顯示(float a, int b)的值
int sum value of (int, int);
String sum value of (int, int);

執(zhí)行編譯時多態(tài)性的算法

在這個可能的算法中,我們將向您展示如何在Java環(huán)境中執(zhí)行編譯時多態(tài)性。通過使用此算法,我們將構(gòu)建一些Java語法,以有效的方式解釋該過程。

  • 步驟1 - 開始該過程。
  • 步驟2 - 導(dǎo)入并聲明用于運行該方法的Java包。
  • 步驟3 - 聲明一個公共類。
  • 步驟4 - 提到字符串參數(shù)。
  • 步驟5 - 創(chuàng)建并聲明兩個函數(shù)參數(shù)。
  • 步驟6 - 定義函數(shù)參數(shù)一。
  • 步驟7 - 定義函數(shù)參數(shù)二。
  • 步驟8 - 顯示兩個列表。
  • 步驟9 - 比較兩個列表。
  • 步驟10 - 如果評估結(jié)果為真,則打印相等的消息。
  • 步驟11 - 如果評估結(jié)果為假,則阻止執(zhí)行過程并打印不相等文本。
  • 步驟12 - 插入另一個元素并覆蓋該方法。
  • 步驟13 - 顯示兩者。
  • 步驟14 - 再次比較兩者。
  • 步驟15 - 獲取結(jié)果。
  • 步驟16 - 終止該過程。

執(zhí)行編譯時多態(tài)性的語法

class SimpleCalculator{
    int add(int a, int b){
        return a+b;
    }
    int add(int a, int b, int c){
        return a+b+c;
    }
}
public class DemoCal{
    SimpleCalculator obj = new SimpleCalculator();
    System.out.println(obj.add(10, 20));
    System.out.println(obj.add(10, 20, 30));
}
}
class SimpleCalculator{
    int add(int a, int b){
        return a+b;
    }
    int add(int a, int b, int c){
        return a+b+c;
    }
}
public class DemoCal{
    SimpleCalculator obj = new SimpleCalculator();
    System.out.println(obj.add(10, 20));
    System.out.println(obj.add(10, 20, 30));
}
}
class MethodOverloading {
    private static void display(int a){
        System.out.println("Got Int data as a value.");
    }
    private static void display(String a){
        System.out.println("Got String object as a value.");
    }
    public static void main(String[] args) {
        display(4);
        display("XYZ");
    }
}
class Student{
    public void stuIdentity(String name, int id){
        System.out.println("stuName :" + name + " "
        + "Id :" + id);
    }
    public void stuIdentity(int id, String name){
        System.out.println("Id :" + id + " " + "stuName :" + name);
    }
}
class Main {
    Student stu= new Student();
    stu.stuIdentity("Mohit Roy", 1);
    stu.stuIdentity(2, "Mohini Basu");
}
}

在上面的語法中,我們嘗試向您展示如何構(gòu)建一個函數(shù)以在多態(tài)性方法中使用它。通過使用這些Java語法,我們將朝著一些與編譯時多態(tài)性相關(guān)的Java方法前進。

遵循的方法

  • 方法1 - Java程序演示通過更改參數(shù)數(shù)量來演示編譯時多態(tài)性的方法重載的工作原理
  • 方法2 - Java程序使用render()類型方法進行編譯時多態(tài)性

方法1:使用數(shù)字參數(shù)執(zhí)行編譯時多態(tài)性

con_str方法的使用

在此方法中,我們將應(yīng)用con_str方法來演示通過更改參數(shù)數(shù)量來演示編譯時多態(tài)性的工作原理。

String con_str = s1 + s2;
System.out.println("Concatenated strings :"+ con_str);

示例

//Java程序演示通過更改參數(shù)數(shù)量來演示編譯時多態(tài)性的方法重載的工作原理
public class ARBRDD {
   void show(int num1){
      System.out.println("number 1 : " + num1);
   }
   void show(int num1, int num2){
      System.out.println("number 1 : " + num1 + " number 2 : " + num2);
   }
   public static void main(String[] args){
      ARBRDD obj = new ARBRDD();
      obj.show(3);
      obj.show(4, 5);
   }
}

輸出

<code>number 1 : 3
number 1 : 4 number 2 : 5</code>

數(shù)據(jù)類型方法的使用

在此方法中,我們將應(yīng)用數(shù)據(jù)類型模式方法來演示通過更改參數(shù)數(shù)量來演示編譯時多態(tài)性的工作原理。

示例

void ARBRDD() { ... }
void ARBRDD(int num1 ) { ... }
void ARBRDD(float num1) { ... }
void ARBRDD(int num1 , float num2 ) { ... }
//顯示(char a)的值
//顯示(char a, char b)的值
//顯示(float a, float b)的值
//顯示(int a, int b)的值
//顯示(int a, float b)的值
//顯示(float a, int b)的值
int sum value of (int, int);
String sum value of (int, int);

輸出

class SimpleCalculator{
    int add(int a, int b){
        return a+b;
    }
    int add(int a, int b, int c){
        return a+b+c;
    }
}
public class DemoCal{
    SimpleCalculator obj = new SimpleCalculator();
    System.out.println(obj.add(10, 20));
    System.out.println(obj.add(10, 20, 30));
}
}
class SimpleCalculator{
    int add(int a, int b){
        return a+b;
    }
    int add(int a, int b, int c){
        return a+b+c;
    }
}
public class DemoCal{
    SimpleCalculator obj = new SimpleCalculator();
    System.out.println(obj.add(10, 20));
    System.out.println(obj.add(10, 20, 30));
}
}
class MethodOverloading {
    private static void display(int a){
        System.out.println("Got Int data as a value.");
    }
    private static void display(String a){
        System.out.println("Got String object as a value.");
    }
    public static void main(String[] args) {
        display(4);
        display("XYZ");
    }
}
class Student{
    public void stuIdentity(String name, int id){
        System.out.println("stuName :" + name + " "
        + "Id :" + id);
    }
    public void stuIdentity(int id, String name){
        System.out.println("Id :" + id + " " + "stuName :" + name);
    }
}
class Main {
    Student stu= new Student();
    stu.stuIdentity("Mohit Roy", 1);
    stu.stuIdentity(2, "Mohini Basu");
}
}

序列參數(shù)方法的使用

在此方法中,我們將應(yīng)用序列參數(shù)方法來演示通過更改參數(shù)數(shù)量來演示編譯時多態(tài)性的工作原理。

示例

String con_str = s1 + s2;
System.out.println("Concatenated strings :"+ con_str);

輸出

//Java程序演示通過更改參數(shù)數(shù)量來演示編譯時多態(tài)性的方法重載的工作原理
public class ARBRDD {
   void show(int num1){
      System.out.println("number 1 : " + num1);
   }
   void show(int num1, int num2){
      System.out.println("number 1 : " + num1 + " number 2 : " + num2);
   }
   public static void main(String[] args){
      ARBRDD obj = new ARBRDD();
      obj.show(3);
      obj.show(4, 5);
   }
}

方法2:render()方法的使用

在此方法中,我們將應(yīng)用render方法來解釋使用編譯時多態(tài)性的運算符重載。

<code>number 1 : 3
number 1 : 4 number 2 : 5</code>

示例1

//Java程序演示通過更改參數(shù)的數(shù)據(jù)類型來演示方法重載的工作原理
public class ARBRDD {
   static void show(int a, int b){
      System.out.println("This is the integer function here");
   }
   static void show(double a, double b){
      System.out.println("This is the double function here");
   }
   public static void main(String[] args){
      show(1, 2);
      show(1.2, 2.4);
   }
}

輸出

<code>This is the integer function here
This is the double function here</code>

在此方法中,我們將應(yīng)用顯示信息方法來解釋使用編譯時多態(tài)性的運算符重載。

示例2

//Java程序演示通過更改參數(shù)的順序來演示方法重載的工作原理
public class ARBRDD {
   static void show(int a, char ch){
      System.out.println("integer : " + a + " and character : " + ch);
   }
   static void show(char ch, int a){
      System.out.println("character : " + ch + " and integer : " + a);
   }
   public static void main(String[] args){
      show(6, 'G');
      show('G', 7);
   }
}

輸出

<code>integer : 6 and character : G
character : G and integer : 7</code>

在此方法中,我們將應(yīng)用display()方法來解釋使用編譯時多態(tài)性的運算符重載。

示例3

String s1 = sc.next();
System.out.println("Enter another string: ");
String s2 = sc.next();
System.out.println(s1+' '+s2);
System.out.println("Enter a number:");
int x = sc.nextInt();
System.out.println("Enter another number:");
int y = sc.nextInt();

輸出

//Java程序使用render()方法進行編譯時多態(tài)性
class Polygon {
   public void render() {
      System.out.println("Rendering Polygon Value...");
   }
}
class Square extends Polygon {
   public void render() {
      System.out.println("Rendering Square Value...");
   }
}
class Circle extends Polygon {
   public void render() {
      System.out.println("Rendering Circle Value...");
   }
}
public class ARBRDD {
   public static void main(String[] args) {
      Square s1 = new Square();
      s1.render();
      Circle c1 = new Circle();
      c1.render();
   }
}

在此方法中,我們將應(yīng)用一些多態(tài)變量和方法來解釋使用編譯時多態(tài)性的運算符重載。

示例4

<code>Rendering Square Value...
Rendering Circle Value...</code>

輸出

//Java程序使用重寫方法進行編譯時多態(tài)性
class Language {
   public void displayInfo() {
      System.out.println("Common English Language");
   }
}
class Java extends Language {
   @Override
   public void displayInfo() {
      System.out.println("Java Programming Language");
   }
}
public class ARBRDD {
   public static void main(String[] args) {
      Java j1 = new Java();
      j1.displayInfo();
      Language l1 = new Language();
      l1.displayInfo();
   }
}

結(jié)論

編譯時多態(tài)性是一個早期綁定過程,通過它我們可以解決程序在執(zhí)行模式下出現(xiàn)的重載問題。在今天的文章中,我們學(xué)習(xí)了關(guān)于編譯時多態(tài)性的各種方法。通過使用算法和語法,我們還構(gòu)建了一些Java代碼,以有效的方式解釋問題陳述。

另請閱讀:Java面試問題和答案

The code examples have been improved for clarity and correctness, and the text has been rewritten to be more concise and engaging while maintaining the original meaning. The image remains in its original format and location.

以上是在Java中編譯時間多態(tài)性的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

如何在Java的地圖上迭代? 如何在Java的地圖上迭代? Jul 13, 2025 am 02:54 AM

遍歷Java中的Map有三種常用方法:1.使用entrySet同時獲取鍵和值,適用于大多數(shù)場景;2.使用keySet或values分別遍歷鍵或值;3.使用Java8的forEach簡化代碼結(jié)構(gòu)。entrySet返回包含所有鍵值對的Set集合,每次循環(huán)獲取Map.Entry對象,適合頻繁訪問鍵和值的情況;若只需鍵或值,可分別調(diào)用keySet()或values(),也可在遍歷鍵時通過map.get(key)獲取值;Java8中可通過Lambda表達式使用forEach((key,value)-&gt

Java中的可比較與比較器 Java中的可比較與比較器 Jul 13, 2025 am 02:31 AM

在Java中,Comparable用于類內(nèi)部定義默認(rèn)排序規(guī)則,Comparator用于外部靈活定義多種排序邏輯。1.Comparable是類自身實現(xiàn)的接口,通過重寫compareTo()方法定義自然順序,適用于類有固定、最常用的排序方式,如String或Integer。2.Comparator是外部定義的函數(shù)式接口,通過compare()方法實現(xiàn),適合同一類需要多種排序方式、無法修改類源碼或排序邏輯經(jīng)常變化的情況。兩者區(qū)別在于Comparable只能定義一種排序邏輯且需修改類本身,而Compar

如何處理Java中的字符編碼問題? 如何處理Java中的字符編碼問題? Jul 13, 2025 am 02:46 AM

處理Java中的字符編碼問題,關(guān)鍵是在每一步都明確指定使用的編碼。1.讀寫文本時始終指定編碼,使用InputStreamReader和OutputStreamWriter并傳入明確的字符集,避免依賴系統(tǒng)默認(rèn)編碼。2.在網(wǎng)絡(luò)邊界處理字符串時確保兩端一致,設(shè)置正確的Content-Type頭并用庫顯式指定編碼。3.謹(jǐn)慎使用String.getBytes()和newString(byte[]),應(yīng)始終手動指定StandardCharsets.UTF_8以避免平臺差異導(dǎo)致的數(shù)據(jù)損壞。總之,通過在每個階段

在C中使用std :: Chrono 在C中使用std :: Chrono Jul 15, 2025 am 01:30 AM

std::chrono在C 中用于處理時間,包括獲取當(dāng)前時間、測量執(zhí)行時間、操作時間點與持續(xù)時間及格式化解析時間。1.獲取當(dāng)前時間使用std::chrono::system_clock::now(),可轉(zhuǎn)換為可讀字符串但系統(tǒng)時鐘可能不單調(diào);2.測量執(zhí)行時間應(yīng)使用std::chrono::steady_clock以確保單調(diào)性,并通過duration_cast轉(zhuǎn)換為毫秒、秒等單位;3.時間點(time_point)和持續(xù)時間(duration)可相互操作,但需注意單位兼容性和時鐘紀(jì)元(epoch)

Hashmap在Java內(nèi)部如何工作? Hashmap在Java內(nèi)部如何工作? Jul 15, 2025 am 03:10 AM

HashMap在Java中通過哈希表實現(xiàn)鍵值對存儲,其核心在于快速定位數(shù)據(jù)位置。1.首先使用鍵的hashCode()方法生成哈希值,并通過位運算轉(zhuǎn)換為數(shù)組索引;2.不同對象可能產(chǎn)生相同哈希值,導(dǎo)致沖突,此時以鏈表形式掛載節(jié)點,JDK8后鏈表過長(默認(rèn)長度8)則轉(zhuǎn)為紅黑樹提升效率;3.使用自定義類作鍵時必須重寫equals()和hashCode()方法;4.HashMap動態(tài)擴容,當(dāng)元素數(shù)超過容量乘以負(fù)載因子(默認(rèn)0.75)時,擴容并重新哈希;5.HashMap非線程安全,多線程下應(yīng)使用Concu

JavaScript數(shù)據(jù)類型:原始與參考 JavaScript數(shù)據(jù)類型:原始與參考 Jul 13, 2025 am 02:43 AM

JavaScript的數(shù)據(jù)類型分為原始類型和引用類型。原始類型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時復(fù)制副本,因此互不影響;引用類型如對象、數(shù)組和函數(shù)存儲的是內(nèi)存地址,指向同一對象的變量會相互影響。判斷類型可用typeof和instanceof,但需注意typeofnull的歷史問題。理解這兩類差異有助于編寫更穩(wěn)定可靠的代碼。

Java中的'靜態(tài)”關(guān)鍵字是什么? Java中的'靜態(tài)”關(guān)鍵字是什么? Jul 13, 2025 am 02:51 AM

InJava,thestatickeywordmeansamemberbelongstotheclassitself,nottoinstances.Staticvariablesaresharedacrossallinstancesandaccessedwithoutobjectcreation,usefulforglobaltrackingorconstants.Staticmethodsoperateattheclasslevel,cannotaccessnon-staticmembers,

什么是Java的重新進入? 什么是Java的重新進入? Jul 13, 2025 am 02:14 AM

ReentrantLock在Java中提供比synchronized更靈活的線程控制。1.它支持非阻塞獲取鎖(tryLock())、帶超時的鎖獲?。╰ryLock(longtimeout,TimeUnitunit))和可中斷等待鎖;2.允許設(shè)置公平鎖,避免線程饑餓;3.支持多個條件變量,實現(xiàn)更精細(xì)的等待/通知機制;4.需手動釋放鎖,必須在finally塊中調(diào)用unlock()以避免資源泄漏;5.適用于需要高級同步控制的場景,如自定義同步工具或復(fù)雜并發(fā)結(jié)構(gòu),但對簡單互斥需求仍推薦使用synchro

See all articles