Java encapsulation, inheritance, polymorphism learning

1. Generic

  1. Why use generics?

    When there is no generic type, any type can be added to the collection, which is not safe; after each value, type coercion is required, which is cumbersome.

  2. Core idea : limit the content of a collection to a specific data type, note: generics can only use reference data types

  3、

    (1) Use generics in collections (master)

      --1: A generic class is declared, but generics are not used, and an object type is returned (generics are available, but cannot be used).

      --2: When instantiating a generic object, you need to specify the type of the generic.

      --3: After specifying, all generic positions in the corresponding type become the generic type specified in the instantiation.

      --4: When customizing the generic type, the generic type is not used during instantiation, and the default is the object type.

      --5: Static methods cannot use class generics.

      --6: If the generic class is an interface or abstract class, you cannot create an object of the generic class.

      --7: Subclasses are derived from generic classes, and generic types need to be concreted.

    (2) Custom generic classes, generic interfaces, generic methods (understanding)

Two, the four basic attributes of the class

  private、public、protected、default。

  

          The same class and the same package subclass anywhere
 private     yes no no no

Default yes yes Same package yes no
                                 different package no   
protected yes yes yes no

public yes yes yes yes yes
    
yes means yes, no means no
The question of whether basic properties are inheritable

3. Inheritance

  (1) Meaning: The subclass inherits the attributes and behaviors of the parent class, and can expand new attributes and behaviors according to its own needs, improving the reusability of the code, the keyword extends .

    eg: Class A extends B{}, class A is a subclass, and class B is a parent class, base class, and superclass.

   super(): Represents the memory space designation of the parent class.

   this(): A reference to the current object.

  (2) Method overloading and rewriting

    --Overloading: In the same class , the same name, different parameter lists, note: the overloading of the method has nothing to do with the return value of the method and the modifier, the constructor can be overloaded.

    --Rewrite: The premise is that on the basis of the inherited class , the subclass can "refactor" the method with the same name in the parent class after obtaining the structure of the parent class. Rule: The return value of the method, the method name, and the formal parameter list are the same. The subclass's permission modifier is not less than the superclass's method of the same name.

    --When the same method appears in the child parent class, the method in the child class will be run first.

    --Static methods can be inherited, but not overridden.

4. Packaging

  (1) Meaning: combine the properties and behaviors (methods) of an object into an independent whole, and hide the internal implementation details of the object as much as possible. In a class, properties are generally decorated with private, and getter and setter methods are used as interfaces to operate with properties.

5. Polymorphism

  (1) Meaning: The specific code bound when the program is running can be changed without modifying the program code, so that the program can choose multiple running states; the specific implementation method is: interface implementation, inheriting the parent class for method rewriting, the same Method overloading in a class can be understood as multiple manifestations of a thing.

  (2) The premise of using polymorphism: class inheritance, subclass overriding the parent class method, parent class reference to the object of the subclass.

  (2) Note:

    --1: When the method in the parent class is only defined in the parent class and not overridden in the child class, it can only be called by the reference of the parent class type.

    --2:对于父类中定义的方法,如果子类中重写了该方法,那么父类类型的引用将会调用子类的这个方法(动态绑定)。静态绑定:static,final,private被修饰的,会在编译是解析,因为编译器JVM知道他们不会被重写。

    --3:对于多胎来说,编译时,看“左边”,将此引用变量理解为父类的类型,执行时看“右边”,关注真正的实体子类对象,子类的对象执行的方法就是子类重写的。

    --4:向上造型:父类  A = new 子类B();向下造型:子类B = (子类)父类对象A,用instanceof判断当前对象是不是一个类的实例。

    --5:子类对象的多态性,并不使用于属性。(属性是不能被重写的)。

六、static关键字

  static关键字修饰属性:

  1、非静态的属性,根据每个实例加载一套。

  2、静态的属性,所有的实例公用一个静态域属性;任何一个实例对其修改,会导致其他对象的也改变。

  3、有对象后,可以用"对象.类变量"使用。但是"类.实例变量"不行。

  4、类变量是存储在静态域中的。

  static关键字修饰方法:

  1、随着类的加载,在内存中也是独一份的。

  2、可以直接通过"类.类方法"的方式调用。

  3、静态方法只能调用静态属性和静态方法,不能调用实例变量。

  4、静态方法也不能调用this()和super()。

七、final关键字

  final,finally,finalize()。

  final关键字可以修饰类、属性、方法为“最终的”,不能更改了。

  1、final标记的类不能被继承。

  2、final标记的方法不能被重写。

  3、final标记的变量即为常量,且只能赋值一次,默认初始化不能被使用。

 

Guess you like

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