六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

Java編程思想讀書筆記(8章)

[摘要]第8章 接口與內(nèi)隱類    一. 接口    1. 如果實現(xiàn)接口的class未實現(xiàn)接口中的所有函數(shù),則這個class必須被聲明為abstractclass,而接口中未被實現(xiàn)的函數(shù)在這個class中為abstractclass。    interface Interface    public vo...
第8章 接口與內(nèi)隱類

   一. 接口

   1. 如果實現(xiàn)接口的class未實現(xiàn)接口中的所有函數(shù),則這個class必須被聲明為abstractclass,而接口中未被實現(xiàn)的函數(shù)在這個class中為abstractclass。

   interface Interface{
   public void f();
   public void g();
   }
   abstract class First implements Interface{
   public void f(){}
   }
   class Second extends First{
   public void g(){}
   }
   public class ExplicitStatic{
   public static void main(String[] args){
   Interface f = new Second();
   f.f();
   f.g();
   }
   }
   2. 接口中的所有函數(shù)自動具有public訪問權(quán)限,所以實現(xiàn)某個接口時,必須將承襲自該接口的所有函數(shù)都定義為public

   interface MyInterface {
   public void f();
   void g();
   }
   class First implements MyInterface {
   public void f(){}
   //!void g(){}出錯,應定義為public
   }
   3. 接口中的數(shù)據(jù)成員自動成為static和final

   interface MyInterface{
   int i = 5;
   void f();
   void g();
   }
   class First implements MyInterface {
   public void f(){}
   public void g(){}
   }
   public class ExplicitStatic{
   public static void main(String[] args){
   MyInterface x = new First();
   // MyInterface的數(shù)據(jù)成員I為static,可直接調(diào)用
   System.out.println("MyInterface.i = " + MyInterface.i + " , x.i = " + x.i);
   // MyInterface的數(shù)據(jù)成員I為final,不能修改
   //x.i++;
   // MyInterface.i++;
   }
   }
   4. 多重繼承

   1) devricedclass可以同時繼承多個interface和一個abstract或concretebaseclass。如果同時繼承了baseclass和interface,那么要先寫下具象類的名稱,然后才是interfaces的名稱。

   2) 如果derivedclass所繼承的具象類具有與interfaces相同的函數(shù),則可在derivedclass不實現(xiàn)那個函數(shù)。

   interface CanFight{
   void fight();
   }
   interface CanSwim{
   void swim();
   }
   class ActionCharacter{
   public void fight(){}
   }
   class Hero extends ActionCharacter
   implements CanFight, CanSwim{
   public void swim(){};
   }
   public class ExplicitStatic{
   static void f(CanFight x) { x.fight(); }
   static void s(CanSwim x) { x.swim(); }
   static void a(ActionCharacter x) { x.fight(); }
   static void h(Hero x){
   x.fight(); x.swim();
   }
   public static void main(String[] args){
   Hero h = new Hero();
   f(h); s(h); a(h); h(h);
   }
   }
   因為在ActionCharacterclass中有與接口CanFight完全相同的函數(shù)fight(),所以在Heroclass可以不實現(xiàn)fight()方法。當要調(diào)用x.fight()時,會調(diào)用ActionCharacterclass中的fight()函數(shù)。

   3) 接口的合并時的名稱沖突問題

   interface I1 { void f(); }
   interface I2 { int f(int i); }
   interface I3 { int f(); }
   class C { public int f() { return 1; } }
   class C2 implements I1, I2{
   public void f() {}
   public int f(int i) { return 1; }
   }
   class C3 extends C implements I2{
   public int f(int i) { return 1; }
   }
   class C4 extends C implements I3{
   public int f() { return 1; }
   }
   //class C5 extends C implements I1{}(a)

   //class C6 extends C implements I1{ public void f(){} }(b)

   interface I4 extends I1, I3{}//(c)

   class C7 implements I4{
   public void f() {}
   public int f() { return 1; }
   }
   (a)處代碼會產(chǎn)生以下錯誤: method f() in class C cannot implement method f() in interface I1 with different return type, was void。

   (b)處代碼也是錯誤的: method f() in class C6 cannot override method f() in class C with different return type, was int。由(b)處代碼也可看出,雖然你試圖實現(xiàn)接口I1中的函數(shù),但由于extends C在前,所以編譯器會把C6中的函數(shù)看成是覆寫classC中的函數(shù),而不是象你想象中的作為實現(xiàn)接口中的函數(shù)的函數(shù)。

   (c)處代碼在原書中(P253)說會出錯,但我在測試時并沒發(fā)生錯誤。但當你試圖通過C7來實現(xiàn)接口I4時,是無論如何也不可能編譯通過的。

   4) Java中唯一可以使用多重繼承的地方

   Java是不允許通過關(guān)鍵字extends來實現(xiàn)多重繼承的,但除了通過多重繼承來擴充接口除外。

   interface I1{
   void f1();
   }
   interface I2{
   void f2();
   }
   interface Ie1 extends I2{
   void fe1();
   }
   class Ce1 implements Ie1{
   public void f2() {}
   public void fe1() {}
   }
   interface Ie2 extends Ie1, I1{
   void fe2();
   }
   class Ce2 implements Ie2{
   public void fe2() {}
   public void f2() {}
   public void fe1() {}
   public void f1() {}
   }
   接口Ie2繼承了兩個接口。

   5. 嵌套的interfaces

   嵌套的interfaces可以在定義該內(nèi)部接口的外部類(接口)之外被使用(但內(nèi)隱類不行)。

   1) 當接口嵌套于class中

   a) 不論接口為public、friendly或private,都可被實現(xiàn)為public、friendly、private三種嵌套類。

   b) 被聲明為private的接口不能在class外被使用。

   class A{
   private interface B{
   void f();
   }
   public class BImp implements B{
   public void f() {}
   }
   private class BImp2 implements B{
   public void f() {}
   }
   public B getB() { return new BImp(); }
   private B dRef;
   public void recivedD(B d){
   dRef = d;
   dRef.f();;
   }
   }
   public class ExplicitStatic{
   public static void main(String[] args){
   A a = new A(); //(a)

   //A.B ab = a.getB();(b)

   //A.BImp = a.getB();(c)

   a.recivedD(a.getB());
   }
   }
   雖然Aclass含有接口,但它仍可被實例化,如(a)。

   由于接口B為private,所以在(b)處調(diào)用接口B時會出錯。但當把接口B聲明為public時,(b)將通過編譯。但(c)處依然會出錯,因為內(nèi)隱類的作用域為定義該內(nèi)隱類的外部類內(nèi)(見內(nèi)隱類)。

   2) 當接口嵌套于接口中

   1) 嵌套于接口中的接口自動為public,且只能為public。

   2) 當實現(xiàn)某個接口時,無需實現(xiàn)其中嵌套的接口。

   3) Private接口無法在其所定義的class之外被實現(xiàn)。



主站蜘蛛池模板: 欧美亚洲性色影视在线 | 亚洲福利视频一区二区三区 | 欧美一级成人 | 日韩 亚洲 翔田千里 在线 | 日日夜夜天天干干 | 一区二区三区久久 | 婷婷综合缴情亚洲狠狠图片 | 中文字幕视频免费在线观看 | 四虎在线免费播放 | 日韩经典欧美精品一区 | 欧美在线a级高清 | 四虎欧美永久在线精品免费 | 网友自拍区一区二区三区 | 五月天婷婷基地 | 欧美一级视频免费看 | 午夜免费体验 | 亚洲视频国产视频 | 日本a免费| 热热99| 日本激情网址 | 色噜噜狠狠色综合中国 | 日本中出视频 | 五级毛片| 日韩美女在线 | 色综合网天天综合色中文男男 | 亚洲经典自拍 | 欧美一区二区在线观看 | 呦呦国产| 在线播放国产一区二区三区 | 日韩一级免费毛片 | 中文字幕精品在线视频 | 天天躁日日躁狠狠躁一级毛片 | 日本在线视频网站www色下载 | 色老成人精品视频在线观看 | 欧美视频在线第一页 | 亚洲综合影视 | 青青草视频免费看 | 青娱乐极品视频在线观看 | 亚洲主播自拍 | 青青草免费线观 | 亚洲资源最新版在线观看 |