Oop_day05_package, public, static, final, inner class

Source: Internet
Author: User

Oop_day05_package, public, static, final, inner class

--20150813


1. Rewrite the method:

1) In parent-child class, method signature is the same, method body is different

2) When the override method is called, look at the type of the object

3) Follow the " two with two small one big " rule:

3.1) Both:

Same method name, same argument list

3.2) Two small:

The return value type of the subclass is less than or equal to the parent class-----is generally equal to

3.2.1) The return type is void, must be the same

3.2.2) The return type must be the same as the base type

3.2.3) When the return type is a reference type, it needs to be less than or equal to

Child class throws an exception that is less than or equal to the parent class

3.3) A large:

Subclass access control permissions greater than or equal to parent class

2. The difference between overriding and overloading---common face questions

1) Rewrite: parent-child class with same method name and same argument list

Follow the "run-time" binding to see how the object's type is called

2) Overloading: A class with the same method name and a different argument list

Follow the "compile period" binding to see the type binding method of the reference

3.package: Package

1) Function: Avoid naming conflicts of classes

2) package name can have a hierarchy, class fully qualified name: Package name. class Name

3) Recommendation: Package name all letters are lowercase

Import: Declaring classes/Introducing classes

1) Syntax: Import package name. Class Name

2) classes in the same package can be accessed directly

3) classes in different packages, there are two ways to access:

3.1) Fully qualified name-----cumbersome (not recommended)

3.2) First import declaration class, directly with the class name-----recommendations

4. Access Control Modifiers:

1) Public: open, anywhere

2) Private: proprietary, this class

3) Protected: Protected, this class, sub-class, same package class

4) Default: Do not write anything, this class, the same package class

Description

1) class access control: Public, default (not available in other packages)

2) Access control for class members: As above 4 kinds can be

Suggestions:

Data (variables) privatization (private), behavior (method) open (public)

5.static: Static

1) Static variables:

1.1) Modified by static

1.2) belongs to the class, there is a method in the area, only one copy

1.3) often accessed through the class name.

1.4) When to use: When all objects have the same data

2) Modification method: Static method

2.1) Modified by static

2.2) No implicit this pass, no direct access to instance members

2.3) often accessed through the class name.

2.4) When to use: the operation of the method is only relevant to the parameter and is not related to the object.

3) Block: static block

3.1) class is automatically executed during load, only once

3.2) When to use: Initialize static resources (Pictures, audio, video, etc.)

code example:

Package Oo.day05;//static Demo public class Staticdemo {public static void main (string[] args) {Joo O1 = new Joo (); O1.show (); C0/>//a=1,b=1joo O2 = new Joo (); O2.show ();  A=1,b=2system.out.println (JOO.B); It is recommended to access System.out.println (o1.b) through the class name point;  Koo.test () is not recommended; Static methods often access Loo O3 = new Loo () through the class name point; Loo O4 = new Loo (); Loo O5 = new Loo ();}} Class loo{static{//Because the class is only loaded once, the static block is only executed once System.out.println ("static Block");} Loo () {System.out.println ("construction Method");}} Class koo{int A;//instance variable, object point to access static int B;//static variable, class name point to access void Show () {//implicit pass THISTHIS.A = 1;b = 2;} static void Test () {//does not have an implicit this pass//a = 1;//Without this means that there is no object, no object means that the instance member cannot be accessed because the instance member needs to be accessed by the object point to B = 2;}} Class Joo{int A;        Instance variable: An object that exists in the heap, and there is a copy of the static int b; Static variable: belongs to the class, exists in the method area, only one copy of Joo () {a++;b++;} void Show () {System.out.println ("a=" +a); System.out.println ("b=" +b);}


6.final: Final (non-changing)

1) modifier variable: variable cannot be modified

2) Modification Method: Method cannot be overridden

3) Modifier class: class cannot be inherited

code example:

Package Oo.day05;//final Demo public class Finaldemo {public static void main (string[] args) {}}final class Poo{}//class Qoo E Xtends poo{}//Compile error, final class cannot be inherited by class Roo{}final class Soo extends Roo{}class noo{void Show () {}final void Say () {}}class Oo O extends Noo{void Show () {}//void say () {}//Compilation error, Final method cannot be overridden}/* * Final modified instance variable, initialized in two ways: *   1) Declaration simultaneous initialization *   2) The constructor initializes the final modifier to the local variable, */class moo{final int a = 1 with the previous initialization, and//declares that the final int B is initialized at the same time; Moo () {b = 2;//constructor initialization}void Show () {final int C;//before initialization can be done without initialization//a = 250;//Compile error, final variable cannot be modified}}


7. Internal class:----this to understand

1) class, the outer class is called the outer class, inside the class is called the inner class

2) Internal classes typically serve only external classes and do not have visibility outside of them

3) Inner class objects are typically created only in external classes

4) external class members (including private) can be accessed directly from the inner class

5) An implicit reference to an inner class that points to the outer class object that created it

----------External class name. This (Mama.this outer.this)

code example:

Package oo.day05;//Internal Class Demo public class Innerdemo {public static void main (string[] args) {Mama m = new Mama ();//baby B = new Baby (); Compilation error, inner class not externally visible}}class mama{//External class private String name; Baby Createbaby () {return new Baby ();} Class baby{//inner class void Showmamaname () {System.out.println (name);//Shorthand System.out.println (Mama.this.name);// System.out.println (this.name); Compilation Error}}}


8. Anonymous inner class:----Master

1) you want to create an object of a class, and the object is created only once,
This class is meaningless, at this point, the class does not have to be named,
Called Anonymous inner class

code example:

Package oo.day05;//Anonymous Inner class public class Nstinnerdemo {public static void main (string[] args) {//too O1 = new Too ();//Create Too Object 1. Created a subclass of Too, with no name//2. An object is created for the subclass, named O2//3. The class body of a subclass in curly braces//too O2 = new Too () {//};//1. A subclass of Too was created, without the name//2. An object is created for the subclass, named O3//3. The class body of the subclass in curly braces//o3 the object of the Too subclass//show () is the method for the Too subclass//Access method is O3.show () Too O3 = new Too () {public void Show () { SYSTEM.OUT.PRINTLN (111);}};/ /o3.show (); O3 is an object of the Too subclass, Show () is the method of the Too subclass             //o3.show () is the method that calls the subclass}}interface Too{void Show ();}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Oop_day05_package, public, static, final, inner class

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.