Qianfengday10-java Basic Learning: Member inner class, Static inner class, local and anonymous inner class, simple Factory mode of design pattern

Source: Internet
Author: User

Course review:

Interface:
1. Properties: public\static\final
2, Method: Public\abstract

Polymorphic:
1. Static (compile-time) polymorphism
Overload
2. Dynamic (runtime) polymorphism
Rewrite
Object transformation
1, Upward transformation
2. Downward transformation

Today's content:
Inner classes: Classes that are defined inside a class
1. member Inner class
Format: [modifier] class class name {

}
There can be no static properties and methods inside,
You can access the properties of the external class,
You can also call methods of external classes

Format used in static methods:
Outer class Outer =new outer Class ();
member Inner class object name = Outer object. New member inner class ();


2. Static internal class
Format:
[modifier] Static class name {

}
Internal can have static/non-static properties and methods
You can invoke the static properties and methods of an external class
However, you cannot invoke external class non-static properties and methods

Use:
The external class name. Static inner Class name object =new external class name. Static inner class ();

3. Local inner class
Format: class Name {}
Classes defined inside a method

Local variables are required to be final decorated when calling local variables.
When used, only within the corresponding method

4. Anonymous inner class
Format: New class name/interface name () {overridden method}
An anonymous inner class creates a subclass or implementation class
and generate an Object
Often used to instantiate abstract classes or interfaces
Anonymous inner class has no class name and is compiled with a digitally named bytecode file


Internal interface: an interface defined inside a class
An interface can be defined inside the class
Interfaces can inherit multiple interfaces


Exercise: Please define an interface Istudy
Own method: study (), Study (String msg);
Create a class student
Attribute several, method custom
Requires one method: public void Showstudy (Istudy ist)
Finally create the test class and call the Showstudy method of student

Simple Factory mode of design mode:
The roles involved:
1. Parent of all products: Class of properties and methods common to product classes
2, Product Category: the product category involved
3. Factory class: Provides static methods for creating objects


Idea: This pattern is for decoupling, meaning that when you use specific objects,
No need to care about the object's creation process
You just need to get the corresponding object based on the specified instruction.
object is created by the static method of the factory class.
Because the object to be involved in multiple product classes is returned, we use the parent class
So the subclass object can act as the parent class object

The simple factory embodies object-oriented: Object transformation (up and down)


Practice: Selling Drinks

Object-Oriented Summary:
Object-oriented is a kind of thought
Object-oriented three features: encapsulation, inheritance, polymorphism
Everything is object.
Object-oriented 2 large core:
1. Class: Abstraction, template, concept, definition: A combination of properties and methods that all objects share
2, the object: concrete, instance

The format of the class:
[Modifier] class class name [extends class name][implements interface name,......] {
Properties
[modifier] Data type property name;
Public/protected/private/final/static
Method
[modifier 1, modifier 2] Returns a value type method name ([formal parameter list]) {
code block;
}
[Construction Method]
[Modifier] class name ([formal parameter list]) {
code block;
}
}
Object Creation Format:
Class Name Object name =[new class name ([actual parameter list]);


Public/final/abstract can modify the class

This usage: this class of
1. Refers to this kind of object
can be used to distinguish between member variables and local variables with the same name

2. Call other construction methods of this class
Only in the constructor method
Must be in the first line
Format: this ([actual parameter list]);

Encapsulation of attributes:
1. Property Private
2. Public get and Set methods

Standard class JavaBean:
1. Property Private
2. Public get and Set methods
3. Non-parametric construction method of public

Static usage: statically
1. Modifier Properties: Static properties
2. Modification method: Static method
3. Decorate code block: Static code block
4. Modify INNER class: Static inner Class


Access modifiers:
1. Public: Common
2. Protected: Protected
3, Default/package (only indicates a state without an access modifier): Default
4. Private: Proprietary

Package:
1. Package statement
2. Import the Package


Inheritance: One class has properties and methods of another class
Extends
Java only allows single inheritance, but can inherit the transitivity of

Super usage: in sub-class
1. Calling properties and methods in the parent class
2. Calling the parent class's construction method
Only in the constructor method of the subclass
Only on the first line
Format: Super ([actual parameter list]);

Overriding: Subclasses enhance methods in parent classes
1, subclasses, overrides the method of the parent class
2. The access modifier cannot be more restrictive than the parent class
3, static/non-static, return value type, method name, formal parameter list must be the same


Object class: Parent class for all classes
Common methods:
1, Hashcode
2. toString
3. Equals
4, GetClass


Final usage: the ultimate
1. Modifier variable: Constant, can only be assigned once
2. Modification method: cannot be rewritten
3. Modified class: Cannot be inherited


Abstract usage: abstracted
1. Modification Method: Abstract method
Format: [modifier] Abstract return type method name ([formal parameter list]);
Abstract methods are only declarations of methods, no methods are implemented (no method body)
2. Modifier Class: Abstract class
Abstract classes cannot be instantiated
Abstract classes do not necessarily have abstract methods
Classes with abstract methods must be abstract classes

Interface: A struct composed of constants and abstract methods
Properties: Default has public, static, final
Method: Default has public, abstract

Polymorphism: Multiple forms
1. Static (compile-time) polymorphism
Overload
2. Dynamic (runtime) polymorphism
Rewrite
Object transformation
1, upward transformation: Subclass to Parent class, automatic
2, down transformation: parent class to subclass, force
Generally used in conjunction with instanceof

instanceof: Usage
Verify that the object is of a certain type
Format: Object name instanceof class name

Inner class: A class defined inside a class
1. member Inner class
The internal cannot use static
member Inner class Name Object = External class object name. New class name ();

2. Anonymous inner class
Format: New class name/interface name () {overridden method}
Can only be used once, without a class name, resulting in an object

3. Static internal class
Non-static properties and methods of external classes cannot be used internally

4. Local inner class
is defined inside a method and can only be used in methods
Local variables are required to be final decorated when manipulating local variables.

Design mode:
1. Singleton mode: The only instance of guaranteed class
Rules:
1. Private Construction method
2. Private static This class object
3. public static method (return to this class object)

2. Simple Factory mode: decoupling
Rules:
1. Product Category
2, the product class of the parent class
3. Factory class (static method)
Embodiment: Object Transformation

The Java principle:
1, the minimum scope of the strongest principle (nearest principle)
2, the best matching principle (overload)


Next week content:
Array
Abnormal
Common classes
Collection


Qianfengday10-java Basic Learning: Member inner class, Static inner class, local and anonymous inner class, simple Factory mode of design pattern

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.