Package, modifier, inner class, Anonymous inner class (Java Basics 10)

Source: Internet
Author: User
Tags modifiers

Overview and role of 1.package keywords

* A: Why should I have a bag
* Classify the byte code (. Class)
* B: Overview of the Package
*
* C: The role of the package
* Package name to be defined in the first line,
* Package name. * All classes under this package are visible
* Concrete analogy is better, can improve efficiency

2. Package Definition and Precautions

* A: Define the format of the package
* Package name;
* Multi-stage pack. Separate
* B: Considerations for defining packages
* The A:package statement must be the first executable code of the program
* B:package statements can only have one in a Java file
* C: If there is no package, default means no packages (previously defined classes do not have a package name)
* C: Case Demo
* Package Definition and Precautions

Overview and use of 3.import keywords

* A: Case Presentation
* Why should I have an import
* B: Guide Package format
* Import Package name (all-inclusive name);
Note
* This way the import is to the name of the class.
* Although can be written last *, but not recommended.
* C:package,import,class There is no sequential relationship (interview question)
* First line of package, import second, class third

4. Common modifiers used by the class and its composition

* A: modifier:
* Permission modifier: private, Default (Defalut), protected,public
* Status modifier: static,final
* Abstract modifier: Abstraction
* B: Class:
* Permission modifier: default modifier, public
* Status modifier: Final
* Abstract modifier: Abstraction
* The most used is: public
* C: Member Variable:
* Permission modifier: private, default, Protected,public
* Status modifier: static,final
* The most used is: private
* D: Construction Method:
* Permission modifier: private, default, Protected,public
* The most used is: public
* E: Member Method:
* Permission modifier: private, default, Protected,public
* Status modifier: static,final
* Abstract modifier: Abstraction
* The most used is: public
* F: In addition to the combination of rules:
* Member variable: public static final
* Member Method:
* public static
* Public abstract
* Public Final

5. Internal class overview and accessibility features

A: Internal class overview
*
* B: Internal class Access features
* A: The inner class can access the members of the external class directly, including the private.
* B: External class to access members of an inner class, you must create an object.

6. Internal class classification and direct use of members ' internal classes

* A: According to the internal class location classification
* Member Location: The class defined in the member location, called the member inner class.
* Local location: The class defined in the local location, called the local inner class.
* B: Member Inner class
* How to access the members of the inner class directly in the test class.
* External class name. Internal class Name Object name = External Class object. Inner class object;

7. Common modifiers and applications of members ' inner classes

* A: Modifier of the member's inner class:
* Private to ensure the security of the data
* Static for easy access to data
* Note: External class data accessed by static internal classes must be statically decorated.
* B: The member inner class is accessed by statically modifying the following methods:
* External class name. Internal class Name Object name = new External class name. Internal class name ();
* Static members cannot be defined in a non-static member inner class

8. The face question of the members ' inner class

Requirements: Use known variables in the console output 30,20,10. classOuter { Public intnum = 10; classInner { Public intnum = 20;  Public voidShow () {intnum = 30;                    SYSTEM.OUT.PRINTLN (num); System.out.println ( This. Num); System.out.println (Outer. This. Num); }            }        }        classInnerclasstest { Public Static voidMain (string[] args) {Outer.Inner Oi=NewOuter ().NewInner ();            Oi.show (); }            }

* Local internal classes access local variables, local variables must be final decorated.

9. The format and understanding of anonymous inner classes

* A: Anonymous inner class
* is a simplified notation of the inner class.
* B: Premise: There is a class or interface
* The class can be either a concrete class or an abstract class.
* C: Format:
*
New class name or interface name () {//Inherit this class or implement this class
Override method; overriding methods in a class or in an interface
}. Method name ();
* D: What is the essence?
* is an anonymous object that inherits the class or implements the interface's subclass.

10. Application of anonymous inner class in development

Write abstract classes Here, interfaces are OK
Abstract class Person {
public abstract void Show ();
}
Class Persondemo {
public void Method (person p) {
S.show ();
}
}
Class Persontest {
public static void Main (string[] args) {
How do I call method methods in Persondemo?
Persondemo PD = new Persondemo ();
Pd.method (new person () {
public void Show () {
System.out.println ("show");
}
});
}
}

11. Anonymous inner class of face question

according to the requirements, the CodeInterfaceInter {voidshow ();} classOuter { Public StaticInter Method () {return NewInter () { Public voidShow () {System.out.println ("Helloworld!");            }                }; }        }        classOuterdemo { Public Static voidMain (string[] args) {Outer.method (). Show (); //chained programming: The calling method returns an object, and you can continue to invoke the method}} requires output from the console "HelloWorld"

Package, modifier, inner class, Anonymous inner class (Java Basics 10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.