Fundamentals of computing 1 - Lecture title: Inner class

Anonymous inner classes (anonymous classes) are probably the strangest feature of the Java programming language. An anonymous class is a class that’s defined on the spot, right at the point where you want to instantiate it. Because you code the body of the class right where you need it, you don’t have to give it a name. That’s why it’s called an anonymous class.

ppt15 trang | Chia sẻ: nguyenlam99 | Lượt xem: 885 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Fundamentals of computing 1 - Lecture title: Inner class, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lecture Title: Inner classFundamentals of Computing 1AgendaInner classStatic inner classAnonymous Inner ClassAn inner class is a class declared inside another classBasic structure for creating an inner class:class outerClassName { private class innerClassName { // body of inner class }} Inner classVisibility of inner classpublic, protected, or privateThese visibilities determine whether other classes can see the inner class. Understanding inner classes An inner class automatically has access to all the fields and methods of the outer classAn inner class carries with it a reference to the current instance of the outer class that enables it to access instance data of the outer class. Understanding inner classes (con’t)Because of the outer class instance reference, you can’t create or refer to an inner class from a static method of the outer class. One of the main reasons for creating an inner class is to create a class that’s only of interest to the outer class. As a result, you usually declare inner classes to be private so other classes can’t access them. Understanding inner classes (con’t)Occasionally, code in an inner class needs to refer to the instance of its outer class. To do that, you list the name of the outer class followed by the dot operator and this. Example: if the outer class is named MyOuterClass, you would use MyOuterClass.this inside the inner class to refer to the instance of the outer class.AgendaInner classStatic inner classAnonymous Inner ClassStatic inner classA static inner class is similar to an inner class, but doesn’t require an instance of the outer class. Basic form is the following: class outerClassName { private static class innerClassName { // body of inner class}Static inner class (cont’d)Like a static method, a static inner class can’t access any non-static fields ormethods in its outer class. It can, however, access static fields or methods.AgendaInner classStatic inner classAnonymous inner ClassAnonymous Inner ClassAnonymous inner classes (anonymous classes) are probably the strangest feature of the Java programming language. An anonymous class is a class that’s defined on the spot, right at the point where you want to instantiate it. Because you code the body of the class right where you need it, you don’t have to give it a name. That’s why it’s called an anonymous class. Creating an anonymous class The basic form for declaring and instantiating an anonymous class is this: new ClassOrInterface() { class-body } Example of anonymous class Understanding Anonymous Class

Các file đính kèm theo tài liệu này:

  • pptlecture_14_inner_class_1794.ppt
Tài liệu liên quan