Summary of learning content such as inheritance of polymorphic abstract class interface

One:
Inheritance:
1. Concept: Extract multiple classes into an independent class, so that independent classes and multiple classes have an inheritance relationship

  1. Inherited keyword: extends
    3. Format:
    class subclass name extends parent class name {
    ...
    }
    4. The benefits of inheritance:
    1> Provides code reuse, solves the bloated code
    2> It is polymorphic The premise (the premise of polymorphism is that there must be an inheritance relationship
    5. Inheritance characteristics:
    the subclass inherits the parent class, which inherits all the things of the parent class (member variables, member methods, including private), but the subclass cannot use private things , it can only be indirectly accessed by subclasses through the public access of the parent class.

    1. Another feature of inheritance:
      1> In Java, inheritance only supports single inheritance and does not support multiple inheritance (subclass name extends parent class name 1, parent class name 2,...)
      2> However, Java can support multiple inheritance Layer inheritance...
      3> The relationship between classes and classes: inheritance relationship
      4> The relationship between classes and interfaces: implementation relationship
      7. Precautions in inheritance:
      1> Constructors cannot be inherited, but through the super keyword To access
      2> private can indirectly access
      3> when to use extends?
      Suppose: there is a
      class A class A{
      public void show1(){}
      public void show2(){}
      }

                                                  有一个B类
                                                      class B{
                                                              public void show1(){}
                                                              public void method(){}
                                                      }
          4>按照正常的情况:发现A类和B类中有共同的show1()方法,根据继承的概念,---->让B类继承A类
                                                  class B extends A{
                                                                      public void method(){}
                                                              }

      There is no problem, but class A is inherited, show1(), show2() are also inherited, maybe show2() is not the function I want; don't use inheritance in this case
      5> Inheritance reflects a kind of "is" "a" relationship:
      6> If A is a kind of B or B is a kind of A, this can use inheritance! In inheritance, matters needing attention
      on the name of member variables : 1> When the current subclass inherits the parent class, If the names of the member variables in the subclass and the superclass are inconsistent, it is very simple to output them separately; 2> When the names of the member variables in the subclass and the superclass are the same: a) Go to the local location of the subclass to find , if found, output b) Not found, look for the member position of the subclass, output if there is, c) If the member position of the class has not been found, directly look for the member position of the parent class, and output d) If it is still No, save it, there is no such variable 2: Question about inherited members? Constructor: 1> Subclass inherits the parent class, and will access the parent class's no-argument constructor by default 2> Assuming that the data has not been initialized , so the parent class should be initialized first, and then the subclass should be initialized ---> Hierarchical initialization ? Question thinking? What if the no-parameter structure of the parent class is not provided? What should I do? Answer: If there is no parameter-free structure, an error will be reported Solution: 1> Provide a no-parameter 2> The subclass accesses the parent class's parameters through super() to initialize it 3> You can also use this() in the subclass to access the parameterized structure in this class , Indirectly access the parent class with parameter construction 4> There must be one construction method of the subclass (parameter construction / no parameter construction), let the parent class initialize!



















      Three:
      The problem of member methods in inheritance?
      1) When the subclass inherits the parent class, and the access member method names are inconsistent, call them separately!
      2) When the member method name in the subclass is the same as the member method name in the parent class:
      3 ) First look for the member position of the subclass, if there is one, call
      4) If not found, look for the member position of the parent class, and call
      4:
      Regarding the usage of inheritance:
      1) It is possible to modify the private class in the parent class Inheritance, but can only access private ones indirectly.
      2) The parent class is modified by private, and the subclass cannot directly access the
      five
      supplements:
      1> The subclass continues the parent class, the parent class is initialized first, and then the subclass To initialize, this is the hierarchical initialization in inheritance
      2> class name object name = new class name (); this sentence creates an object and initializes it through the constructor (default initialization, display initialization)

      一:
      1)多态的概念:
                     多态就是事物在同一个时刻,体现出来的不同状态;
      2)多态的好处:
        1>可以提供代码的复用性:继承保证
          2>可以提高的代码的扩展性:由多态保证...  (父类的引用指向子类对象)
      3)多态的弊端:
                  1>父类引用指向子类对象,
                  2>通过父类的引用调用子类特有功能,不能调用....
                  3>不能访问子类特有功能
        4>将父类的引用强制转换子类的引用  ,向下转型使用不当,会出现一个异常:属于运行时期异常:ClassCastException
                  二:
                     1):
                       方法重写:
          1>由于子类继承父类的时候,提供一摸一样的方法声明,然后会将父类该方法覆盖掉(重写,复写)
         2>有时候(具体的需求),不需要子类重写父类的功能,针对这种情况,Java提供了一个关键字:final  最终的,终态的意思
                      2)
                       final关键字:
                          1> final:表示最终,终态(不能被更改的)
                          2>它可以修饰类,那么该类不能继承
                          3>它可以修饰成员方法,成员方法不能被重写
                          4>它可以修饰变量,此时这个变量是一个常量
                          5>常量的分类:
                                  字面值常量:
                                  字符串常量,字符常量,,,,
              3)
              1>final不仅可以修饰基本数据类型
        2>还可以引用类型
              3>如果final修饰的是一个基本数据类型:基本数据类型的值不能再改变了...
              4>如果final习俗的是一个引用类型数据:引用类型的地址值不能再改变了,但是堆内存中的成员变量的值可以变得

      5> The final initialization timing is initialized before use, and assignment (assignment before the constructor) (non-static...)
      Three:
      Code block description:
      1) The code enclosed in {} is collectively referred to as code;
      according to its The location and declaration are different: divided into??
      1> Construction code block: In the member position in a class, enclosed in {}, the
      effect: You can put the same code in multiple construction methods into the construction code block, Initialize the object. Before each execution of the constructor, execute the construction code block.
      2> Static code block: In the member position of a class, it is also wrapped with {}, but it is modified by static
      : in general, its Function to initialize the class
      Interview questions?
      The priority of construction code block, construction method, static code?
      1> Static code block> Construction code block> Construction method
      Note:
      1> Static code: can only be executed once
      2> Construction code block is in It will be executed every time before the constructor is executed.

                      -------------------------------------------------------------------------------------
      
      一:
          抽象类概念:
                  Java中,如果一个类中有一个方法声明(抽象方法)抽象功能,那么这个类定义为抽象类

      1)
      Keyword: abstract meaning
      2)
      Features of abstract classes:
      Abstract classes cannot be instantiated directly! (Cannot create objects) Interfaces cannot be instantiated
      3) About abstract classes:
      1> If a class has abstract methods, then This class must be an abstract class
      2.> Does an abstract class have to have abstract methods? Not necessarily all abstract methods
      in an abstract
      class
      Can not be instantiated, how to create an object
      2> subclass concrete class, then the subclass must implement the abstract function in the parent class.
      5)
      6) Member characteristics of abstract class:
      1> Member variable:
      can be a variable or a constant
      2> Constructor: with or without parameters, with parameters, role: to initialize the object.
      3>Member method: there can be abstract methods and non-abstract methods...
      2:
      abstract and which keywords are in conflict , cannot share?
      1>abstract and private
      2>abstract and final
      3>abstract and static

      One:
      1) The concept of interface:
      interface: reflects the extension of the transaction ××× (additional actions, acquired learning, etc.)
      2) interface format (identifier: class, interface: see the name and meaning)
      interface interface name {
      //abstract function
      public abstract void jump() ;

                                                              }
          3)接口的特点:不能实例化
          4)接口的子实现类:实现类 (具体的实现类)
                                                                      class 类名+impl implements 接口名{
      
                                                                      }

      5) If the subclass of the interface is an abstract class: it is meaningless and cannot be instantiated
      6) Use???
      Interface polymorphism (at most)
      Abstract class polymorphism (more)
      The creation of concrete objects (often used)
      Two:
      interface members Features:
      1) Member variable: a constant, cannot be changed, and the default modifier
      public static final:
      2) Constructor: there is no constructor in the interface
      3) Member method: all abstract methods
      Default modifier: public abstract
      4) The relationship between interfaces and interfaces: inheritance relationship
      3 :
      In actual development, is the interface used as a formal parameter?
      1> If the formal parameter is an interface:
      the traditional way:
      1> Provided to the sub-implementation class of the interface
      2> Inner class The way (no need to provide a sub-implementation class)
      four:
      1) The problem of inner class:
      inner class: define class A inside class B, class A belongs to the inner class of B
      2) Features of inner class accessing outer class:
      it can Direct access to external members, including private
      3) How do external classes access members of internal classes?
      Indirect access by creating internal class objects...
      4) Classification of internal classes:
      1> Members Internal classes: members of external classes Position
      2> Local inner class: Local position
      5) Member inner class in the outer class:
      1> Can directly be a member of the outer class, including private
      2> The outer class wants to access the member methods of the inner class (non-static inner class):
      3> ​​Format: outer class name. inner class name object name = outer class object. inner class object
      6)
      Modifiers about member inner classes:
      1>private: Function: Ensure the security of data!
      2>static modification: static inner class can be regarded as a member of outer class.
      3>Features: static member inner class accesses the data of the outer class, and the data must be statically modified7 ) Local inner class:
      1> You can access the members of the outer class, including private...
      2> To access the show() of the inner class in the local position of the outer class, you need to create an inner object in the local position, and access it through the object
      8) Interview question?
      Why does this local variable have to be modified with final?
      Because of life cycle issues, the local variable in the method will be released after the method ends, and final ensures that this variable always points to an object.
      9) Anonymous inner class
      1 > The premise is that there is a class or interface
      2 > This class can be a concrete class or an abstract class
      3 > new class name or interface name {
      method override ();
      }

    4> The essence of anonymous inner class: it inherits the class or implements the interface subclass object...
    Interview questions??
    Anonymous inner class interview questions:
    fill in the code
    interface Inter { void show(); }
    class as required Outer { //Complete the code}
    class OuterDemo {
    public static void main(String[] args) {
    Outer.method().show();
    }
    }
    request to output "HelloWorld" in the console

                                        interface Inter3{
                                            void show() ;//public abstract 
                                        }
                                        class Outer7{
                                            //补齐代码
                                            public static Inter3 method() {
    
                                                //返回的是接口:当前并不提供接口的子实现类,所以只能用匿名内部类
                                                return new Inter3() {
                                                    public void show() {
                                                        System.out.println("helloworld");
                                                    }
                                                };
                                            }
                                        }
                                        //测试类
                                        public class Test {
    
                                            public static void main(String[] args) {
                                        //      Outer.method().show();
                                                //Outer7.method() :当前这个method方法是静态方法
                                                //Outer.method().show(); //Outer.method()---->返回一个对象.show()
                                                //由于show()方法是一个接口中的方法,返回值是一个接口类型
    
                                                Outer7.method().show();
                                            }
                                        }                                               
    
    10)形式参数的问题:
         1>形式参数是引用类型:
            a>引用类型:是一个抽象类
                b>形式参数是一个抽象类:此时这块需要的是该抽象类的子类对象   (抽象类多态
    11)形式参数是引用类型
                                         1>形式参数是一个接口 
                                     2>形式参数的问题:
                                                                                形式参数是基本类型,对实际参数没有影响(简单)
                                                                                形式参数是引用类型:
                                                                                        类(普通类)
                                                                                            抽象类
                                                                                        接口
      12)返回值:
        1>如果返回值基本类型:用对应的基本类型去接收数据即可!
        2>引用类型:
                            类(具体类): 需要的是该类的对象
                            抽象类
                            接口
           3>如果返回值基本类型:用对应的基本类型去接收数据即可!
    4>引用类型:
        类(具体类): 需要的是该类的对象
        抽象类:那么返回需要的结果是需要当前抽象类的子类对象 抽象类多态
        接口
            接口和抽象类的区别:

    Difference problem????
    A) Difference between members
    1> Member variable:
    abstract class: either constant or variable
    Interface: only constant, default modifier: public static final
    2> Constructor:
    abstract class: There are constructors, no parameters/parameters (initializing objects)
    Interface: no constructors
    3>Member methods:
    abstract classes: can have abstract methods or non-abstract methods
    Interface: only abstract methods: default Modifier: public abstract

                                    B)类与类/类与接口关系的区别
                                        1>类与类之间的关系:
                                                    继承关系,只支持单继承,不支持多继承,但是可以多层继承
                                        2>类与接口的关系:
                                                    实现关系,一个类继承另一个类的同时,还可以实现多个接口...
                                        3>接口与接口的关系:
                                                    是一种继承关系,既可以单继承,也可以多继承...
                                                    接口1 extends 接口2,接口3...
                                    C)设计原则的 区别
                                        1>抽象类: 是有继承关系---->体现的是一种"is a"的关系   (A是B的一种或者B是A的一种)
                                        2>接口:类和接口:实现关系--->体现的是一种" like a"的关系(扩展功能)  :跳高猫 像猫

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324595210&siteId=291194637