201521123040 Java Programming 6th Week of study summary

Source: Internet
Author: User
Tags event listener

1. This chapter studies the summary 1.1 object-oriented learning temporary, use the mind map, to encapsulate, inherit, polymorphism as the core concept of a mind map, the object-oriented thinking of a summary.

2. Written work cloning method
1.1 Object in the clonemethod is modified by protected, what do you need to be aware of when overriding the Clone method in your custom class?

(1) The protected-modified method only allows its subclasses to be called, while the outer class is not allowed to invoke the method.

(2) using the Clone method of the object class, the name of the method in the custom class is the same as the name in Object.clone.

(3) The return value must be the same as the return value of the overridden method

1.2 When you design your own class, what kind of method is generally used to modify the protected? Take the job shape as an example to illustrate.

When you want to restrict access to friends and subclasses, and not allow other external classes to access, you can use the protected adornment, which also protects it from being called by other classes.

Reading the Job shape code, you can see that Getperimeter and Getarea two methods to calculate the circumference and area, is its and its children's characteristics, and should be used for its and inherit its children class and not for other classes to call, so with protected decoration.

1.3 Write simple in the Test1 package EmployeeClass, create a new one in the Test2 package TestProtectedclass, and in main, try to invoke the Clone method of the employee in the Test1 package to clone a new object, can it be successful?

Cannot succeed. First two classes are not in a package, and Test2 does not have an import test1 package, so the class cannot be obtained, and the class TestProtected should inherit the employee class to invoke its methods.

2. Using an anonymous class with a lambda expression to overwrite a set of objects 2-advanced-polymorphic interface topics for internal classes 5-2

public static void Main (string[] args) {//Overwrites inner class as internal anonymous class comparator<personsortable2> namecomparator= (  PersonSortable2 O1, PersonSortable2 O2)->{if (O1.getname (). CompareTo (O2.getname ()) >0) {return 1;  }else if (O1.getname (). CompareTo (O2.getname ()) <0) {return-1;  }  else {return 0;  }; Arrays.sort (person,namecomparator);}

  

  

3. Analyze the following code to answer what does Shapecomparator point to the object that is related to the comparator interface?
comparator<shape> shapecomparator = new  comparator<shape> () {       @Override public       int Compare ( Shape O1, Shape O2) {           //Your Code           }         };  

  

The object that Shapecomparator points to implements the Compatator interface and implements the Compare method in its anonymous inner class (the actual procedure is overridden) so that the abstract method can be called by Collection.sort.

Event handling in 4.GUI 4.1 writes out the most important keywords in the event-handling model.

event, event source, event listener.

4.2 Use code and annotations to prove that you understand the event-handling model.
    public static void Main (string[] args) {            btn = new JButton ("This is a btn");//Create a button to listen          for events//implement listener interfaces and write listener binding events 
   btn.addactionlistener (new ActionListener () {            @Override public            void actionperformed (ActionEvent e) {                System.out.println ("Hello World");             }        );    }      

  

Experimental summary

1. When using an internal class, you need to create an external class before implementing it.
2. The inner class can be static, or public,default,protected and private adornments are available.
3. The method name of the inner class can be the same as the external class.

Code on the cloud on the submission of records and PTA

201521123040 Java Programming 6th Week of study summary

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.