10_ Object-oriented (classes, abstract classes, integrated exercises of interfaces, formal parameters and return values, overview and explanation of packages, modifiers, inner classes)

Source: Internet
Author: User

1: Problems with formal parameters and return values (understanding)
(1) Formal parameters:
Class Name: An object that requires this class
Abstract class Name: A subclass object that requires this class
Interface Name: An implementation class object that requires this interface
(2) Return value type:
Class Name: Returns the object of the class
Abstract class Name: Returns the subclass object of the class
Interface Name: An object that returns an implementation class for the interface
(3) Chain-type programming
Object. Method 1 (). Method 2 () .... Method N ();

This usage: In fact, after the invocation of Method 1 (), an object should be used;
After Method 2 () is called, an object should be returned.
After the method n () call is complete, the object may or might not be an object.

2: Package (understanding)
(1) is actually a folder
(2) Function:
A: Distinguish between classes of the same name
B: Classification Management of classes
A: According to the function of
B: According to the module sub-
(3) Definition of package (master)
Package name;

Multi-stage packet. Separate.
(4) Precautions: (Master)
The A:package statement must be the first valid statement in the file
B: In a Java file, there can be only one package
C: If there is no package, the default is the No bag name
(5) Compile and run with package
A: Manual Type
B: Automatic (Master)
Javac-d. Helloworld.java

3: Guide Pack (Master)
(1) We use a class with a package many times, very troublesome, this time, Java provides a keyword import.
(2) Format:
Import Package Name ... Class name;
Another type of:
Import Package Name ... *;(not recommended)
(3) Order of Package,import,class
Package > Import > Class

4: Permission modifier (master)
(1) Permission modifier
This is similar to a package under different sub-classes under different packages of unrelated classes
Private Y
Default Y y
Protected Y y
Public y-y y y
(2) These four permission modifiers can only appear at any one time.
public class Demo {}

5: Common modifiers (understanding)
(1) Classification:
Permission modifier: Private, default, Protected,public
Status modifier: static,final
Abstraction modifier: Abstract
(2) Modification of common classes and their compositions
Class:
Default, Public,final,abstract

Common: Public

Member variables:
Private, default, protected,public,static,final

Common: Private

Construction Method:
Private, default, Protected,public

Common: Public

Member Methods:
Private, default, Protected,public,static,final,abstract

Common: Public
(3) The other more common:
public static final int X = 10;
public static void Show () {}
Public final void Show () {}
public abstract void Show ();

6: Inner Class (understanding)
(1) The class is defined inside another class, which is called an inner class.
Example: Class B is defined in Class A, and Class B is called an inner class.
(2) Access rules for internal classes
A: Members of external classes can be accessed directly, including private
B: External class to access internal class members, you must create an object
(3) Classification of internal classes
A: Member Inner class
B: Local inner class
(4) member inner class
A:private for the security of the data
B:static for ease of access

The member inner class is not static:
The external class name. Internal class Name Object name = new External class name. New internal class name ();
The member inner class is static:
The external class name. Internal class Name Object name = new External class name. Internal class name ();
(5) Questions of inner class of members (fill in the blanks)
30,20,10

Class Outer {
public int num = 10;

Class Inner {
public int num = 20;

Public Viod Show () {
int num = 30;

SYSTEM.OUT.PRINTLN (num);
System.out.println (This.num);
System.out.println (Outer.this.num);
}
}
}
(6) Local inner class
A: Local internal class access local variables must be final decorated.
B: Why?
Because local variables are used to disappear, the data for heap memory does not disappear immediately.
So, the heap memory is still using that variable, and the amount of change is gone.
In order for the value to exist, the final decoration is added.
With the anti-compilation tool, we see that, when final is added, the heap memory stores the value directly, not the variable name.
(7) Anonymous inner class (master)
A: A simplified form of a local inner class
B: Prerequisites
There is a class or interface
C: Format:
New class name or interface name () {
Override method;
}
D: Nature:
is actually a subclass of anonymous objects that inherit the class or implement an interface
(8) Use of anonymous internal classes in development
When we develop, we see abstract classes, or interfaces as parameters.
And this time, we know that what is actually needed is a subclass object.
If the method is called only once, we can use the format of the anonymous inner class to simplify.

Interface Person {
public abstract Void Study ();
}

Class Persondemo {
public void Method (person p) {
P.study ();
}
}

Class Persontest {
public static void Main (string[] args) {
Persondemo PD = new Persondemo ();
Pd.method (new person () {
public void Study () {
System.out.println ("Good study, day up");
}
});
}
}

(9) Anonymous inner class of face question (completion code)
Interface Inter {
void Show ();
}

Class Outer {
The code is padded
public static Inter method () {
return new Inter () {
public void Show () {
System.out.println ("HelloWorld");
}
};
}
}

Class Outerdemo {
public static void Main (string[] args) {
Outer.method (). Show (); "HelloWorld"
}
}

10_ Object-oriented (classes, abstract classes, integrated exercises of interfaces, formal parameters and return values, overview and explanation of packages, modifiers, inner classes)

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.