Java object-oriented programming Chapter 51-9

1. What are the main features of object-oriented?

Three wherein: encapsulation, inheritance, and polymorphism .
Packaging : packaging refers to the properties and behavior of something into an object, the object is only made public properties and behavior need to be exposed, and this announcement may also selectively released to other objects.
Inheritance : is the child object can inherit the properties and behavior of the parent object, that parent object has properties and behavior of its children will have these attributes and behaviors.
Polymorphism : refers to allow objects of different classes respond to the same message. Polymorphism flexible language, abstract, behavior sharing, code-sharing advantages of a good solution to the problem of the application function of the same name.

2. The package is how to achieve?

Package information object is hidden inside the object, properties and methods to directly access the inside of the external objects is prohibited.
java package class achieved in three steps:

  1. Modify the Visible property to restrict access.
  2. The method of setting the read attribute.
  3. In the read attribute method, adding restrictions on the read attribute.

3. What how the interaction between objects? Action conditions?

By combining packaging and design, having a " high cohesion, low coupling characteristics" is. For example, A has a class referenced class B, then examples of A can be seen as an object instance of the object B combination.

What are the characteristics 4.protected modifier?

The protected modifier, major reference books will say this: access within the class, and subclasses within the package, so methods defined in the parent class member variables and is protected if modified, any access to the same package, but only the Father subclass public class declaration when different packages can also be accessed.

What methods 5.Object have?

ObjectC ± »å ç ???? ?? ?? ° ½æ

1.Object clone()

The current object cloning , protection methods to achieve the object of a shallow copy, only to achieve the Cloneable interface can call this method, otherwise throw a CloneNotSupportedException.

Mainly JAVA in addition to the eight basic types of transmission parameters are passed by value, the other class objects pass parameters are passed by reference, we sometimes do not want to talk about change in the method parameter, this is the class you need to clone replication method.

2.Class getClass()

Obtain the current class object

3.String toString()

Resulting string representing the object , sub-class has generally covered.

4.void finalize()

Object is used when released , because it can not determine what the method is invoked rarely used.

5.Boolean equals()

Determining whether two objects point to the same reference, which is a common data type, parameters can not

6.int hashCode()

An integer object to be represented , in this application is running integer remain unchanged

7.void wait()

Applied thread synchronization of threads waiting

wait is to make the current thread wait for a lock of the object, the current thread must be the owner of the object, that is, has a lock on the object. wait () method waits until the lock is obtained or is interrupted. wait (long timeout) setting a timeout interval, if a lock is not obtained within a predetermined time period returns.
After calling this method the current thread to sleep until the following events occur.

  1. Other thread calls the notify method of the object.

  2. Other thread calls notifyAll method of the object.

  3. Other thread calls interrupt interrupt this thread.

  4. Time interval to.

    At this time, the thread can be scheduled, if it is interrupted, then throw an InterruptedException exception.

8.notify

Thread for thread synchronization in the wake of waiting

9.notifyAll

For thread synchronization of all the threads waiting wake

What is the relationship between general method 6. overloaded?

The same method name, a different number or type of parameters , can be viewed as an overloaded methods are two methods of realization, only return different types, the compiler can not

7. What are the conditions subclass overrides the parent class method requires? Subclass defined with the same name as the parent class must overwrite it?

condition:

  1. Subclass access rights modifier should be equal or greater than the parent
  2. Same name static methods and non-static methods can not cover each other
  3. Method front final modifier , this method can not be overridden in a subclass method
  4. In JDK, many subclasses of the parent class re-cover, given a different meaning, such as the Object class boolean equals (Object obj) Method
  5. If there is an abstract class abstract method , the concrete subclasses must abstract methods covering

Not necessarily:

The method of the parent class and subclass must be an example method, if the parent class is a static method instead of an instance of a subclass, or vice versa will be given.
If the parent class and subclass are static methods, the cover or subclass override inherited methods.

8. A package, what inheritance and polymorphism in object-oriented programming for?

Packages make internal implementation hidden from the user, it is conducive to safe operation, and inherit the common properties of the object can be achieved between the polymorphism closer to people's habits, make the program more convenient.

9. The two design classes and Dis Src, Src packaged in a property of type int (non-negative required), whenever the object changes Src attribute by the particular method, the object can be informed Dis and to obtain the value of this property Src message.


class Dis {
    int val;
    public  Dis(int con_val){
       val = con_val;
    }
    public void monitor() {
        System.out.println("the value of Src has changed");
    }
}

class Src{
    Dis dis;//组合dis
    int value;

    public Src(Dis con_dis) {
        value = con_dis.val >= 0 ? con_dis.val : 0;//保证value非负
        this.dis=con_dis;//Src对象中拥有了Dis对象的引用
    }
    public void valueChange() {
        for (int i = 0; i < 2; i++) {
            int oldvalue = value;
            value += i;
            if (oldvalue != value){
                dis.monitor();//只改变了一次
            }
        }
    }
}

public class test {
    public static void main(String[] args) {
        Dis dis=new Dis(666);
        Src src=new Src(dis);//创建src对象,并将src对象作为参数传入
        src.valueChange();//the value of Src has changed
    }
}

end:

After the supplement has come to change ~ ~ ~

Guess you like

Origin www.cnblogs.com/He-Fan/p/11575976.html