Java Learning--basic knowledge advanced fourth day--package and permission modifiers, inner classes

Source: Internet
Author: User
Tags modifiers

Introduction of today's content

U Package and Permission modifiers

U Inner Class

1th Chapter Package and permission modifiers

1.1 Overview of Packages

Java package, in fact, is the folder in our computer system, the package is stored in the class file.

When the class file is very large, we usually use multiple packages to store and manage them, which is called subcontracting management.

In the project, we put classes of the same functionality into a package for easy administration. And the division of the Daily project is also with the package as border.

1.2 Declaration format for packages

Usually use the company URL anti-write, you can have multi-layer package, the package name in all lowercase letters, multi-layer package between the "." Connection

The declaration format of a package in a class:

Package name. The package name ...;

such as: Black Horse programmer url itheima.com then the URL is Com.itheima

Preach Intelligence podcast itcast.cn so the URL is cn.itcast

NOTE: Statements that declare packages must be written in the first line of the program's valid code (comments are not counted)

Code Demo:

Package com.itheima_01;

/*

* Features of the package:

* Can have multi-layer

* Filenames under different packages can be duplicated

* Package declaration must be the first line of code

*

*/

public class Packagedemo {

}

1.3 packets are accessed between each other

When you access a class, you must use the full name of the class containing the package name (the package name, class name) in order to be able to find the class.

Package name. package name .... class name

such as: Java.util.Scanner

Java.util.Random

Cn.itcast.Demo

Class with package, create Object Format: Package name. class Name Variable name = new package name. Class name ();

Cn.itcast.Demo d = new Cn.itcast.Demo ();

Premise: the access to the package is closely related to access rights, in the general case, that is, the class is decorated with public.

Simplified access to classes

When we want to use a class, this class is in the same package as the current program (that is, the same folder), or if the class is a class in the Java.lang package, you can usually omit the name of the swap and use the class directly.

Each time we use a class, we need to write a long package name. Very troublesome, we can simplify by the way of import guide package.

This class can be used in the form of a guide, which avoids the use of the full class name (that is, the package class. Class name).

Format of the Guide package:

Import package name. class name;

1.3.1 Case Code One:

Package com.itheima_01;

Import java.util.ArrayList;

/*

*

* Mutual access between different packages

* Use the full name of the class

* Import the class using the keyword imports

*

*

* Note: package name. *

* * represents the wildcard character, which represents the import of all classes under this package, and does not import the class under the sub-package

*

* The full name of the class: the package name. The class name represents the class that represents the package (directivity), and cannot import the class under the child package

*

*

*/

public class PackageDemo2 {

public static void Main (string[] args) {

Classes under the same package can be accessed directly and do not require other operations

Packagedemo PD = new Packagedemo ();

Java.util.ArrayList list = new Java.util.ArrayList ();

ArrayList list2 = new ArrayList ();

}

}

1.4 Permission Modifiers

There are four access rights in Java, and when different access rights are used, the modified content has different access rights, and the following table describes the ability to access different permissions:

Public

Protected

Default

Private

In the same class

In the same package (subclass and unrelated classes)

Sub-classes of different packages

Unrelated classes in different packages

Summarize: In the daily development process, write the class, method, member variable access

A: To be accessible only in this class, use the private adornment

B: To be able to access the classes in this package except for the private modifier, you can

C: To make the class in this package accessible to subclasses in other packages using the protected adornment

D: All classes in the package can be accessed using the public adornment.

Note: If the class is decorated with public, the class name must be the same as the file name. There can be only one public-decorated class in a file.

1.4.1 Case Code Two:

Package com.itheima_02;

/*

* Permission modifier:

Public current class, different classes under the same package, classes under different packages

Default current class, different classes under the same package

Private Current class

Protected the current class, different classes under the same package

Default: Used under current package

Protected: Let subclass objects use

*

*/

public class Permissionsdemo {

public void Publicmethod () {

System.out.println ("Publicmethod");

}

void Defaultmethod () {

System.out.println ("Defaultmethod");

}

private void Privatemethod () {

System.out.println ("Privatemethod");

}

protected void Protectedmethod () {

System.out.println ("Protectedmethod");

}

public static void Main (string[] args) {

Permissionsdemo PD = new Permissionsdemo ();

Pd.publicmethod ();

Pd.defaultmethod ();

Pd.privatemethod ();

Pd.protectedmethod ();

}

}

Package com.itheima_02;

public class PermissionsDemo2 {

public static void Main (string[] args) {

Permissionsdemo PD = new Permissionsdemo ();

Pd.publicmethod ();

Pd.defaultmethod ();

Pd.privatemethod ();

Pd.protectedmethod ();

}

}

Package com.itheima_03;

Import Com.itheima_02.PermissionsDemo;

public class PermissionsDemo3 {

public static void Main (string[] args) {

Permissionsdemo PD = new Permissionsdemo ();

Pd.publicmethod ();

Pd.defaultmethod ();

Pd.privatemethod ();

Pd.protectedmethod ();

}

}

Package com.itheima_03;

Import Com.itheima_02.PermissionsDemo;

public class PermissionsDemo4 extends Permissionsdemo {

public void function () {

Super.publicmethod ();

Super.protectedmethod ();

}

public static void Main (string[] args) {

}

}

1.5 Modifier Summary

Modifier Summary

Modifier

Class

Member variables

Member Methods

Construction method

Public

Y

Y

Y

Y

Default

Y

Y

Y

Y

Protected

 

Y

Y

Y

Private

 

Y

Y

Y

Abstract

Y

 

Y

 

Static

 

Y

Y (called only in special cases)

 

Final

Y

Y (two types of initialization)

Y

 

2nd Chapter Inner Class

2.1 Internal class overview

A: What is an internal class

Writing classes inside other classes can be written in the member and local locations of other classes, where classes written inside other classes are called inner classes. Other classes are also known as external classes.

B: When to use internal classes

In describing things, if a thing contains other things that might be contained inside, such as when describing a car, the engine is included in the car, and the engine can be described using an inner class.

Class Car {//External classes

Class Engine {//Internal classes

}

}

2.2 Member Inner class

A member inner class that defines the position of a member in an external class. Similar to member variables in a class, accessible through an external class object

A: Define the format

Class External Classes {

Modifier class Inner Class {

Other code

}

}

B: Access mode

The external class name. Internal class name Variable name = new external class name (). New internal class name ();

2.2.1 Case Code Three

Package com.itheima_01;

/*

* Member Inner class:

* The member position of the class, and the member variable and the location of the member method is the same

* Within the inner class, members of the external class can be accessed directly, including private members

*/

public class Innerdemo {

public static void Main (string[] args) {

Outer o = new Outer ();

O.method ();

Outer.Inner i = new Outer (). New Inner ();

I.function ();

}

}

Class Outer {

private int num = 10;

public void Method () {

Inner i = new Inner ();

I.function ();

}

Class Inner {

public void function () {

SYSTEM.OUT.PRINTLN (num);

}

}

}

member Inner class

Modifiers that can be used by a member's inner class: Private,public,procted,final,static,abstract

2.2.2 Case Code IV

Package com.itheima_01;

/*

* Modifier for member inner class:

* We can use the permission modifier to decorate a member's inner class, but if you use private to decorate it,

* cannot be accessed in other classes

* We can use static to decorate a member's inner class without having to create an object of the outer class again.

*

* We can use abstract,final to decorate member inner class

*/

public class InnerDemo2 {

public static void Main (string[] args) {

Outer2.inner2 i;

Outer2.inner2 i = new Outer2.inner2 ();

I.function ();

Outer2.Inner2.function ();

}

}

Class Outer2 {

public void Method () {

Inner2 i = new Inner2 ();

}

Static Class Inner2 {

public static void function () {

System.out.println ("function");

}

}

}

2.3 Local Inner class

A local inner class that defines a local location within an external class method. Similar to local variables in Access methods, accessible by calling methods

A: Define the format

Class External Classes {

Modifier return value type method name (parameter) {

Class Inner Classes {

Other code

}

}

}

B: Access mode

In the external class method, create an inner class object to access

2.3.1 Case Code Five:

Package com.itheima_02;

/*

* Local inner class

* Within the method, the method cannot be used.

*

*

*/

public class InnerDemo3 {

public static void Main (string[] args) {

Outer o = new Outer ();

O.method ();

}

}

Class Outer {

public void Method () {

int num = 10;

Class Inner {

public void function () {

System.out.println ("function");

}

}

Inner i = new Inner ();

I.function ();

}

public void Test () {

Inner i = new Inner ();

SYSTEM.OUT.PRINTLN (num);

}

}

2.4 Anonymous Inner class

A: function: Anonymous an inner class is a shortcut to create a subclass object of a type.

B: Format:

New parent class or interface () {

To do a method rewrite

};

Code Demo

The parent class that already exists:

Public abstract class person{

public abstract void Eat ();

}

Defines and creates a subclass object of the parent class, and assigns a value to the parent class reference variable in a polymorphic way

Person p = new person () {

public void Eat () {

SYSTEM.OUT.PRINTLN ("I ate");

}

};

Calling the Eat method

P.eat ();

Using an anonymous object, the subclass is defined with two steps to create a subclass object by one format at a time. Although it is a two-step process, the two steps are done together.

Anonymous inner class is also an anonymous object if you do not define a variable reference. The code is as follows:

New Person () {

public void Eat () {

SYSTEM.OUT.PRINTLN ("I ate");

}

}.eat ();

2.4.1 Case Code VI:

Package com.itheima_03;

/*

* Anonymous Inner class:

* You can think of an anonymous inner class as a local inner class without a name

* Defined in the method

* The object must be created when an anonymous inner class is defined

Format

* New class/interface () {

* If you create a subclass object that inherits this class, we can override the parent class's method

* If you are creating a subclass object that implements this interface, we must implement all the methods of that interface

* };

* Principle: Instead of creating a subclass object inheriting this class or creating a subclass object that implements this interface

*

*/

public class InnerDemo4 {

public static void Main (string[] args) {

Outer o = new Outer ();

O.method ();

}

}

Interface Inner {

public void function ();

}

Class Outer {

public void Method () {

/*new Inner () {

@Override

public void function () {

System.out.println ("function");

}

}.function (); */

Inner i = new Inner () {

@Override

public void function () {

System.out.println ("function");

}

};

I.function ();

I.function ();

}

}

2.4.2 Case Code VII:

Anonymous inner classes are passed as parameters

Package com.itheima_04;

Public interface Animal {

public abstract void Eat ();

}

Package com.itheima_04;

public class Cat implements Animal {

@Override

public void Eat () {

System.out.println ("Cat eats fish");

}

}

Package com.itheima_04;

public class Dog implements Animal {

@Override

public void Eat () {

System.out.println ("Dog chew Bones");

}

}

Package com.itheima_04;

/*

* Application Scenarios for anonymous internal classes:

* Passed as a parameter

*

*

*/

public class InnerDemo5 {

public static void Main (string[] args) {

Method (New Dog ());

Method (New Cat ());

Method

New Animal () {

@Override

public void Eat () {

System.out.println ("Cat eats fish");

}

}

);

}

public static void Method (Animal a) {

A.eat ();

}

}

Java Learning--basic knowledge advanced fourth day--package and permission 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.