Java learning -4

One of the three characteristics of object-oriented: inheritance

The main problem: the common extraction

Among the characteristics of inheritance: a subclass can have a parent class of "content", subclasses can also have their own exclusive content

 

A format defined parent class (class is the definition of a common format):

Public class class name {

  Public method Method Name Type {

  }

}

Define a subclass format:

Public class subclass name extends the parent class name {

//........

}

If the parent class variable with the same name in the subclass variable, object creation is a priority with whoever who among the variables.

If the variable methods in the parent class and subclass objects are created using methods whose priority whoever among.

 

Member variable to distinguish between the parent class, subclass member variables, local variables:

Local variables: direct use

Subclass member variables: . The this member variable name

The parent class member variables: . Super member variable name

 

Parent-child class constructor access features:

Subclasses must use the parent class constructor, a default is not written using Super () ; if you use only once and must be the first statement of position.

 

super keywords of

  1. Way to access the parent class members
  2. Access to member variables of the parent class
  3. Access the parent class constructor

 

 

Inheritance of 3 major feature

Single inheritance (superclass of a class can only have one), multi-level inheritance (a parent class can have multiple top-level parent, the parent class is the highest java.long.Object ), multiple subclasses (how can a parent class subclasses)

 

Abstract method

Use: Class of them uncertain what its content is a method, the method may be an abstract class

Structure: Returns the value of the method is preceded by an abstract key, braces removed, but must be an abstract method can be defined in an abstract class, an abstract class structure of the class before adding an abstract key.

 

 

 

Instructions:

Example: Main Method

      package peizeng;


public class text {
    public static void main(String[] args) {
        zi zi = new zi();
        zi.fu();
    }

}

      Abstract parent class and abstract method

      package peizeng;

      public abstract class fu {
           public abstract void fu();
       }

      Subclasses of the parent class method overrides a method in abstract method

      package peizeng;

class public zi and the extends FU {
    public void FU () {
        . the System OUT .println ( " sub-methods to perform it! " );
    }
}

 

If the parent class is an abstract class, the subclass must override all abstract methods of the parent class, otherwise an error unless the subclass is also an abstract class

 

interface

Interface is a kind of common specification standards

Defines the basic format of an interface:

Public interface Interface Name {

  // interface content

}
Interfaces include: Constant, abstract method, the default method, static methods, private methods.

 

Interface using the steps:

1. The interface can not be directly used, there must be a "implementation class (subclass similar)" to "realize" that interface

format:

Public class implementation class name implements interface name {

  //........

}

  1. Override all abstract methods (implement) the interface implementation class interface must cover
  2. Create an object implementation class, be using

 

The default interface method

What is the default interface method, that is, when the interface needs to be upgraded (to add a new abstract method), will lead the class has been defined interfaces error, so if the new abstract method to add changed the default method, it will not appear this situation, defined interface class will by default have a default method.

The default method definition:

Public default return type method name (parameter list) {

// method body

}

 

Static method interface

Define a static method:
public static return type method name (parameter list) {

// method body

}

Usage: When using directly through (the interface name . Static method name) call, call the class object can not be achieved.

 

Interface private methods

When a method is only intended to be used in the interface, not desirable to use in the implementation class can be defined as private methods.

  1. Ordinary private methods: Repeat the code to solve the problems between multiple default method

format:

Private return type method name (parameter list) {

  The method body

}

  1. Static private methods: resolve duplicate code across multiple static method problem

format:

Private static return type method name (parameter list) {

The method body

}

 

Constant Interface

That the member variables

format:

Public static final data type constant name = data value;

note:

This 3 keywords can be omitted, must be assigned, once the assignment can never be changed, the name of capital.

Call: the name of the interface . Constant Name

 

Notes on use interface

  1. Although a class can have only one parent, but can have multiple interfaces

Example: public class implementation class name implement interface name 1 , interface name 2 {

// cover so overridden method

}
2 If the same abstract method in the presence of a plurality of interface, then one must overwrite

3 .. If there is a plurality of identical interfaces implemented default method, then override the default implementation class must be overridden method conflict.

4 may also be present between the interface and inherits the interface,

For example, an interface needs to inherit one or more interfaces :

Public interface interface name extend the interface 1 , the interface 2 , ...... {

// method body

}

 

One of the three features of object-oriented: polymorphic

Concept: parent application to sub-class object

Format: name of the parent class object name = new subclass name ();

Or interface name object name = new implementation class name ();

 

Polymorphic Rules for use of member variables:

Direct use: the equal sign is left who will come with priority, not then look up

Indirect use (access by members of the member variables method): Method priority belongs to whoever is with who, not then look up, but the new method overridden by a subclass of class if the quilt cover is used

 

 

Use multi-state rules in member methods:

Look at new that will come with priority who, not then look up, namely: Compile look left, look to the right to run.

 

Transition objects

Strong rotation similar to data , up only a small range of steering a wide range, but the process will lose subclass specific method

Up transformation (that is, multi-state writing):

Format: name of the parent class object name = new subclass name ();

Downward transition (to restore to the original parent class object subclass object, conditions: must be a corresponding sub-class object)

Format: Name subclass object name = (subclass name) parent class object ();

Use instanceof keyword for the original parent class object is determined which sub-class object and returns a Boolean value

Example:

Animal Dog = new new Animal (); // this is upcast

ifanimal instanceof Dog{

  //......

}

 

Final keyword represents the final and unalterable

Common use:

  1. Modification of a class: the class can not have subclasses. Public final class XXX {.....}
  2. A modification of the method: the class can not be overwritten. Public final XXX {......}
  3. Modifying one local variable : If the basic data types, the data can not be changed after the assignment, if it is a reference address data type can not be changed. Int = 10 Number Final ;
  4. Modification of a member variable: Private Final XXX xx ; you must direct assignment or assignment by the constructor

 

Inner classes

I.e. inside a class contains another class.

Category: internal class members and the local inner class.

 

Members of the inner class:

Modifier class external name class {

  Modifier class inner class name {

    //...

  }

  //...

}

Usage: external name class . Internal name of the object class name = new outer class name () .new internal class name ();

 

How to access external member method of the class within the class: class name outside .this. External class member variable name

 

Internal class local (within a defined class method):

Modifier class external name class {

  Modifier return type method name (parameter list) {

    class partial internal class name {

  //......

    }

  }

}

Note: If the member variable access method in order to local inner class, the member must be a (valid final value) which can only be assigned once.

 

 

Anonymous inner classes

When the interface implementation class requires the use of only one time, it may be omitted to use the implementation class anonymous inner classes.

Definition Format:

Interface Name object name = new interface name () {

// overwrite all abstract methods

}

Note: it can only be used when creating the object only once

Guess you like

Origin www.cnblogs.com/pzbangbangdai/p/11616248.html