複用代碼是 Java 衆多引人注目的功能之一。java
Java 能夠經過建立類來複用代碼,要在使用類的時候不破壞現有代碼,有兩種方式:app
toString()
方法,並且當編譯器須要一個 String 而你傳入一個對象時,toString()
會被調用。class Soap { private String s; Soap() { System.out.println("Soup()"); s = "Constructed"; } @Override public String toString() { return s; } } /** * Bath */ public class Bath { private String s1 = "happy", // 在定義處初始化 s2; private Soap soap; private int i; public Bath() { System.out.println("Inside Bath()"); soap = new Soap(); // 在構造函數中初四花 } @Override public String toString() { if (s2 == null) { s2 = "Joy"; // 惰性初始化 } return s2; } { i = 2; // 實例初始化 } public static void main(String[] args) { Bath b = new Bath(); System.out.println(b); } }
extends
指定,其形式如class Detergent extends Cleanser{}
,基類的全部方法和成員都會自動導入到導出類中。public main
也能夠經過 java className
的方式訪問到super
關鍵詞訪問基類版本的方法,如super.func()
;代理是指,咱們將一個成員對象置於要構造的類中(像組合),但與此同時咱們在新類中暴露該成員對象的全部或部分方法(想繼承)。dom
IDEA自動建立代理的過程:ide
Alt + Insert
快捷鍵,選中 Delegation
class SpaceShipControls { void up(int velocity) {} void down(int velocity) {} void left(int velocity) {} void right(int velocity) {} void back(int velocity) {} void turboBoost() {} } public class SpaceShipDelegation { SpaceShipControls spaceShipControls = new SpaceShipControls(); public void up(int velocity) { spaceShipControls.up(velocity); } public void down(int velocity) { spaceShipControls.down(velocity); } public void left(int velocity) { spaceShipControls.left(velocity); } public void right(int velocity) { spaceShipControls.right(velocity); } public void back(int velocity) { spaceShipControls.back(velocity); } public void turboBoost() { spaceShipControls.turboBoost(); } public static void main(String[] args) { SpaceShipDelegation spaceShipDelegation = new SpaceShipDelegation(); spaceShipDelegation.left(1); } }
@Override
註解來標識某個方法咱們但願其是重寫而不是重載,若是一不當心重載了,則會出現編譯錯誤來提醒咱們。class Instrument { public void play() {} static void tune(Instrument i) { i.play(); } } public class Wind extends Instrument { public static void main(String[] args) { Wind wind = new Wind(); Instrument.tune(wind); // 傳遞參數時,用了向上轉型 } }
final 關鍵詞的含義一般指「沒法改變的」,使用這個關鍵詞一般是由於設計和效率的緣由。,final 能夠用在數據、方法和類上。函數
final int value = 1
。一般,編譯時常量仍是一個static
數據,即static final int VALUE_ONE = 1
。_
隔開final
,咱們也沒法肯定其是編譯時常量,由於初始化沒有要求是字面量,即初始化能夠經過調用函數實現,如final int value = rand.nextInt(20)
。final
數值,若是其是static
的,那麼它多是在類導入時初始化的,而他不是static
的話,它是在實例化時初始化的。當將一個類的總體定義爲 final 時,就代表該類沒法被繼承,同時隱式地將全部方法都定義爲 final。單元測試
package com.company.ch07; class Insert { private int i = 9; protected int j; Insert() { System.out.println("i = " + i + " j = " + j); j = 39; } private static int x1 = printInit("static Insert.x1 init"); static int printInit(String s) { System.out.println(s); return 47; } } public class Beetle extends Insert { private int k = printInit("Beetle.k init"); public Beetle() { System.out.println("k = " + k); System.out.println("j = " + j); } private static int x2 = printInit("static Beetle.x2 init"); public static void main(String[] args) { System.out.println("Beetle constructor"); new Beetle(); } } // static Insert.x1 init // static Beetle.x2 init // Beetle constructor // i = 9 j = 0 // Beetle.k init // k = 47 // j = 39
class Demo { public Demo() { System.out.println("Demo"); } @Override public String toString() { return "toString()"; } } /** * Ex1 */ public class Ex1 { Demo demo; @Override public String toString() { if (demo == null) { demo = new Demo(); } return demo.toString(); } public static void main(String[] args) { Ex1 ex1 = new Ex1(); System.out.println(ex1); } }
class Cleanser { private String s = "Cleanser"; public void append(String a) { s += a; } public void dilute() { append(" dilute()"); } public void apply() { append(" apply()"); } public void scrub() { append(" scrub()"); } @Override public String toString() { return s; } public static void main(String[] args) { Cleanser cleanser = new Cleanser(); cleanser.dilute(); cleanser.apply(); cleanser.scrub(); System.out.println(cleanser); } } /** * Detergent */ public class Detergent extends Cleanser { @Override public void scrub() { append(" Detergent.scrub()"); super.scrub(); } public void foam() { append(" foam()");} public static void main(String[] args) { Detergent detergent = new Detergent(); detergent.dilute(); detergent.apply(); detergent.scrub(); detergent.foam(); System.out.println(detergent); Cleanser.main(args); } } class NewDetergent extends Detergent { public void scrub() { append("NewDetergent"); super.scrub(); } public void sterilize() { append("sterilize"); } public static void main(String[] args) { NewDetergent newDetergent = new NewDetergent(); newDetergent.dilute(); newDetergent.apply(); newDetergent.scrub(); newDetergent.foam(); newDetergent.sterilize(); System.out.println(newDetergent); Detergent.main(args); } } // Cleanser dilute() apply()NewDetergent Detergent.scrub() scrub() foam()sterilize // Cleanser dilute() apply() Detergent.scrub() scrub() foam() // Cleanser dilute() apply() scrub()
class Art { Art() { System.out.println("Art"); } } class Drawing extends Art { Drawing() { System.out.println("Drawing"); } } /** * Cartoon */ public class Cartoon extends Drawing{ // public Cartoon() { // System.out.println("Cartoon"); // } public static void main(String[] args) { new Cartoon(); } } // Art // Drawing
class A { A() { System.out.println("A"); } } class B { B() { System.out.println("B"); } } class C extends A { B b = new B(); public static void main(String[] args) { new C(); } } // A // B
class Game { Game(int i) { System.out.println("Game" + i); } } class BoardGame extends Game { BoardGame(int i) { super(i); System.out.println("BoardGame"); } } /** * Chess */ public class Chess extends BoardGame { Chess() { super(11); // 去掉這條語句,會報編譯錯誤 System.out.println("Chess"); } public static void main(String[] args) { new Chess(); } }
class A { A(int i) { System.out.println("A"); } } class B { B(int i) { System.out.println("B"); } } class C extends A { B b = new B(1); C() { super(2); } public static void main(String[] args) { new C(); } }
class Game { Game(int i) { System.out.println("Game" + i); } } class BoardGame extends Game { BoardGame() { super(1); System.out.println("BoardGame Default"); } BoardGame(int i) { super(i); System.out.println("BoardGame"); } }
class Component1 { Component1() { System.out.println("Component1"); } } class Component2 { Component2() { System.out.println("Component2"); } } class Component3 { Component3() { System.out.println("Component3"); } } class Root { Component1 c1 = new Component1(); Component2 c2 = new Component2(); Component3 c3 = new Component3(); Root() { System.out.println("Root"); } } class Stem extends Root { Stem() { System.out.println("Stem"); } public static void main(String[] args) { new Stem(); } } // Component1 // Component2 // Component3 // Root // Stem
class Component1 { Component1(int i) { System.out.println("Component1"); } } class Component2 { Component2(int i) { System.out.println("Component2"); } } class Component3 { Component3(int i) { System.out.println("Component3"); } } class Root { Component1 c1 = new Component1(1); Component2 c2 = new Component2(2); Component3 c3 = new Component3(3); Root(int i) { System.out.println("Root"); } } class Stem extends Root { Stem(int j) { super(j); System.out.println("Stem"); } public static void main(String[] args) { new Stem(2); } }
class DetergentDelegation { Detergent detergent = new Detergent(); public void append(String a) { detergent.append(a); } public void dilute() { detergent.dilute(); } public void apply() { detergent.apply(); } public void scrub() { detergent.scrub(); } public void foam() { detergent.foam(); } public static void main(String[] args) { Detergent.main(args); } }
package com.company.ch07; class Component1 { Component1(int i) { System.out.println("Component1"); } void dispose() { System.out.println("Component1 dispose"); } } class Component2 { Component2(int i) { System.out.println("Component2"); } void dispose() { System.out.println("Component2 dispose"); } } class Component3 { Component3(int i) { System.out.println("Component3"); } void dispose() { System.out.println("Component3 dispose"); } } class Root { Component1 c1 = new Component1(1); Component2 c2 = new Component2(2); Component3 c3 = new Component3(3); Root(int i) { System.out.println("Root"); } void dispose() { System.out.println("root dispose"); c1.dispose(); c2.dispose(); c3.dispose(); } } class Stem extends Root { Stem(int j) { super(j); System.out.println("Stem"); } void dispose() { System.out.println("Stem dispose"); super.dispose(); } public static void main(String[] args) { Stem stem = new Stem(2); try { // do something } finally { stem.dispose(); } } } // Component1 // Component2 // Component3 // Root // Stem // Stem dispose // root dispose // Component1 dispose // Component2 dispose // Component3 dispose
class Plate { Plate(int i) { System.out.println("Plate"); } void func(int i) { System.out.println("func int " + i); } void func(double d) { System.out.println("func double " + d); } void func(String s) { System.out.println("func string " + s); } } class DinnerPlate extends Plate { DinnerPlate(int i) { super(i); System.out.println("DinnerPlate"); } void func(char c) { System.out.println("func char " + c); } public static void main(String[] args) { DinnerPlate dinnerPlate = new DinnerPlate(1); dinnerPlate.func('c'); dinnerPlate.func("hello"); dinnerPlate.func(1); dinnerPlate.func(1.0); } } // Plate // DinnerPlate // func char c // func string hello // func int 1 // func double 1.0
package com.company.ch07; class Engine { public void start() {} public void rev() {} public void stop() {} void service() {} } class Wheel { public void inflate(int psi) {} } class Window { public void rollup() {} public void rolldown() {} } class Door { public Window window = new Window(); public void open() {} public void close() {} } public class Car { public Engine engine = new Engine(); public Wheel[] wheels = new Wheel[4]; public Door left = new Door(), right = new Door(); public Car() { for (int i = 0;i < 4; i++) { wheels[i] = new Wheel(); } } public static void main(String[] args) { Car car = new Car(); car.left.window.rollup(); car.right.window.rolldown(); car.wheels[0].inflate(72); car.engine.service(); } }
package com.company.ch05; public class Test { protected void func() {} }
package com.company.ch07; import com.company.ch05.*; public class Ex15 extends Test{ public static void main(String[] args) { Ex15 ex15 = new Ex15(); ex15.func(); } }
class Amphibian { void func() { } static void test(Amphibian amphibian) { amphibian.func(); } } public class Frog extends Amphibian { public static void main(String[] args) { Frog frog = new Frog(); Amphibian.test(frog); } }
class Amphibian { void func() { System.out.println("Amphibian func"); } static void test(Amphibian amphibian) { amphibian.func(); } } public class Frog extends Amphibian { @Override void func() { System.out.println("Frog func"); } public static void main(String[] args) { Frog frog = new Frog(); Amphibian.test(frog); } } // Frog func
public class Ex18 { static Random random = new Random(12); final int i = random.nextInt(12); static final int j = random.nextInt(12); public static void main(String[] args) { Ex18 ex18 = new Ex18(); System.out.println("ex18.i = " + ex18.i); System.out.println("ex18.j = " + ex18.j); Ex18 ex181 = new Ex18(); System.out.println("ex181.i = " + ex181.i); System.out.println("ex181.j = " + ex181.j); } } // ex18.i = 8 // ex18.j = 6 // ex181.i = 4 // ex181.j = 6
public class Ex19 { final int k; Ex19() { k = 1; // 必須賦值 // k = 2; // 會報錯 } public static void main(String[] args) { Ex19 ex19 = new Ex19(); // ex19.k = 1; // 會報錯 } }
package com.company.ch07; class WithFinal { private final void f() { System.out.println("WithFinal.f()"); } private void g() { System.out.println("WithFinal.g()"); } } class OverridingPrivate extends WithFinal { // @Override //加上註解後編譯錯誤 private final void f() { System.out.println("OverridingPrivate.f()"); } // @Override //加上註解後編譯錯誤 private void g() { System.out.println("OverridingPrivate.g()"); } } class OverridingPrivate2 extends OverridingPrivate { // @Override //加上註解後編譯錯誤 public final void f() { System.out.println("OverridingPrivate2.f()"); } // @Override //加上註解後編譯錯誤 public void g() { System.out.println("OverridingPrivate2.g()"); } } public class FinalOverridingIllusion extends OverridingPrivate2 { public static void main(String[] args) { OverridingPrivate2 overridingPrivate2 = new OverridingPrivate2(); overridingPrivate2.f(); overridingPrivate2.g(); OverridingPrivate overridingPrivate = overridingPrivate2; // overridingPrivate.f(); 沒法調用 // overridingPrivate.g(); WithFinal withFinal = overridingPrivate; // withFinal.f(); 沒法調用 // withFinal.g(); } }
package com.company.ch07; class Final { final void f() {} } public class Ex21 extends Final { void f() {} // 編譯出錯 }
package com.company.ch07; final class FinalClass { } public class Ex22 extends FinalClass { //編譯出錯 }
class Insert { private int i = 9; protected int j; Insert() { System.out.println("i = " + i + " j = " + j); j = 39; } private static int x1 = printInit("static Insert.x1 init"); static int printInit(String s) { System.out.println(s); return 47; } } public class Beetle extends Insert { private int k = printInit("Beetle.k init"); public Beetle() { System.out.println("k = " + k); System.out.println("j = " + j); } private static int x2 = printInit("static Beetle.x2 init"); public static int x3 = 3; public static void main(String[] args) { System.out.println("Beetle constructor"); new Beetle(); } } class Ex23 { public static void main(String[] args) { new Beetle(); // static Insert.x1 init // static Beetle.x2 init // i = 9 j = 0 // Beetle.k init // k = 47 // j = 39 // or // System.out.println(Beetle.x3); // static Insert.x1 init // static Beetle.x2 init // 3 } }
class Insert { private int i = 9; protected int j; Insert() { System.out.println("i = " + i + " j = " + j); j = 39; } private static int x1 = printInit("static Insert.x1 init"); static int printInit(String s) { System.out.println(s); return 47; } } public class Beetle extends Insert { private int k = printInit("Beetle.k init"); public Beetle() { System.out.println("k = " + k); System.out.println("j = " + j); } private static int x2 = printInit("static Beetle.x2 init"); public static int x3 = 3; public static void main(String[] args) { System.out.println("Beetle constructor"); new Beetle(); } } class Ex24 extends Beetle { public static void main(String[] args) { new Ex24(); // static Insert.x1 init // static Beetle.x2 init // i = 9 j = 0 // Beetle.k init // k = 47 // j = 39 } }
static Insert.x1 init
static Beetle.x2 init
new Ex24
時,實例化的順序爲 Insert -> Beetle -> Ex24
i = 9 j = 0
,之因此 j 爲0,是由於int默認值爲0private int k = printInit("Beetle.k init");