Cattle off network do problems 5.28

(1) In Java, the class can also define a number of methods of the same name, the number of parameters in the form of these methods, the sequence type or different, the return value may not be the same. This feature is called object-oriented programming 

Overload

Method overrides:

In a subclass, phenomena and methods of the parent class exactly the same statement appear.

Method overloading:

The same class, the same method name appears, the list of parameters different phenomena.

 

Method overloading change the return type, and because it is independent of the type of the return value.

 

Override: method overrides

Overload: Method overloading

Difference (2) concerning final, finally, finalize the

final

Modifier (keyword) if a class is declared final, meaning that it can not send another unexpected new sub-class can not be inherited as a parent. Thus, a class can not both be declared abstract, it has been declared final. The variable or method declared final, can be changed to ensure that they are not in use. Is declared as final variables must be given in the initial statement, a reference in the future can only read, can not be modified.

 

finally

Providing finally block to perform any cleanup exception handling. If an exception is thrown, then matches the catch clause will be executed, then control will enter the finally block (if any). Exception handling block is generally required.

Method name. Java technology allows the use of finalize () method to remove the object to do the necessary clean-up work before going out from memory in the garbage collector. This method is determined by the garbage collector in this object is not called when this object is referenced. It is defined in the Object class, so all classes inherit it. A subclass overrides the finalize () method to organize system resources or perform other cleanup work. finalize () method is to delete the object before the garbage collector calls this object. 

All Java classes inherit finalize () method from the Object class.

When the garbage collector (garbage colector) recovering an object determined, the object will run finalize () method.

(3) java idea of ​​using a local priority. Local variables and member variables can be the same as when using the call identifier, priority use of local variables

(4) using the final member variables

亲自测试了一下,类的final成员变量必须满足以下其中一个条件

 1、在构造函数中赋值

 2、初始化赋值

(5) If you need to define a class, the following modifiers which are allowed to be used

This problem only say that the definition of a class, but did not say this is ordinary external or internal class category.

Because the general category that is outside the class, through eclipse warning "Illegal modifier for the Test at The class; only  publicabstract final  are permitted" it can only be seen with the public, abstract and final modifications.

Internal class you can use  a modified internal modified class member variable modifiers, such as  Private, static,  protected modification.

 

(6) In Jdk1.7, the difference between the following statement in contact with the abstract class interface to correct

An interface (interface) can be said to be a special case of the abstract class, all methods in the interface must be abstract. The method defined interface defaults to public abstract type member variable type interface defaults to public static final. Further, interfaces and abstract classes differ in the method:    

1. An abstract class can have a constructor, interface can not have a constructor.  

2. An abstract class may contain ordinary non-abstract methods, all interfaces are abstract must not have non-abstract ordinary method.

3. An abstract class can have an ordinary member variables, the interface is not an ordinary member variables 

4. Access Type abstract methods abstract class may be public, protected, and default type

The abstract class may contain static methods, static methods can not contain an interface

6. abstract classes and interfaces can contain static member variables, static member variables of type of access abstract class is arbitrary, but variables defined in the interface can only be public static final type, and the default is the public static final type

7. A class can implement multiple interfaces, but can only inherit an abstract class. In both applications are also quite different: more interfaces play a role in the system architecture design method, used primarily for communication between the contract definition module. The abstract class play a role in implementing aspects of the code, code reuse can be achieved, for example, Template Method design pattern is a typical application of abstract class, assuming that all the Servlet class for a project should be carried out to determine the permissions the same way, record access logs and exception handling, then you can define an abstract base class, so that all the Servlet inherit the abstract base class, complete authority to determine, record access logs and exception handling code in the service method abstract base class, in each sub only class of their respective business logic code.

(7)

wait (), notify () and notifyAll () is the  Object class  method

From the text of the three methods described can know the following information:

1) wait (), notify () and notifyAll () method is a local method, and a method for the final and can not be rewritten.

 

2) call to wait an object () method allows the current thread to block and monitor the current thread must own this object (ie lock)

 

3) call an object's notify () method can be a wake-up is waiting for this object's monitor thread, if there are multiple threads waiting on this object's monitor, you can only wake up one thread;

 

4) call notifyAll () method can awaken all are waiting for the object monitor thread;

 

A friend may be in doubt: Why these three methods is not the Thread class declaration, but declared in the Object class method

 

(Of course, due to the Thread class inherits from the Object class, so the caller can Thread three methods)? In fact, this Q

 

The question is very simple, because each object has a monitor (ie lock), so let the current thread to wait for a lock of an object, of course,

 

With this object to be operated. Rather than operate with the current thread as the current thread may wait for multiple threads

 

Lock, if operated by a thread, it is very complicated.

 

As already mentioned, if you call an object's wait () method, the current thread must own this object's monitor (ie,

 

Lock), so the call wait () method must be performed (or sync blocks in the synchronized block synchronization method or

 

synchronized方法)。

 

Call an object wait () method, the equivalent of the current thread surrender monitor this object, and then enter the wait state,

 

Wait for the follow-up to obtain this object again lock (sleep method of the Thread class in the current thread suspended for some time, from

 

And let the other threads have the opportunity to continue to implement, but it does not release the object lock);

 

notify () method can be a wake-up is waiting for the object monitor thread, when there are multiple threads waiting on this object

 

The monitor, then wake up only one thread, which thread specific wake-up is not known.

 

Likewise, calling an object notify () method, the current thread must own this object's monitor, so calling

 

notify () method must be performed (block synchronized or synchronized method) or in a sync block synchronization process.

 

nofityAll () method can awaken all are waiting for the object monitor thread, which is the notify () method is different.

Condition is only appeared in java 1.5, which is used to replace the traditional Object's wait (), notify () to achieve cooperation between the threads, compared to the use of Object wait (), notify (), using Condition1 the await () , signal () in this way to achieve cooperation between threads more secure and efficient. Therefore, it is rather generally recommended Condition, blocking queue in a blog post that tells you to, in fact blocking queue is used to simulate Condition collaboration between threads.

  • Condition is the interface, the basic approach is the await () and signal () method;
  • Lock Condition depends on the interface, generate a substantially Condition codes are lock.newCondition ()
  • Condition await the call () and signal () method, have to be protected within the lock, that is to say it must be used in Conditon await between Lock.lock () and lock.unlock () corresponding to the wait Object () ; for condition Condition the signal () corresponding to the Object notify (); signalAll Condition in () corresponds to notifyAll Object ()

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41673515/article/details/90643129