java 繼承示例_Java中的繼承類型以及示例

java 繼承示例

Prerequisite: Inheritance and its implementation in Java

先決條件: 繼承及其在Java中的實現

Java中的繼承類型 (Type of inheritance in Java)

In Java programming, there are following types of the inheritances,

在Java編程中,有以下幾種類型的繼承

  1. Single Inheritance

    單繼承

  2. Multiple Inheritances (Through Interface)

    多重繼承(通過接口)

  3. Multilevel Inheritance

    多級繼承

  4. Hierarchical Inheritance

    層次繼承

1)單一繼承 (1) Single Inheritance)

If a class extends another class (i.e. the only one class).

如果一個類擴展了另一個類(即唯一一個類)。

Syntax:

句法:

    class Parent {
// Fields and Methods
}
class Child extends Parent {
// Fields and Methods
}

Example of Single Inheritance:

單一繼承的示例:

/*Java program to demonstrate the  
concept of inheritance */
// Parent class 
class Parent {
// The Parent class has one method
// displayParentMessage() method to print message of Parent Class
public void displayParentMessage() {
System.out.println("Hello, we are in parent class method");
}
}
// Sub class or derived class
class Child extends Parent {
// The Child subclass adds one more method
// displayChildMessage() method to print message of Parent Class
public void displayChildMessage() {
System.out.println("Hello, we are in child class method");
}
}
// Main class in this class we will create 
//object of parent and child class 
class Main {
public static void main(String[] args) {
// Creation of Parent class object
Parent p = new Parent();
// Calling Parent class method by Parent class object
p.displayParentMessage();
// Creation of Child class object
Child c = new Child();
// Calling Child class method by Child class object
c.displayChildMessage();
// Calling Parent class method by Child class object
c.displayParentMessage();
}
}

Output

輸出量

D:\Programs>javac Main.java
D:\Programs>java Main
Hello, we are in parent class method
Hello, we are in child class method
Hello, we are in parent class method

2)多重繼承(通過接口) (2) Multiple Inheritance (Through Interface))

If we extend more than one class. Java doesn’t support multiple inheritances directly but with the help of interface we can implement but it is similar to multiple inheritance.

如果我們擴展多個類。 Java不直接支持多重繼承,但是借助我們可以實現的接口的幫助,它類似于多重繼承。

Syntax:

句法:

    interface interface1 {
// Field and Method declaration
}
interface interface2 {
// Field and Method declaration
}
Class class_name implements interface1, interface2 {}

Example of Multiple Inheritance:

多重繼承的例子:

/*Java program to demonstrate the  
concept of multiple inheritance */
interface Print {
void print();
}
interface Show {
void show();
}
class Main implements Print, Show {
public void print() {
System.out.println("Hello");
}
public void show() {
System.out.println("World");
}
public static void main(String args[]) {
Main obj = new Main();
obj.print();
obj.show();
}
}

Output

輸出量

D:\Programs>javac Main.java
D:\Programs>java Main
Hello
World

3)多級繼承 (3) Multilevel Inheritance)

If a classA extends by classB and classB extends by classC is called multilevel inheritance.

如果將classA擴展為classB ,將classB擴展為classC,則稱為多級繼承。

Syntax:

句法:

    class classA {
// Fields and Methods
}
class classB extends classA {
// Fields and Methods
}
class classC extends classB {
// Fields and Methods
}

Example of Multilevel Inheritance:

多級繼承的示例:

/*Java program to demonstrate the  
concept of multilevel inheritance */
// ClassA class 
class ClassA {
// The ClassA class has one method
// displayClassAMessage() method to print message of ClassA Class
public void displayClassAMessage() {
System.out.println("Hello, we are in ClassA class method");
}
}
// ClassB class
class ClassB extends ClassA {
// The ClassB class adds one more method
// displayClassBMessage() method to print message of ClassB Class
public void displayClassBMessage() {
System.out.println("Hello, we are in ClassB class method");
}
}
// ClassC class
class ClassC extends ClassB {
// The ClassC class adds one more method
// displayClassCMessage() method to print message of ClassC Class
public void displayClassCMessage() {
System.out.println("Hello, we are in ClassC class method");
}
}
// Main class in this class we will create 
//object of ClassA and ClassB and ClassC class 
class Main {
public static void main(String[] args) {
// Creation of ClassA class object
ClassA ca = new ClassA();
// Calling ClassA class method by ClassA class object
ca.displayClassAMessage();
// Creation of ClassB class object
ClassB cb = new ClassB();
// Calling ClassB class method by ClassB class object
cb.displayClassBMessage();
// Calling ClassA class method by ClassB class object
cb.displayClassAMessage();
// Creation of ClassC class object
ClassC cc = new ClassC();
// Calling ClassC class method by ClassC class object
cc.displayClassCMessage();
// Calling ClassB class method by ClassC class object
cc.displayClassBMessage();
// Calling ClassA class method by ClassC class object
cc.displayClassAMessage();
}
}

Output

輸出量

D:\Programs>javac Main.java
D:\Programs>java Main
Hello, we are in ClassA class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method
Hello, we are in ClassC class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method

4)層次繼承 (4) Hierarchical Inheritance)

If more than one class is inherited from the base class is called hierarchical inheritance.

如果從基類繼承多個類,則稱為層次繼承。

Syntax:

句法:

    class classA {
// Fields and Methods
}
class classB extends classA {
// Fields and Methods
}
class classC extends classA {
// Fields and Methods
}

Example of Hierarchical Inheritance:

層次繼承的示例:

/*Java program to demonstrate the  
concept of hierarchical inheritance */
//ClassA
class ClassA {
// The ClassA class has one method
// displayClassAMessage() method to print message of ClassA Class
public void displayClassAMessage() {
System.out.println("Hello, we are in ClassA class method");
}
}
// ClassB class
class ClassB extends ClassA {
// The ClassB class adds one more method
// displayClassBMessage() method to print message of ClassB Class
public void displayClassBMessage() {
System.out.println("Hello, we are in ClassB class method");
}
}
// ClassC class
class ClassC extends ClassA {
// The ClassC class adds one more method
// displayClassCMessage() method to print message of ClassC Class
public void displayClassCMessage() {
System.out.println("Hello, we are in ClassC class method");
}
}
// Main class in this class we will create 
//object of ClassA and ClassB and ClassC class 
class Main {
public static void main(String[] args) {
// Creation of ClassA class object
ClassA ca = new ClassA();
// Calling ClassA class method by ClassA class object
ca.displayClassAMessage();
// Creation of ClassB class object
ClassB cb = new ClassB();
// Calling ClassB class method by ClassB class object
cb.displayClassBMessage();
// Calling ClassA class method by ClassB class object
cb.displayClassAMessage();
// Creation of ClassC class object
ClassC cc = new ClassC();
// Calling ClassC class method by ClassC class object
cc.displayClassCMessage();
// Calling ClassA class method by ClassC class object
cc.displayClassAMessage();
}
}

Output

輸出量

D:\Programs>javac Main1.java
D:\Programs>java Main1
Hello, we are in ClassA class method
Hello, we are in ClassB class method
Hello, we are in ClassA class method
Hello, we are in ClassC class method
Hello, we are in ClassA class method

翻譯自: https://www.includehelp.com/java/types-of-inheritance-in-java-with-examples.aspx

java 繼承示例

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378909.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378909.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378909.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于HtmlParser的網絡爬蟲

一、 目標 獲取網頁中的超鏈接及鏈接名,如從http://www.hao123.com/開始,抓取所有hao123鏈接到的超鏈接,再以獲取到的鏈接網頁為目標,獲取它所鏈接到的網頁。 二、環境及開發工具 環境:Java 工具:MyEclip…

VMware下Ubuntu無法全屏顯示問題

一、運行Ubuntu的時候無法全屏顯示,如圖所示下載VMware Tools 二、之后將下載的文件拷貝到home文件夾下 三、解壓該壓縮包 由于該壓縮包是.tar.gz結尾的故壓縮命令:tar -zxvf VMwareTools-10.2.5-8068393.tar.gz,當然各版本有可能不一樣&am…

AMQP RabbitMQ

轉載:http://blog.ftofficer.com/2010/03/translation-rabbitmq-python-rabbits-and-warrens/官方介紹:http://www.rabbitmq.com/erlang-client-user-guide.html開始吧AMQP當中有四個概念非常重要:虛擬主機(virtual host&#xff…

fsync與fflush的關系和區別

read/write/fsync與fread/fwrite/fflush的關系和區別 read/write/fsync: linux底層操作; 內核調用, 涉及到進程上下文的切換,即用戶態到核心態的轉換,這是個比較消耗性能的操作。 fread/fwrite/fflush:…

lumanager mysql密碼_LuManager單獨安裝mysqli

首先確定你正在使用的php版本以及php.ini的位置,LuManager自帶了幾個版本。如果是默認安裝,應該是5.2.17。php.ini的位置應該是在/usr/local/php_fcgi/lib/php.ini要確定這些信息,可以自己編寫一個 info.phpphpinfo();?>把文件存放到網站…

數據庫系統數據庫管理系統_數據庫管理系統介紹

數據庫系統數據庫管理系統數據庫 (Database) A database is a collection of related data. In database any user can efficiently access the data which users want to retrieve. It can be anything from a simple collection of roll numbers, names, addresses and phone…

vba將select的值直接賦給變量

strSql ""strSql strSql & " select max(number) from dbo.#DATA" & vbCrLfrss.Open strSql, cnn numb rss.Fields(0)rss.Close轉載于:https://www.cnblogs.com/zigewb/archive/2013/02/06/2900645.html

set_exception_handler 自定義異常處理

剛才已經說過了set_error_handler這個函數,作用就是自定義錯誤處理, 那么現在就來簡單的說一下set_exception_handler,看名字我們就能發現,這說的是自定義異常處理。 呵呵,我聰明吧?來,先看一下…

如何獲取ubuntu源碼包里面的源碼進行編譯

如何獲取ubuntu源碼包里面的源碼進行編譯 1、在獲取源碼包之前,確保在軟件源配置文件 /etc/apt/sources.list中添加了deb-src項 2、使用如下命令獲取xxx源碼包的詳細信息: sudo apt-cache showsrc xxx 這用來查詢當前鏡像站點中是否有該源碼包。 3、源碼包中通常…

python 示例_帶有示例的Python字典popitem()方法

python 示例字典popitem()方法 (Dictionary popitem() Method) popitem() method is used to remove random/last inserted item from the dictionary. popitem()方法用于從字典中刪除隨機/最后插入的項目。 Before the Python version 3.7, it removes random item and from …

優化算法的意義,之二。

前一篇分析了求質數的兩個算法,在代碼執行效率和系統開銷兩方面進行了比較。 這在通信系統的設計和實現中,是非常重要的兩點。因為需要同時面對的是巨大的用戶群,和復雜的業務應用,通信系統的設計經常要面臨魚與熊掌間的選擇。 用…

srs配置文件分析

配置文件中的每一項都是一個SrsConfDirective對象。 例子:vhost 1、 整個vhost 是一個SrsConfDirective對象。 1.1、名字:std::string name vhost 1.2、參數:std::vectorstd::string args第0個值 defaultVhost 1.3、子SrsConfDirective&a…

寄存器(CPU工作原理)03 - 零基礎入門學習匯編語言08

第二章:寄存器(CPU工作原理)03 讓編程改變世界 Change the world by program 物理地址 CPU訪問內存單元時要給出內存單元的地址。所有的內存單元構成的存儲空間是一個一維的線性空間。 我們將這個唯一的地址稱為物理地址。 16位結構的CPU…

判別Linux是CentOs還是Ubuntu的最簡單方法

在終端執行以下兩條命令即可 CentOs:yum -help Ubuntu:apt-get -help

threadgroup_Java ThreadGroup toString()方法與示例

threadgroupThreadGroup類的toString()方法 (ThreadGroup Class toString() method) toString() method is available in java.lang package. toString()方法在java.lang包中可用。 toString() method is used to returns string denotation of this thread group (i.e. this m…

240多個jQuery插件

文件上傳(File upload)Ajax File Upload.jQUploader.Multiple File Upload plugin. jQuery File Style.Styling an input type file.Progress Bar Plugin.表單驗證(Form Validation)jQuery Validation.Auto Help.Simple jQuery form validation.jQuery XAV - form validations…

解壓縮命令

.Tar.gz 解壓:Tar zxvf FileName.Tar.gz 壓縮:Tar zcvf FileName.Tar.gz DirName 大致總結了一下Linux下各種格式的壓縮包的壓縮、解壓方法。但是部分方法我沒有用到,也就不全,希望大家幫我補充,我將隨時修改完善&…

Anaconda下安裝OpenCV和Tensorflow(最簡潔高效的方法)

安裝Tensorflow 1,打開Anaconda Navigator 2,手動創建tensorflow環境,這個和你的python版本號一致哈(方法一第一步之后,輸入python即可查看當前的版本) 3,手動搜索并下載添加 4,…

Java System類console()方法及示例

系統類console()方法 (System class console() method) console() method is available in java.lang package. console()方法在java.lang包中可用。 console() method is used to return the console object which is uniquely associated with the current JVM(Java Virtual …