Вложенные классы и интерфейсы

  • Published on
    05-Apr-2017

  • View
    17

  • Download
    5

Embed Size (px)

Transcript

  • . unguryan@itstep.org

    *

  • - , .

    *

  • : .

    *

  • , (static nested classes). (inner classes).

    *

  • (nested)class OuterClass { ...static class NestedClass {...}}

    *

  • (inner)class OuterClass { ...class NestedClass {...}}

    *

  • . , private, public, protected package private.

    *

  • , private.

    *

  • , : () , . .

    *

  • : , A B, B A, . B A , B .

    *

  • : , .

    *

  • :OuterClass.StaticNestedClass :OuterClass.StaticNestedClass nestedObject =new OuterClass.StaticNestedClass();

    *

  • ( ) , . .

    *

  • . .

    *

  • .

    *

  • Outer outerObject = new Outer();Outer.Inner innerObject = outerObject.new Inner();

    *

  • publicclassOuterClass{publicvoidmethod(){...}publicclassInnerClass{publicvoidmethod(){...}publicvoidanotherMethod(){method();}}}

    *

  • (shadowing) ( ) - (, ) , .

    *

  • public class ShadowTest {public int x = 0;class FirstLevel {public int x = 1;void methodInFirstLevel(int x) {System.out.println("x = " + x);System.out.println("this.x = " + this.x);System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);}}

    *

  • public static void main(String... args) {ShadowTest st = new ShadowTest();ShadowTest.FirstLevel fl = st.new FirstLevel();fl.methodInFirstLevel(23);}}

    *

  • : .

    *

  • : , ; .

    *

  • , . private, public, protected static;

    *

  • .

    .

    *

  • , , . , . , . (GUI).

    *

  • new Thread(new Runnable() { public void run() { ... } }).start();

    *

  • :new . , . .

    *

  • . , , (final) (effectively final). , , , .

    *

  • . , . . .

    *

  • final final . . final abstract ( ),

    *

  • final final . , final, , .

    *

  • final final , , . final , ,

    *

  • . - . public, private protected.

    *

  • class A { interface B { void f(); } public class C implements B { public void f() {} } private class D implements B { public void f() {} }}

    *

  • . (nested) (public) (static) . , , API .

    *

  • interface Colorable { public Color getColor(); public static class Color { private int red, green, blue; Color(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; }

    *

  • int getRed() { return red; } int getGreen() { return green; } int getBlue() { return blue; } } } class Triangle implements Colorable { private Color color; // ... @Override public Color getColor() { return color; } }

    *

  • Colorable.Color color = new Colorable.Color(0, 0, 0); color = new Triangle.Color(255, 255, 255);

    *

  • *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

    *

Recommended

View more >