大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java中的接口和繼承

Java中的接口和繼承

更新時間:2022-10-14 10:35:12 來源:動力節點 瀏覽1585次

相信大家對Java繼承定義已經有所了解,先決條件: Java、Java 和多重繼承中的接口一個類可以擴展另一個類和/可以實現一個和多個接口。

例子:

// Java program to demonstrate that a class can
// implement multiple interfaces
import java.io.*;
interface intfA
{
	void m1();
}
interface intfB
{
	void m2();
}
// class implements both interfaces
// and provides implementation to the method.
class sample implements intfA, intfB
{
	@Override
	public void m1()
	{
		System.out.println("Welcome: inside the method m1");
	}
	@Override
	public void m2()
	{
		System.out.println("Welcome: inside the method m2");
	}
}
class GFG
{
	public static void main (String[] args)
	{
		sample ob1 = new sample();

		// calling the method implemented
		// within the class.
		ob1.m1();
		ob1.m2();
	}
}

輸出;

歡迎:在方法m1里面
歡迎:在方法m2里面

繼承就是將父類的屬性繼承到子類中。

Java 中的繼承是一種機制,其中一個對象獲取父對象的所有屬性和行為。

Java 繼承背后的理念是,您可以創建基于現有類的新類。從現有類繼承時,可以重用父類的方法和字段。

您還可以在當前類中添加新方法和字段。

繼承表示 IS_A 關系,也稱為父子關系。

例如:

狗 IS_A 動物

車 IS_A 車輛

員工 IS_A 人

外科醫生 IS_A 醫生等。

class Animal
{
	public void eat()
	{
	}
}
Class Dog extends Animal
{
	Public static void main(String args[])
	{
		Dog d=new Dog;
		d.eat();
	}
}

Java繼承的語法

類 <子類名稱> 擴展 <超類名稱>
{
    //方法和字段
}

注意:extends 關鍵字表示您正在創建一個從現有類派生的新類。“擴展”的意思是增加功能。

例如_1:

import java.io.*;
class Person {
int id;
String name;
void set_Person()
{
try{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Id:");
id=Integer.parseInt(br.readLine());
System.out.println("Enter the Name");
name=br.readLine();
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Person()
{
System.out.print(id+"\t"+name+"\t");
}
}
class Employee extends Person{
int sal;
String desgn;
void set_Emp()
{
try{
set_Person();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Designation:");
desgn=br.readLine();
System.out.println("Enter the Salary:");
sal=Integer.parseInt(br.readLine());
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Emp()
{
disp_Person();
System.out.println(desgn+"\t"+sal);
}
public static void main(String args[])
{
Employee e1=new Employee();
e1.set_Emp();
e1.disp_Emp();
}
}

例如_2:

class Person1 {
int id;
String name;
void set_Person(int id,String name)
{
try{
this.id=id;
this.name=name;
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Person()
{
System.out.print(id+"\t"+name+"\t");
}
}
class Employee1 extends Person1 {
int sal;
String desgn;
void set_Emp(int id,String name,String desgn,int sal)
{
try{
set_Person(id,name);
this.desgn=desgn;
this.sal=sal;
}catch(Exception ex){ex.printStackTrace();}
}
void disp_Emp()
{
disp_Person();
System.out.print(desgn+"\t"+sal);
}
public static void main(String args[])
{
Employee1 e1=new Employee1();
e1.set_Emp(1001,"Manjeet","AP",20000);
e1.disp_Emp();
}
}

java中的繼承類型

Java 在java中支持三種類型的繼承:單級、多級和在類的情況下的分層繼承,以避免歧義。

在 Java 編程中,僅通過接口支持多重繼承和混合繼承。

單繼承示例

當一個類繼承另一個類時,稱??為單繼承。

class A
{
int a;
void set_A(int x)
{
a=x;
}
}
class B extends A{
int b,product;
void set_B(int x)
{
b=x;
}
void cal_Product()
{
product=a*b;
System.out.println("Product ="+product);
}
public static void main(String[] args) {
B b=new B();
b.set_A(5);
b.set_B(5);
b.cal_Product();
}
}

多級繼承示例

當存在繼承鏈時,稱為多級繼承。

class A
{
int a;
void set_A(int x)
{
a=x;
}
}
class B extends A{
int b;
void set_B(int x)
{
b=x;
}
}
class C extends B{
int c,product;
void cal_Product()
{
product=a*b;
System.out.println("Product ="+product);
}
public static void main(String[] args) {
C c=new C();
c.set_A(5);
c.set_B(5);
c.cal_Product();
}
}

層次繼承示例

當兩個或多個類繼承一個類時,稱??為層次繼承。

例如:

class A
{
int a;
void set_A(int x)
{
a=x;
}
}
class B extends A{
int b;
void set_B(int x)
{
b=x;
}
}
class C extends A{
int c;
void set_C(int x)
{
c=x;
}
// Java program to demonstrate inheritance in
// interfaces.
import java.io.*;
interface intfA {
	void geekName();
}
interface intfB extends intfA {
	void geekInstitute();
}
// class implements both interfaces and provides
// implementation to the method.
class sample implements intfB {
	@Override public void geekName()
	{
		System.out.println("Rohit");
	}
	@Override public void geekInstitute()
	{
		System.out.println("JIIT");
	}
	public static void main(String[] args)
	{
		sample ob1 = new sample();
		// calling the method implemented
		// within the class.
		ob1.geekName();
		ob1.geekInstitute();
	}
}

輸出:

羅希特
JIIT

一個接口也可以擴展多個接口。

// Java program to demonstrate multiple inheritance
// in interfaces
import java.io.*;
interface intfA {
	void geekName();
}
interface intfB {
	void geekInstitute();
}
interface intfC extends intfA, intfB {
	void geekBranch();
}
// class implements both interfaces and provides
// implementation to the method.
class sample implements intfC {
	public void geekName() { System.out.println("Rohit"); }
	public void geekInstitute()
	{
		System.out.println("JIIT");
	}
	public void geekBranch() { System.out.println("CSE"); }
	public static void main(String[] args)
	{
		sample ob1 = new sample();
		// calling the method implemented
		// within the class.
		ob1.geekName();
		ob1.geekInstitute();
		ob1.geekBranch();
	}
}

輸出

羅希特
JIIT
CSE

為什么Java中的類不支持多重繼承,但通過接口可以實現?由于歧義,類不支持多重繼承。在接口的情況下,沒有歧義,因為方法的實現是由 Java 7 之前的實現類提供的。從 Java 8 開始,接口也有方法的實現。因此,如果一個類實現了兩個或多個具有相同方法簽名的接口,那么它也必須在類中實現該方法。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 成熟热自由日本语亚洲人 | 国产精品久久久久久久久久久久 | 日本一级~片免费永久 | 四虎影视在线看免费观看 | 欧美乱大交xxxxxbbb | 国产日韩欧美综合一区 | 国产男女猛视频在线观看网站 | 97在线免费视频观看 | 国产21区 | 国产精品中文字幕在线 | 欧美性xxx久久 | 亚洲乱码中文字幕 | 亚洲欧美另类视频 | 日本一级毛片不卡免费 | 欧美成人免费观看国产 | 国产精品久久天天影视 | 最新国产精品好看的国产精品 | 四影虎库最新2021 | 黄动漫在线无限看免费 | 老司机午夜在线视频免费 | 欧美日本中文 | 97se综合| 男人手机天堂 | 中文字幕精品一区二区三区在线 | 久久欧美精品欧美九久欧美 | 嘿咻嘿咻免费区在线观看吃奶 | 久久a热6 | 亚洲成人黄色在线 | 亚洲一区二区精品推荐 | 久久网精品视频 | 人人看人人鲁狠狠高清 | 香蕉午夜 | 中文字幕日韩精品亚洲七区 | 亚洲最大成人 | 国产精品深爱在线 | 欧美日本一道免费一区三区 | 伊人久久五月天 | 亚洲国产成人综合 | 在线观看黄色免费视频 | 立即播放免费毛片一级 | 国产一级片毛片 |