Dark Horse Programmer Java SE Review (2)

Source: Internet
Author: User
Tags define abstract java se

----<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Two Uses of 1.this: (1) when the name of the member variable and local variable is the same, you can use the keyword this to differentiate. This represents the object, which represents the reference to the object to which this function is located (which object calls the function where this is located, which object is represented) (the member in a class must have an object call if it wants to be executed.) except static) (2) This can be used to call other constructors in the constructor (note: Only the first row of the constructor can be defined, because initialization must be performed first)

Features of the 2.static keyword: (1) The static modifier, which is used to decorate members (member variables, member functions) (2) Static decorated members are shared by all objects

3.static takes precedence over the existence of an object because static members already exist as the class is loaded. Static decorated members are called in a way that can be called directly with the class name. Static members. Static decorated data is shared data and objects are stored in unique objects

4. Member variables (also called instance variables) are compared to static variables (also called class variables): (1) Two variables have a different declaration period: the member variable exists as the object is created, and is released as the object is recycled. Static variables exist as the class loads, disappearing as the class disappears. (2) The invocation method is different: the member variable can only be called by the object, the static variable may be called by the object, can also be called by the class name (3) The alias is different: The member variable is also called the instance variable; Static variables are stored in the method area (static area), so it is also called the shared data of the object

5. Precautions for static use: (1) static methods can only access static members (static variables, static functions. Static first in (with the load of the class exists), non-static after (with the creation of the object exists), first in the not accessible after the, after the can be accessed first in, that is, static can not access the static, but non-static access to statically many, can also access non-static (2) The This or Super keyword cannot be used in a static method (3) The main function is static.

6. There are two ways to invoke a method: Class invocation, method invocation.

7. Methods are divided into static and non-static zones. All members of the non-static zone have a This belongs, because the non-static zone can only be called by the object. The static zone belongs to the class name. The class name is omitted before the static member, which is omitted before static.

8. When do I use static?

(1) Static variables: When the values of the member variables in the Analysis object are the same, this member variable can be statically decorated, as long as the data is different in the object, that is, the object's unique properties, must be stored in the object, is non-static.

(2) Static function: Whether the function is static decoration, the reference point is whether the function has access to the object's unique properties, simple point of view from the source code, whether the function needs to access non-static member variables, if necessary, the function is non-static, if not required, you can define the function as static, Of course, it can also be defined as non-static. (objects are encapsulated with unique data, if the method does not have access to the unique data, the creation of the object is meaningless, there is no access to the unique data, you can define the function as static, directly with the class name can be called)

(3) Static code block: Executes as the class is loaded and executes only once.

Function: Used to initialize a class (note: Not all classes are initialized by constructors, and some classes do not need to create objects to perform initialization operations.) If the class is full of static members, the class does not need the object, and the class is initialized with static blocks of code.

(4) Constructing code blocks: All objects can be initialized. (with the versatility of object initialization)

(5) Constructor: A targeted initialization of the corresponding object (with initialization for a specific object)

(6) Local code block: defining the declaration period for local variables

1 /**2 * Requirements: Write a piece of code to verify the static code block, code block, constructor function3 * 1. Order of execution when initializing objects4 * 2. The order of execution when invoking a static variable of the class directly with the class name5  * @paramargs6  */7 //execution Order: Static code block ------constructor8  Public classTestfirstdemo {9 Ten      Public Static voidMain (string[] args) { One          A         NewStaticcodeconstructor ();//Initializing Objects -String haha =staticcodeconstructor.test; - sop (haha);  the     } -     //Redefining Print Functionality -      Public Static voidsop (Object obj) { - System.out.println (obj); +     } - } +  A classstaticcodeconstructor{ at     //Static code block -     Static { -System.out.println ("Static code block--"); -     } -     //code block -     { inSystem.out.println ("code block--"); -     } to     //constructor Function + Staticcodeconstructor () { -System.out.println ("constructors--"); the     } *     //define a static variable $      Public Static FinalString test = "Static variable";   Panax Notoginseng}

9. Execution order: Static code block---Construction code block--constructor

10.23 Design Patterns: The effective solution to the problem is a problem-solving idea.

The problem with the singleton design pattern is that you can guarantee the uniqueness of a class in memory.

When you must use the same configuration information object for multiple programs, you need to ensure that the object is unique, so how do you guarantee the uniqueness of the object?

(1) Do not allow other programs to create this class object with new

(2) Create an instance of this class in this class

(3) To provide a method for other programs to obtain the object

11. Singleton Mode Steps: (1) Privatization the constructor of the class (2) Creates a class object in this class (3) by using new to define a public method that returns the Created object

12. Examples of single cases:

1  Public classSingle {2     Private Static FinalSingle S =Newsingle ();//Create a Class object in this class with new3     PrivateSingle () {};//privatize the constructor for this class4      Public Staticsingle getinstance () {//defines a public method that returns the created object5         returns;6     }7 }8 9 classsingledemo{Ten      Public Static voidMain (string[] args) { One //a hungry man or lazy- type A     } -}

13. Inheritance: (extract)

Java only supports single inheritance

Single inheritance: A subclass can have only one direct parent class

Multiple inheritance: A subclass can have multiple direct parent classes (not supported by Java, but improved), not directly supported because of the uncertainty of the call when there are the same members in multiple parent classes

14.java supports multiple inheritance (multi-layer inheritance is the inheritance system), when it is necessary to use an inheritance system (1) to view the top class of the system, to understand the basic function of the system (2) to create the most sub-class object in the system, complete the call

15. Use this to differentiate between a member in this class and a local variable with the same name (this represents a reference to this class of objects)

When a member variable with the same name in a child parent class is distinguished by Super, Super represents the parent class

When a member function occurs in a child parent class, the subclass function is used first, which is called overwrite.

A subclass cannot call directly a private property member in the parent class, and if the parent provides a method externally, the subclass can be called indirectly through the super. Method name.

16. Two features of the function: (1) overloading (overloading): overwriting (overriding overriding) in the same class (Overrides): In subclasses, it is necessary to have an inheritance relationship

17. Overload: Can change the return value type, formula: Two, three different that is the same class, the same method name, parameter list (method signature) number, type, order of different

18. Overwrite (override) Precautions:

(1) When a subclass method overrides a parent class method, the child class permission must be greater than or equal to the parent class permission

(2) Static subclass can only overwrite static parent class

19. Overwrite preserves the parent class feature and implements subclass-specific content. So when should you use the overwrite operation:

When a subclass is extended to a class, the subclass needs to preserve the function declaration of the parent class. However, to define the unique content of the feature in a subclass, you should use overrides to do so.

20. When a subclass constructs an object, it discovers the constructor that accesses the child class, and the parent class is also accessed. The reason for this is that there is an implicit super () in the first row of the constructor of the subclass, which is the default

21. The instantiation process for subclasses will default to access the Null parameter construction method of the parent class. If the subclass wants to access the parent class's constructor method with a parameter, write the super (parameter) manually in the subclass construction method

22. Why do I need to access the constructor of the parent class when the subclass is instantiated?

The super statement must be defined in the first row of the subclass constructor, because the parent class initializes the action to be completed first.

23. Are there constructors in abstract classes? is used to initialize the subclass object.

24. Can you not define abstract methods in an abstract class? Yes, but it's rare to keep the class from creating objects

25. What keywords can abstract keywords not coexist with?

Private reason: Abstract method to Quilt class method overrides, if private, hidden method, the subclass method cannot complete overwrite

Static reason: If the member becomes static, the object is not needed, and the abstract class does not need the object, and the class name can be used directly. Method name (), abstract method body run meaningless because no content

Final reason: The final function is to modify the variable: to indicate that the variable is not modifiable; The Adornment method: Indicates that this method cannot be overridden (overwritten); The decorated class: Indicates that this class cannot be inherited. Abstract modifies an abstraction method that requires subclasses to implement its abstract methods. Therefore not to be used with final

26. Characteristics of abstract and general classes:

The same point: both abstract and generic classes are used to describe things, and members are defined internally

Different: (1) The general class has enough descriptive information; Abstract classes describe things that may be insufficient information

(2) Abstract methods cannot be defined in general classes, only non-abstract methods can be defined, abstract methods may be defined in abstract classes, and non-abstract methods may be defined.

(3) The general class can be instantiated; Abstract classes cannot be instantiated

27. The abstract class must be a parent class? Yes, you can instantiate a child class only if it needs to overwrite its method.

28. The class is an inheritance relationship with the class, the implementation relationship between the class and the interface, an inheritance relationship between the interface and the interface, and the ability to inherit multiple

Implementation: The methods in the interface are not implemented, they have to be implemented by themselves

29. While inheriting another class, one class can also extend the functionality to implement multiple interfaces, which avoids the limitations of single inheritance.

30. What is the difference between an abstract class and an interface?

(1) Abstract classes need to be inherited, and can only be inherited. Interface needs to be implemented, and can be implemented more, that is, to implement multiple interfaces

(2) Abstract classes can be defined abstract methods and non-abstract methods, subclass inheritance can directly invoke non-abstract methods, the interface can only define abstract methods (that is, all of the interfaces are not implemented methods), subclasses must implement their methods, or subclasses can only be defined as abstract class

(3) The abstract class is an inheritance, is a relationship, is the definition of the basic common content of the system (extracted); Interface implementations are like a relationships, which is the additional extension function of the definition system

31. Polymorphism (An object, multiple forms), embodied in code: a reference to a parent class or interface to an object of a subclass

32. Polymorphism Benefits: Improved code extensibility, pre-defined code later available

Cons: Pre-defined content, cannot use (invoke) the content of later subclasses

33. Polymorphism Premise: (1) must have relations, inheritance or implementation of the relationship (2) to have coverage

If there are errors, please correct me! Thank you

Dark Horse Programmer Java SE Review (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.