The third week of learning Java

  1. The method of overload (Overload):
    1.1 Method plurality of the same name defined in a class.
    1.2 Requirements:
    1) the name of the same method
    2) different parameter list.
    3) and the access modifier, regardless of the value returned.
    1.3 benefits:
    1) Use the difference shield user convenience.
    You can call different methods based on different parameters passed

  2. Constructor (the Constructor):
    2.1 Concepts: class particular method is mainly used to create objects. For object initialization
    2.2 Features:
    1) name the same as the class name (including the case).
    2) No return type.
    3) When you create an object (when new objects), triggering calls constructor, not in the form of manual call through a period of.
    2.3 Note: If the display is not in the class constructor been defined, the compiler provides a default constructor with no arguments.
    2.4 Note: If the parameters have been manually added through the constructor, no reference is no longer provided by default constructor, add their own demand may be combined. (Recommended, you must manually add a constructor with no arguments)
    2.5 constructor can also be overloaded, as overloading rules and methods

    1. this keyword:
      3.1 this represents the "current instance", that is the current object template, template service with which object, which points to this object.
      3.2 this first usage: calling class instance attributes of the present, example method. For example: this.name, this.run ()
      3.3 the this second usage: constructor call other methods in this class. For example: this (), this (argument). Note: You must first line in the constructor, and must be used in the constructor.
      4. The process of creating an object
      in memory space to open up the
      given initial value for each attribute
      code is executed in the constructor
      [assign objects to a variable address]
      5. Package:
    2. Concept: hidden object as an internal implementation details, and access control permissions to modify objects.
    3. private access modifier, modify attributes, to this class visible effect.
    4. get / set channel is the only external access to the private property, the internal data filtering method. (Can be added in the filter condition set method)
    5. Public access methods to ensure that data can be properly entered.
      5. Data privatization of public behavior
  3. Inheritance:
    Inheritance 6.1 program, it is a gift or get between classes characteristics and behavior.
    Must satisfy the relationship "is a" class of between 6.2 and class.
    Select parent class 6.3: Function finer, more coincident points, closer to the direct parent.
    6.4 abstract parent class: The program needs to use a plurality of concrete classes, common for extraction, and further defined parent class.
    6.5 In the same or a similar group of classes, and the extracted features common behavior, as defined in the parent class, reuse.
    After 6.6 produces inheritance, the subclass can use the properties and methods of the parent class, but also define a subclass of unique properties and methods.
    6.7 = complete sub-class generic parent class + subclass unique
    6.8 benefits: not only improves the reusability of code, but also improves the scalability of the code.
    6.9 Java single inheritance, a class can have only one direct parent, but can be multi-level inheritance, attributes and methods step by step superposition.

    6.10 access modifier: // Other: not in a package, there is no inheritance
    Here Insert Picture Description

    Can not be inherited 6.11:
    1) the parent class constructor, not inherited subclass.
    2) The parent class modified by private members, can not be inherited (not visible).
    3) When parent class members by default modified, not in the same sub-class package, can not be inherited (not visible).

    A method of covering 6.12 / rewritable (Override)
    . 1). When the parent class can not be satisfied the needs of the subclass, the same method can be overridden defined in the parent class and subclass.
    2) requirements:
    . A) method name, parameter list, the return value must be identical to the parent.
    B). access modifiers should be the same or wider than the parent class the parent class.
    3) implementation mechanisms: the subclass overrides the superclass method, a method version takes precedence subclass coverage.

  4. super keyword:
    7.1 The first usage: in a subclass, you can ". super" form to access the properties and methods of the parent class, solve certain attributes shelter, a member of the parent class method call coverage problems.
    7.2 The second Usage: super () represents the first line of the subclass constructor, call the no-argument constructor of the parent class. Do not write and write hidden by default in the sub-class constructor in the first line.

  5. Objects created in inheritance:
    8.1 inheritance, when building a subclass of objects, will first build the parent object.
    8.2 by the "common parent" + "subclass unique" into a complete sub-class objects.

  6. Create objects in inheritance process:
    9.1 Construction of the parent object (parent class object initializes itself attribute code execution logic configured method)
    9.2 initialize itself attributes
    9.3 code itself performs logical configuration method

  7. NOTE:
    10.1 if the subclass constructors, no display definition super () or super (argument), the default provider super ().
    10.2 the same sub-class constructor, super (), this () can not coexist.

11. Polymorphic:
more than 11.0 states meant allowing different types of objects (sub-objects of the same class the parent class) respond to the same message. I.e. the same message can be transmitted according to different objects and many different behavior. (Send message function is invoked) variables of the same type (the type of the parent class) In the same manner different call (subclass rewrite the parent class) Results.
11.1 concept: references to the parent class subclass object, resulting in a variety of forms.
11.2 premise polymorphic configuration, must have a direct or indirect inheritance relationship between the two, reference may be directed to the parent class subclass object, thereby forming a polymorphism.
11.3 refers only to the parent class can call the parent class properties and methods declared, can not call the subclass unique properties and methods.

11.4	多态两种应用场景:
	1).	场景一:使用父类作为方法形参,实现多态

When you call a method, argument types can be transmitted include: the type of object + present all of its subclasses the object.
2) Using the method of the parent class as the return value, achieve polymorphism.
When a method is available types include results: This type of object + object is all of its subclasses.
12. The boxing and unboxing:
1) Packing: The parent class references stored in the real subclass object, called the upward transition (polymorphic core concepts).
2) Unpacking: The parent application objects of the real subclass, the subclass itself strong back type called downcast.
3) Note: downcast by the target if the type of the parent class subclass object references do not match, the type of conversion exception occurs. a java.lang.ClassCastException
13. instanceof keyword:
1) Syntax: instanceof parent class reference type (returns the result of the boolean type)

总结:
	1).	多态的两种应用场景:
		a).	使用父类作为方法形参,实现多态。
			调用方法时,可传递的实参类型包括:本类型对象+其所有的子类对象。

		b).	使用父类作为方法返回值,实现多态。
			调用方法后,可得到的结果类型包括:本类型对象+其所有的子类对象。

	2).	多态的作用:
		a).	屏蔽子类间的差异。
		b).	灵活、耦合度低。

	时间与空间的平衡、效率与安全的平衡

two. Abstract:
program among which objects should not be new?
Animal Vehicle (transportation) new Vehicle (
parent: incomplete, not specific enough, should not exist independently, how to solve the modification by abstract class, which means an abstract class, not new target?

1.	abstract的意思:抽象的,似是而非的,像,却又不是,具备某种对象的特征,但不完整。
2.	abstract修饰类,意为“不够完整、不够具体,不该独立存在”
	I.	即为抽象类,不能独立new对象。
	II.	可被子类继承,对子类提供共性的属性和方法。
	III.	可声明引用,更纯粹的使用多态。
	IV.	抽象类的构造方法的作用:构建子类对象时,先构建父类对象,由父类共性+子类独有组成完整的子类对象。

Abstract modified method
1). There is no way the body even abstract method the braces are not only declared part of the semicolon ending
2) must have an abstract method of class is an abstract class, abstract classes do not necessarily have an abstract method
3) abstract method must be in subclasses override the abstract method
3. summary:
I. modified abstract class: not new objects, but can declare references.
. II abstract modification methods: only the method declaration, there is no way to achieve. (Need to include in an abstract class)
III. Abstract classes are not necessarily the abstract method, but the method must abstract class is an abstract class.
After IV. Subclass inherits the abstract class, you must override the parent class among all the abstract methods, abstract classes or subclasses otherwise.

Released four original articles · won praise 0 · Views 215

Guess you like

Origin blog.csdn.net/weixin_44077166/article/details/104445214