Java編程思想讀書筆記(8章)
發表時間:2024-01-20 來源:明輝站整理相關軟件相關文章人氣:
[摘要]第8章 接口與內隱類 一. 接口 1. 如果實現接口的class未實現接口中的所有函數,則這個class必須被聲明為abstractclass,而接口中未被實現的函數在這個class中為abstractclass。 interface Interface public vo...
第8章 接口與內隱類
一. 接口
1. 如果實現接口的class未實現接口中的所有函數,則這個class必須被聲明為abstractclass,而接口中未被實現的函數在這個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. 接口中的所有函數自動具有public訪問權限,所以實現某個接口時,必須將承襲自該接口的所有函數都定義為public
interface MyInterface {
public void f();
void g();
}
class First implements MyInterface {
public void f(){}
//!void g(){}出錯,應定義為public
}
3. 接口中的數據成員自動成為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的數據成員I為static,可直接調用
System.out.println("MyInterface.i = " + MyInterface.i + " , x.i = " + x.i);
// MyInterface的數據成員I為final,不能修改
//x.i++;
// MyInterface.i++;
}
}
4. 多重繼承
1) devricedclass可以同時繼承多個interface和一個abstract或concretebaseclass。如果同時繼承了baseclass和interface,那么要先寫下具象類的名稱,然后才是interfaces的名稱。
2) 如果derivedclass所繼承的具象類具有與interfaces相同的函數,則可在derivedclass不實現那個函數。
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完全相同的函數fight(),所以在Heroclass可以不實現fight()方法。當要調用x.fight()時,會調用ActionCharacterclass中的fight()函數。
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)處代碼會產生以下錯誤: 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)處代碼也可看出,雖然你試圖實現接口I1中的函數,但由于extends C在前,所以編譯器會把C6中的函數看成是覆寫classC中的函數,而不是象你想象中的作為實現接口中的函數的函數。
(c)處代碼在原書中(P253)說會出錯,但我在測試時并沒發生錯誤。但當你試圖通過C7來實現接口I4時,是無論如何也不可能編譯通過的。
4) Java中唯一可以使用多重繼承的地方
Java是不允許通過關鍵字extends來實現多重繼承的,但除了通過多重繼承來擴充接口除外。
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可以在定義該內部接口的外部類(接口)之外被使用(但內隱類不行)。
1) 當接口嵌套于class中
a) 不論接口為public、friendly或private,都可被實現為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)處調用接口B時會出錯。但當把接口B聲明為public時,(b)將通過編譯。但(c)處依然會出錯,因為內隱類的作用域為定義該內隱類的外部類內(見內隱類)。
2) 當接口嵌套于接口中
1) 嵌套于接口中的接口自動為public,且只能為public。
2) 當實現某個接口時,無需實現其中嵌套的接口。
3) Private接口無法在其所定義的class之外被實現。