What is the goal of three kinds of modifier?

What is a modifier?

  • The role of modifier on the original component to add some extra features, there are many modifiers, different modifiers that can be modified components exhibit different characteristics;
  • A plurality of modifiers may be used together, the performance characteristics of the assembly will continue in accordance with the number of superimposed modifier.

abstract What can be modified?

  • It may be modified classes, methods, modified class call abstract class, the modified method is called abstract methods

How to define an abstract class?

  • Add the abstract modifier in front of the class keyword:
    abstract modifier

Abstract classes What are the characteristics?

  • You can declare a reference can not create object.

How to define abstract methods?

  • Abstract modifier was added before the method returns values, note that the method can not be achieved with a portion "{}":
    Here Insert Picture Description

Abstract methods What are the characteristics?

  • The statement did not only achieve, must exist in the abstract class.

How to use an abstract class?

1, write a class that inherits the abstract class
Here Insert Picture Description
2, all the abstract methods of the parent class abstract modifier added to achieve remove cover portion "{}"
Here Insert Picture Description
3, polymorphic syntax to create an object, invoke methods.
Here Insert Picture Description
Note: If a subclass is not abstract methods of the parent class coverage, then the sub-category also "forced" to become an abstract class.

static What can be modified?

  • Can modify the properties, methods, initialization code block, the modified attribute called static properties, the modified method is called the static method, the modified called static initializer block code block.

How to define a static property?

Static modifier was added before the data type defined attribute:
Here Insert Picture Description

Static properties What are the characteristics?

  • Static property is shared by all objects of property, do not have to create an object can be accessed directly through the class name of the attribute name.

How to define a static method?

  • Static modifier was added before the return value of the method:
    Here Insert Picture Description

Static method What are the characteristics?

1, static methods can use the class name method name called directly;.
2, static methods can not be used this has nothing to do with the object or the super keyword;
3, in a static method static properties and static methods can only access;

How static code block definition?

  • Static modifier was added before the return value of the method:
    Here Insert Picture Description

What are the characteristics of static code block?

  • Static block of code is executed when the "class loader" static properties may be initialized in a static block.

What is the class loader?

  • When using a virtual machine for the first time a class (new object, call a static member, use subclasses), the virtual machine to load the information into memory .class in the subsequent ease of use; when basic information is loaded performs a "static code block "statements.

What final can be modified?

  • Attribute may be modified, local variables, methods, classes; modified variable called constant, steady modified attribute called attribute; modified method final method, modified final class category.

How to define constants?

  • When the local variables defined in the method, the final modifier was added before the data type:
    Here Insert Picture Description

Constants What are the characteristics?

  • Life can only be assigned once and can not be modified (reassigned).

How to define constants property?

  • When defining the properties, data type is added before the final modifier:
    Here Insert Picture Description

Constant Property What are the characteristics?

  • Can only be assigned once, with no default, you must be manually assigned.
    Manual direct assignment:
    Here Insert Picture Description
    Assignment opportunity static property three times, this assignment opportunities for the most basic, the most direct and most commonly used.

How to define the final methods?

  • Final modifier was added before the type of the return value of the method:
    Here Insert Picture Description

The final method What are the characteristics?

  • The final method is not covered by subclasses.

How to define the final class?

  • When you define a class to join final before the class keyword:
    Here Insert Picture Description

The final category What are the characteristics?

  • The final class can not be inherited.

What modifiers can be used in conjunction?

1, access modifier (private / default / protected / public ) can be static, final conjunction.
2, access modifier (private / default / protected) can be used in conjunction with the abstract.
3, static / final can be used in conjunction, it will usually modified together with a public attribute used to indicate a static constant disclosed.

Released four original articles · won praise 6 · views 517

Guess you like

Origin blog.csdn.net/qq_46288317/article/details/104502854