201521044091 Java Programming 5th Week of study summary

Source: Internet
Author: User
Tags comparable

1. This week's study summary 1.1 attempts to summarize knowledge points about polymorphism and interfaces using mind mapping.

1.2 Optional: Summarize other lessons using the usual methods. Interfaces can define behavior, but they cannot define operations;the abstract method modifies methods that may change. 2. Written work 1.1 com.parent package Child.java file can be compiled through? Which sentence will be wrong? Try to correct the error. and analyze the output results.
Package parent;PublicClassoutofparentpackage{PublicStaticvoidSHOWPARENTJ (Parent p) {System.Out.println (P.J); System.Out.println (P.geti ()); P.GETJ (); }}Classparent{Privateint i=1;Protectedint j=2;ProtectedIntGeti (){return i; }PublicvoidGETJ () {System.Out.println (j); }}Classother{PublicStaticvoidSHOWPARENTJ (Parent p) {System.Out.println (P.J); System.Out.println (P.geti ()); } }PublicClassChildExtendsparent{PublicStaticvoid main (String[] args) {Parent p = Span class= "Hljs-keyword" >new Parent (); Child C = new Child (); C.getparenti (); C.getparentj (); OTHER.SHOWPARENTJ (P); } public void getparenti ( Out.println (i); } public void GETPARENTJ ( Out.println (SUPER.J); System. out.println (j); System. out.println (Geti ()); System. out.println (Super.geti ());}          
答:编译通过不了,System.out.println(i);这一句有错误。修饰符private并不能使得子类访问父类,故需要改成protected。

The output result is

1.2 Outofparentpackage.java In another package, can the compiler pass? What errors are prompted? Analyze the cause. How can I change it to make it compile properly? (It doesn't matter if you can't write the correct answer, but be sure to write the thinking process.)
答:另外一个包中的OutOfParentPackage.java,不能编译通过。
(2) Abstract Advanced: Reading Guessgame abstract class design and use of source code 2.1 guess the pre-transformation code is simple, and the modified code uses abstract classes, abstract methods look very complex, then what is the benefit of such a transformation?
答:这样有利于程序的可扩展性,
2.2 What should I do if I want to transform this game into a graphical interface?
答:设计一个图形界面的子类去继承前面的抽象类;
2.3 Combining This example, when do you think you should use abstract?
答:只知道具体的类,而不知道具体的方法。或者说是方法只有声明,但没有确切的定义。
2.4 Important: In this case, what is the change and what is the same? Attempt to combine abstract, inheritance and other concepts to illustrate.
答:(1)变化的是是可使用的环境,改造前只能在控制台输出,改造后除了在控制台,海能在对话框图形界面,web等地方输出。
(2)不变的就是这个抽象类。
(3) Comparable and Comparator3.1 describe the purpose of the comparable interface. Why does a class implement the comparable interface so that it can be sorted directly using Arrays.sort?
答:Comparable接口对实现它的每个类的对象进行整体排序。实现此接口的对象列表(和数组)可以通过Arrays.sort进行自动排序。实现此接口的对象可以用作有序映射表中的键或有序集合中的元素。

Comparable接口有compareTo一个方法,覆盖这个方法就能排序了,直接调用Arrays.sort就可以对元素进行排序,省去了很多繁琐的排序代码。
3.2 Why do I need a comparator interface with the comparable interface?
答:Comparable接口和Comparator接口,两个都是用来实现集合中元素的比较、排序的。Comparable是在集合内部定义的方法实现的排序,
Comparator是在集合外部实现的排序,Comparable接口是一个对象本身已经支持自比较所需要实现的接口。Comparator是一个另写一个外部类来实现的比较的接口。
(4) interface-oriented case analysis, read Case-studentdao.zip case 4.1 draw a class diagram describing the role of each class and interface.

答:Student是一个包含名字属性的类。StudentDao接口中包含writeStudent和readStudent以及diplayAllStudent方法。StudentDaoArrayImpl是对StudentDao接口的操作,其中定义了方法Student readStudent(String name) 和boolean writeStudent(Student student) 以及void diplayAllStudent()。StudenDaoListImpl是对StudentDao接口的另一个操作,也包含了对方法Student readStudent(String name) 和boolean writeStudent(Student student) 以及void diplayAllStudent()的定义。
What is the difference between 4.2 Studendaolistimpl and Studentdaoarrayimpl?
答:StudenDaoListImpl使用动态容器ArrayList,而StudentDaoArrayImpl使用的是固定了大小为80的数组。
(5) What is interface-oriented programming? What are the benefits of interface-oriented programming? Discuss the analysis in conjunction with topic 3 and 4 (Test.java code). Do not copy Baidu intact! 答:题目3种采用Comparable和Comparator接口,调用Arrays.sort方法实现排序;Comparable定义在类的内部,Comparator定义在类的外部。
      • Benefits: Can make code logic clear, code understandable, easy to expand, maintainability strong;
        Interface similar function, we just write the function well, do not care who want to use, this can improve efficiency, later according to the specific requirements implements this interface is good.
3.1. Code Cloud codes Submission record

3.2. PTA experiments require a deep understanding of the concept and definition of interfaces. Experimentation is not easy, and it takes a lot of data to get it done.

201521044091 Java Programming 5th 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.