Title VII of the java-based Interface

1. What is the interface:

 * Interface is a set of methods, and interface methods are all abstract

 * You can put an interface as a special "abstract class", interface methods are all abstract

 *

 *

 * 2.java in how to define interfaces:

 * Define the class keyword: class

 * Define the interface keyword: interface

 * Format:

 * Public interface interface name {

 * // member method

 * public abstract void demo01();

 * public abstract void demo02();

 * }

 * Interface public abstract methods must be modified, you can not write, can also optionally write, but do not write does not mean that there is no (we recommend writing)

 *

 * 3. Interface can create objects? Can not create an object, the interface is also used as a natural parent

 *

 4. * class relationships and interfaces, called the implementation class that implements the interface (the relationship between the classes and class inheritance, implementation is the relationship between classes and interfaces)

 *

 * 5. how to achieve: implements (keyword implementation) extends (inheritance keyword)

 * A. A class that implements the interface, we call this class interface implementation class

 * B. A class inherits the abstract class, which we call a subclass of abstract class

 * 6. The members of the interface features:

 * A. All are public abstract methods must be modified

 *

 * B. Interface member variables must be modified by the public static final

 * We recommend above all modifiers are written on

 

 Interface members features:

 * 1. All methods are public abstract must be modified

 *

 * 2. The interface must be modified by the member variables public static final

 * We recommend above all modifiers are written on

 

 

 * Some interface features:

 *

 * 1. Class A and only single inheritance, classes and interfaces can achieve more than

 *

 * 2. The relationship between the interface and the interface is called inheritance, but the interface and the interface can be multiple inheritance

 *

 * Face questions: java support multiple inheritance in it?

 * Java classes and only single inheritance, but multiple layers can inherit

 * Java interface and the interface can be multiple inheritance

 *

 * 3. A class inherits another class, while achieving a plurality of interfaces

 * Fake code:

 * Public class subclasses of the parent class implements the interface extends 1, 2 ... Interface {

 * // subclass want to create an object

 * // 1. rewrite abstract methods of the parent class (there may not be possible)

 * // 2. Rewrite all interfaces in all the abstract methods

 * }

 

  The difference between abstract classes and interfaces:

 *

 * 1. abstract class can only be defined in all subclasses common content

 *

 * 2. The interface definition is a method other than the entire inheritance hierarchy

 

 Polymorphism: [more metamorphosis] forms a plurality of things

 * For example, a student, you can be seen as a student, you can be seen as people

 *

 * 1. There must be an inheritance relationship (or realization relationship)

 * 2. There must override the methods (but not rewrite the multi-state loses its meaning)

 12 * 3. Under the premise, the polymorphic forms of the parent class object reference variable points subclass

 * Interface reference variable pointing to the object implementation class

 * AbstractB  aa = new ClassB();

 * = New interest and ClassA ();

 *

 * Assuming abstract class Person {

 * public abstract void sleep();

 * }

 * 1. Inheritance

 * class Student extends Person{

 * // 2. Rewrite Method

 * public void sleep(){

 * ..

 * }

 * }

 * 3 forms:

 * Parent class object reference variables pointing to subclass

 * Student s = new Student (); // not polymorphic

 * Person p = new Student (); // polymorphism is

 

 Polymorphic member variables of the parent class and have only :( relationship)

 * 1. compile time: see parent class

 * 2. Run time: see parent class

 *

 * Polymorphism in the member method:

 * 1. compile time: see parent class

 * 2. Run time: see subclass

 *

 * Summary: The only member variable polymorphic and parent related, members of the multi-state compilation watching to see subclass the parent class runtime

 *

 * Multi-state benefits and drawbacks:

 *

 * 1. drawbacks: the child can only call methods common to the parent class, but can not be called a subclass-specific methods

 *

 * 2. Benefits: Improved scalability program

 * Case: Description multi-state benefits and improve the scalability of the program

 * Case feeding cats and dogs

 1. Extract * parent (Animal)

 * 2. cats and dogs inherit Animal, while rewriting the abstract methods

 

 

 * Polymorphic forms:

 * 1. premise:

  Wine Jiannanchun a =

 

      Wuliangye Liquor b =

 

      C = alcoholic liquor wine

 

      …

 

      Here is the performance of the polymorphism. Jiannanchun, Wuliangye, alcoholic liquor is liquor subclass,

  We just can refer to different subclasses of a superclass by this wine, which is polymorphic -

  We will not know the specific instance of an object reference variable points only at run time.

 * A. There must be an inheritance relationship (or realization relationship)

 * B. There must be a method of rewriting

 * 2. Expressions

 * Parent class type variable name = new subclass type ();

 * Interface Type variable name = new implementation class type ();

 *

 * Multi-state benefits and drawbacks:

 * A. There can only be called child-parent class content, can not be called a subclass-specific content

 See also the parent when the parent is running to see compilation: * member variables polymorphism

 * Members of the multi-state: the compiler is to see the parent class, subclass look at runtime

 . * B advantages: providers scalability, while improving the reusability of code

 *

 * Multi-state solution to the drawbacks :( transformation plan)

 * 1. upcast :( automatic conversion)

 * Double d = 1; // d is not printed but a 1.0

 * Person p = new Student (); // upcast

 * Animal an = new Dog (); // upcast

 *

 * 2. Transformation down (cast)

 * Int i = (int) 1.9; // print is a print out i

 * Person p = new Student (); // Polymorphic

 * Student s = (Student) p; // downcast

Knowledge Review yesterday:

 * The role of super and 1.this

 *

 * 2. The process of rewriting the concept

 *

 * 3. The method overrides meaning: to enhance the functionality of the parent class

 *

 * 4. What is an abstract method:

 * What is an abstract class:

 *

 * The meaning of abstract classes exist?

 *

 * 6. The significance of the existence of abstract methods abstract class?

 *

 * Supplementary: this (parameters):

 * Super (parameters): single principle

 *

 *

 */

public class TestDemo {

public static void main(String[] args) {

C c = new C ();. // 1 open space 2. 3. The return address constructor

// Constructors: 1: super () 2: initialize member (assigned default) 3: behind the method body

}

}

 

class A{

int x = 10;-

public A(){

super();

// initialize member

showX();

}

public void showX(){

System.out.println(x);

}

}

class B extends A{

public B(){

super();

 

showX();

}

}

class C extends B{

public C(){

super();

//initialization

showX();

}

}

 

1. Before we define member variables and member methods of a class of time, since we are to call these objects created by members

 *

 * 2. Define a class CZStudent (student number, name, sex, school: attend school)

 * Static member variables modified: for example: static String school

 * Be modified static member variables does not belong to a particular object that belongs to this class, but the students and shared variable

 *

 * Case: Chinese human

 * name.age,job,国籍

 *

 * Summary: to be modified static member variables, there is an in memory, save to static area

 * Then no matter how many objects created, all objects share a data

 * So we generally say modified static member variable called class members

 *

 * 3. be modified static member access:

 * Member variables:

 * Object name .static modified member variable name; // not recommended

 * Class name .static modified member variable name; // recommended

 * Member method:

 * Object name .static modified method name (); // not recommended

 * Class name .static modified method name ();

 * 4. Supplement: The modified static members and not modified static member access to each other's problems

 * Life Cycle questions:

 * Static member of modification occurred earlier -----> Qin Shihuang (ancestors)

 * Late-modified non-static member appears ----> Our (future generations)

Guess you like

Origin www.cnblogs.com/haizai/p/10990211.html