20172319 "Java Programming Tutorial" Week 8 Learning Summary

20172319 2018.04.24-05.03

"Java Programming Tutorial" Week 8 Learning Summary

content


Textbook learning content summary

Chapter 10 Polymorphism:

  • Post-binding:
    Binding refers to the association between the invocation of a method and the class (method body) where the method is located;
    in java, binding is divided into static binding (early binding) and dynamic binding (late binding) ;
    Early binding: The method has been bound before the program is executed (that is to say, it is known in which class the method is in the compilation process), and it is implemented by the compiler or other linker at this time;
    Note: java Among the methods, only final, static, private and constructor are early binding;
    late binding: binding according to the type of specific object at runtime;
    • Pros: great flexibility
    • Disadvantages: low efficiency (judging type process)
  • Polymorphism is achieved by inheritance: a reference variable can point to any object of any class that has an inheritance relationship; but when a method is called, the version of the method (the actual method body of the method) depends on the type of the object.

  • Using interfaces to achieve polymorphism:
    interface names can declare object reference variables

声明接口: public interface Speaker { public void speak(); public void announce(String str); }

声明对象引用变量(可指向实现该接口任何类的任何对象) Speaker current;
   There is a "yes" relationship between the implementing class and the interface;
   when calling a method, what is actually called depends on the type of object pointed to by the interface reference;
   when using an interface reference variable, only the method defined in the interface can be called , if trying to call other methods in the class implementing the interface will generate a compile-time error (the compiler can only determine that the class object is a kind of Speaker);
   appropriate type conversion can achieve the call:

((实现接口的类)接口引用变量).方法名()

   The interface name can be used as the method parameter type, and the class object that implements the same interface can be passed to the method as a parameter.

  • Sorting:
    The process of adjusting a set of elements into an ordered arrangement
  • (1) Sorting by selection method:
    Principle:
    1. Start from the first element, compare with the following elements, and find the smallest element to exchange positions with the first element;
    2. Start from the second element, respectively
    3. Repeat the above steps until all the elements are arranged from small to large .
    example:
待比较数据:7, 6, 9, 8, 5,1
    第一轮:此时指针指向第一个元素7,找出所有数据中最小的元素,即1,交换7和1的位置,排序后的数据为:1,6,9,8,5,7
    第二轮:第一个元素已经为最小的元素,此时指针指向第二个元素6,找到6,9,8,5,7中最小的元素,即5,交换5和6的位置,排序后的结果为:1,5,9,8,6,7
    第三轮:前两个元素为排好序的元素,此时指针指向第三个元素9,找到9,8,6,7中最小的元素,即6,交换6和9的位置,排序后的结果为:1,5,6,8,9,7 
    第四轮:前三个元素为排好序的元素,此时指针指向第四个元素8,找到8,9,7中最小的元素,即7,交换8和7的位置,排序后的结果为:1,5,6,7,9,8
    第五轮:前四个元素为排好序的元素,此时指针指向第五个元素9,找到9,8中最小的元素,即8,交换9和8的位置,排序后的结果为:1,5,6,7,8,9
到此,全部排序完成。
  • (2) Insertion sorting:
    Principle:
    1. Point the pointer to an element, assuming that the elements on the left of the element are all ordered, extract the element, and then compare it with the elements on the left in the order from right to left, When it encounters an element larger than it, move the element to the right until it finds an element smaller than the element or finds the leftmost element and finds that the element on the left is larger than it, and stops;
    2. At this time, a vacancy will appear, and the element Put it into the vacancy, at this time, the elements on the left side of the element are smaller than it, and the elements on the right side are larger than it;
    3. Move the pointer one bit backward, and repeat the above process. For each round of operation, the ordered elements on the left are increased by one, and the unordered elements on the right are decreased by one.
    example:
待比较数据:7, 6, 9, 8, 5,1
  第一轮:指针指向第二个元素6,假设6左面的元素为有序的,将6抽离出来,形成7,_,9,8,5,1,从7开始,6和7比较,发现7>6。将7右移,形成_,7,9,8,5,1,6插入到7前面的空位,结果:6,7,9,8,5,1
  第二轮:指针指向第三个元素9,此时其左面的元素6,7为有序的,将9抽离出来,形成6,7,_,8,5,1,从7开始,依次与9比较,发现9左侧的元素都比9小,于是无需移动,把9放到空位中,结果仍为:6,7,9,8,5,1
  第三轮:指针指向第四个元素8,此时其左面的元素6,7,9为有序的,将8抽离出来,形成6,7,9,_,5,1,从9开始,依次与8比较,发现8<9,将9向后移,形成6,7,_,9,5,1,8插入到空位中,结果为:6,7,8,9,5,1
  第四轮:指针指向第五个元素5,此时其左面的元素6,7,8,9为有序的,将5抽离出来,形成6,7,8,9,_,1,从9开始依次与5比较,发现5比其左侧所有元素都小,5左侧元素全部向右移动,形成_,6,7,8,9,1,将5放入空位,结果5,6,7,8,9,1。
  第五轮:同上,1被移到最左面,最后结果:1,5,6,7,8,9。
  • (3) Comparison of sorting algorithms: The
    two are essentially the same in efficiency, both are n²-order algorithms, but the former performs fewer exchange operations and is better than the latter.

  • Search:
    The process of finding a specified target element in a set of elements
  • (1) Linear search: Start from an endpoint, scan the entire search pool in a linear manner, and finally find or search to the endpoint to find that there is no target element.
  • (2) Binary search:
    when the array is ordered, the efficiency is higher than linear search;
    principle:
    Assuming that the elements in the table are arranged in ascending order, compare the keyword recorded in the middle of the table with the search keyword, if the two are equal, Then the search is successful; otherwise, the table is divided into two sub-tables before and after the middle position record. If the key in the middle position record is greater than the search key, the previous sub-table is further searched, otherwise the latter sub-table is further searched. The above process is repeated until a record that satisfies the condition is found, and the search succeeds, or until the subtable does not exist, and the search is unsuccessful at this time.

  • (3) Comparison of search algorithms:
    Binary search requires data to be sorted, and the choice of search algorithm depends on specific conditions.

  • Polymorphism design:
    Allows inconsistent behavior to be implemented in a consistent way. Classes and objects should be defined more reasonably, and appropriate associations should be established to improve the flexibility of the software.

Back to Contents


Problems and Solving Processes in Teaching Materials Learning

  • Question 1: What are up- and down-transformations?
  • Solution:
    Upcasting:
    Converting the object referenced by the subclass to the superclass type is called upcasting. In layman's terms, it is to convert the subclass object to the superclass object, and the superclass object can also be an interface.
    eg:
public class Animal {
    public void eat(){
        System.out.println("animal eatting...");
    }
}

public class Cat extends Animal{

    public void eat(){

        System.out.println("我吃鱼");
    }
}

public class Dog extends Animal{

    public void eat(){

        System.out.println("我吃骨头");
    }

    public void run(){
        System.out.println("我会跑");
    }
}

public class Main {

    public static void main(String[] args) {

        Animal animal = new Cat(); //向上转型
        animal.eat();

        animal = new Dog();
        animal.eat();
    }

}

//结果:
//我吃鱼
//我吃骨头

   Animal animal = new Cat(); Convert the subclass object Cat to the parent class object Animal. At this time, the method called by the animal reference is a subclass method.
   Note: When upcasting, methods defined separately by subclasses will be lost. For example, in the run method defined in the Dog class above, when the animal reference points to an instance of the Dog class, the run method cannot be accessed, and animal.run() will report an error.
   Advantage: Makes the code more concise.

   eg: There is a group of animals of different species, and the method I want to define them to eat
   does not use upcasting:

public void eat(Cat c){
    c.eat();
}

public void eat(Dog d){
    d.eat();
}
//......

eat(new Cat());
eat(new Cat());
eat(new Dog());
//......

   If there are tens of thousands of species, I have to write a way to eat for each oneThis must not be exhausting!
   And the upward transformation has such benefits

public void eat(Animal a){
    a.eat();
}

eat(new Cat());
eat(new Cat());
eat(new Dog());
//.....

   Even if other kinds of animals are added later, just let it implement its own class and inherit Animal, which undoubtedly makes the coding much more efficient.

   Downcasting:
It's    simple, it's the opposite of the previous one. Let's turn the parent class object into a subclass object.
   Let's look at an example:

//animal和cat dog
Animal a = new Cat();
Cat c = ((Cat) a);
c.eat();
//输出  我吃鱼
Dog d = ((Dog) a);
d.eat();
// 报错 
Animal a1 = new Animal();
Cat c1 = ((Cat) a1);
c1.eat();
// 报错 

   Why do I get an error? ? ? a itself is a Cat object, so of course it can be down-cast to Cat, but not to Dog;
   and a1 is an Animal object, you don't know whether it is a cat or a dog, of course, you can't complete the conversion
   . Notes: The premise of down-casting is the parent class The object points to a subclass object, that is: Animal a = new Cat()or Animal a = new Dog()
      can only be transformed into an object of this class (ie, cat is cat, dog is dog, cat! = dog)

Back to Contents


Problems and solutions in code debugging

  • Issue 1: Project PP10.4,

  • Solution: Get it at the beginning, (⊙o⊙)… Look at the original code,

       the statement is min, hey! So simple, Kuang Kuang changed all min to max, so fast, this thing is a bit simple! However:

       emmm, obviously, not against it. Descending, min becomes max, it makes sense! However, after a while,

       the variable name is just changed, which has no effect on the operation of the program.

       After reading it carefully, I found the problem.

       Sure enough, programming still has to start with the code, instead of just changing the variable name by habitual thinking.

Back to Contents


code hosting

Back to Contents


Summary of last week's exam mistakes

  • Error 1:

  • Understanding: The Object class is very special. Java does not support multiple inheritance. All subclasses inherit directly or indirectly from the Object class, and they can have any number of subclasses.

  • Error 2:

  • Understanding: The reserved word super provides a mechanism to access the methods and instance data of the parent class (whether it is hidden or not), and can also be used to access the constructor of the parent class.

  • Error 3:

  • Understanding: When a class is modified with final, it cannot be extended.

  • Error 4:

  • Understanding: The subclass can redefine the instance data and methods of the parent class, and even if it is hidden, it can be accessed through super.

Back to Contents


Pairing and mutual evaluation

Reviewed classmates blog and code

  • Pair study this week :
    • 20172316 Zhao Qianchen
    • Worth learning or existing problems in the blog:
      1. There is a problem with the typesetting of text and pictures
      2. The content of the textbook is concise and clear
      3. You can write in the blog where you understand better, or write a piece of code for practical application.
    • 20172329 Wang Wenbin
    • Worth learning or existing problems in the blog:
      1. The text typesetting and indentation in the column of problems and solving process in teaching materials are incorrect;
      2. The content of the teaching materials is concise and clear
      3. For solutions, if there are corresponding examples, please Even better (it might be hard...).

Back to Contents


Others (perception, thinking, etc., optional)

  • When you get a problem, you can't just knock it out. You must analyze it carefully and prescribe the right medicine. It's like thinking about the UML diagram before you can write the code. Not what you want.
    Back to Contents

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 30 articles 400 hours
the first week 202/202 1/2 15/15 Actively type code
the second week 490/692 1/3 18/33 Good thinking
The third week 581/1273 1/4 15/48 good at perfection
the fourth week 1857/3130 1/6 33/81 tired
fifth week 655/3787 1/7 22/103
Week 6 531/4318 1/8 18/121
Week 7 810/5128 1/9 23/ 144
eighth week 810/5128 1/12 21/ 165

Back to Contents


References

"Java Programming and Data Structure Tutorial (Second Edition)"
"Java Programming and Data Structure Tutorial (Second Edition)" study guide

Back to Contents

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325228587&siteId=291194637