日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java电脑类的接口_java 一个类实现两个接口的案例

發布時間:2024/7/23 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java电脑类的接口_java 一个类实现两个接口的案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

直接用英文逗號分隔就可以了,比如:?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

inerface IHello {

String sayHello(String name);

}

interface IHi {

String sayHi(String name);

}

class ServiceImplimplements IHello, IHi {// 實現三個四個。。。n個接口都是使用逗號分隔

public String sayHello(String name) {

return "Hello, " + name;

}

public String sayHi(String name) {

return "Hi, " + name;

}

}

inerface IHello {

String sayHello(String name);

}

interface IHi {

String sayHi(String name);

}

class ServiceImpl implements IHello, IHi {// 實現三個四個。。。n個接口都是使用逗號分隔

public String sayHello(String name) {

return "Hello, " + name;

}

public String sayHi(String name) {

return "Hi, " + name;

}

}

補充知識:Java 一個類實現的多個接口,有相同簽名的default方法會怎么辦?

看代碼吧~?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public interface A {

default void hello() {

System.out.println("Hello from A");

}

}

public interface Bextends A {

default void hello() {

System.out.println("Hello from B");

}

}

public class Cimplements B, A {

public static void main(String... args) {

new C().hello();

}

}

public interface A {

default void hello() {

System.out.println("Hello from A");

}

}

public interface B extends A {

default void hello() {

System.out.println("Hello from B");

}

}

public class C implements B, A {

public static void main(String... args) {

new C().hello();

}

}

這段代碼,會打印什么呢?

有三條規則

類永遠贏。類聲明的方法,或者超類聲明的方法,比default方法的優先級高

否則,子接口贏

否則,如果集成自多個接口,必須明確選擇某接口的方法

上面代碼的UML圖如下

所以,上面的代碼,輸出是

Hello from B

如果這樣呢??

1

2

3

4

5

6

7

8

public class Dimplements A{

}

public class Cextends Dimplements B, A {

public static void main(String... args) {

new C().hello();

}

}

public class D implements A{

}

public class C extends D implements B, A {

public static void main(String... args) {

new C().hello();

}

}

UML圖是這樣的

規則1說,類聲明的方法優先級高,但是,D沒有覆蓋hello方法,它只是實現了接口A。所以,它的default方法來自接口A。規則2說,如果類和超類沒有方法,就是子接口贏。所以,程序打印的還是“Hello from B”。

所以,如果這樣修改代碼?

1

2

3

4

5

6

7

8

9

10

public class Dimplements A{

void hello(){

System.out.println("Hello from D");

}

}

public class Cextends Dimplements B, A {

public static void main(String... args) {

new C().hello();

}

}

public class D implements A{

void hello(){

System.out.println("Hello from D");

}

}

public class C extends D implements B, A {

public static void main(String... args) {

new C().hello();

}

}

程序的輸出就是“Hello from D”。

如果D這樣寫?

1

2

3

public abstract class Dimplements A {

public abstract void hello();

}

public abstract class D implements A {

public abstract void hello();

}

C就只能實現自己的抽象方法hello了。

如果是這樣的代碼呢?

1

2

3

4

5

6

7

8

9

10

11

12

13

public interface A {

default void hello() {

System.out.println("Hello from A");

}

}

public interface B {

default void hello() {

System.out.println("Hello from B");

}

}

public class Cimplements B, A {

}

public interface A {

default void hello() {

System.out.println("Hello from A");

}

}

public interface B {

default void hello() {

System.out.println("Hello from B");

}

}

public class C implements B, A {

}

UML圖如下

會生成這樣的編譯器錯誤

"Error: class C inherits unrelated defaults for hello() from types B and A."

怎么修改代碼呢?只能明確覆蓋某接口的方法?

1

2

3

4

5

public class Cimplements B, A {

void hello(){

B.super.hello();

}

}

public class C implements B, A {

void hello(){

B.super.hello();

}

}

如果代碼是這樣的,又會怎樣呢??

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public interface A{

default void hello(){

System.out.println("Hello from A");

}

}

public interface Bextends A {

}

public interface Cextends A {

}

public class Dimplements B, C {

public static void main(String... args) {

new D().hello();

}

}

public interface A{

default void hello(){

System.out.println("Hello from A");

}

}

public interface B extends A {

}

public interface C extends A {

}

public class D implements B, C {

public static void main(String... args) {

new D().hello();

}

}

UML圖是這樣的

很明顯,還是不能編譯。

以上這篇java 一個類實現兩個接口的案例就是小編分享給大家的全部內容了,

總結

以上是生活随笔為你收集整理的java电脑类的接口_java 一个类实现两个接口的案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。