Learning Java eighth day

Learning Java eighth day

Abstract classes and abstract methods

  • With abstract keyword to modify a class, the class is called abstract class.
  • With abstract to modify a method called abstract method.
  • Abstract Methods: Only the method declaration does not implement the method. Ends with a semicolon: for example: public abstract void talk ();
  • Abstract class containing the method must be declared as an abstract class.
  • An abstract class can not be instantiated. An abstract class is used to be inherited, subclasses of the abstract class must override the abstract parent class method, and a method thereof. If not override all abstract methods, is still an abstract class.
  • Abstract variables can not be modified, block builder;
  • Private methods can not be modified with abstract, static methods, final methods, final classes.

Anonymous class

  • format:
  • An abstract class creates an instance of the far right plus braces
  • And rewriting the abstract methods

Template Method design pattern

  • When implementing an algorithm in software development, overall steps are fixed, general, these steps have been written in the parent class. But some parts of the volatile, volatile portions can be abstracted out for different subclasses to implement. This is a kind of template mode

interface

why:

  • On the one hand, and sometimes you must be derived from several classes in a subclass inherits all of their properties and methods. However, Java does not support multiple inheritance. With the interface, you can get the effect of multiple inheritance.
  • On the other hand, we may be extracted from several classes some common behavior characteristics, and nor is-a relationship between them, only it has the same behavior characteristics. For example: mouse, keyboard, printer, scanner, camera, charger, MP3, mobile phones, digital cameras, mobile hard disk and so support USB connections.

What:

  • Interface is a specification that defines a set of rules that reflect the real world. "If you are / you must be able to ... to ..." thinking. Inheritance is an "is not" relationship, while the interface is "can" relationship.
  • The nature of the interface is the contract, standards, specifications, like our laws the same. After making a good we all have to abide by.

Interface features:

  • Interface is a specification that defines a set of rules that reflect the real world. "If you are / you must be able to ... to ..." thinking. Inheritance is an "is not" relationship, while the interface is "can" relationship.
  • The nature of the interface is the contract, standards, specifications, like our laws the same. After making a good we all have to abide by.

Notes interface;

  • The syntax definition of Java classes: the first to write extends, after writing implements

    class SubClass extends SuperClass implements InterfaceA{ }

  • A class can implement multiple interfaces, other interfaces can also be inherited interfaces.

  • Class that implements the interface must provide a specific interface implemented in all methods content, before instantiation. Otherwise, the class is still abstract.

  • The main purpose of the interface implementation class is to be achieved. (Oriented Programming Interface)

  • Similar inheritance, polymorphism between the interface and implementation class

  • Interfaces and classes are juxtaposed relationship, or can be understood as a special class. Essentially, the interface is a special abstract class, which defines only the abstract class contains constants and methods (JDK7.0 and before), but not implemented variables and methods.

For his summary interface

    接口 里面 定义了抽象方法 也就是一些功能 但是这些功能 在不同的实现类当中 实现这个功能的步骤 方法也是不一样的
    相似的 在usb 这个接口当中 有传输数据的这个功能 但是在 电脑 和 手机 上用usb 传输数据的 具体方法是不一样的 所以就要加载不同的驱动程序

New features after jdk8.0

  • You can define static methods and the default method

  • Static methods in an interface can only be invoked via the interface

  • The default interface method can create an instance of the class interface to call

  • Both interfaces are defined in the default method of the same name with the parameters of the conflict can be resolved by implementing the interface class and override the default method call is overridden

  • If the interface and the parent class defines the default method parameters with the same name on the principle of giving priority to follow the parent class

  • Call interface implementation class in which the default method

     A.super.method();

Agent proxy mode

  • Proxy mode is used in Java development more of a design pattern.
  • Acting is designed to provide a proxy to control access to this object to other objects

Factory model factory

  • Will create and call the object separation

Comparison between abstract classes and interfaces

  • The same point: can not instantiate can be inherited
  • difference:
  • Abstract class: there constructor
  • Interface: You can not declare a constructor
  • Multiple inheritance and single inheritance

Inner classes

  • Class inner member into an internal class (static and non-static) (inner code block constructor method) VS local inner class

  • Members of the inner class:

    • The method can be defined attribute constructors
    • Can be modified final
    • Abstract may be modified
    • You can call members of the outer class
    • It can be modified static
    • It can be modified different modifiers
  • Focus on the following three issues

    • How to instantiate an object of class members inside

      Peson.Dog dog = new Person.Dog
          //静态成员内部类的声明对象
      Person p = new Person();
      Person.Organ o1 = p.new Organ();
      //动态成员内部类对象的创建
    • How to distinguish the call structure of the external members of the class in inner class

    • Development using local inner class

      public class ClassA {
          // 开发中常用 返回一个实现了Comparable接口实现类的对象
          public Comparable getcomparable(){
              class MyComparable implements Comparable{
      
                  @Override
                  public int compareTo(Object o) {
                      return 0;
                  }
              }
              return new MyComparable();
          }
      }

Guess you like

Origin www.cnblogs.com/wumingzhibei/p/12545377.html