Student number 2017-2018-20172309 "Program Design and Data Structure" Week 7 Learning Summary

Student number 2017-2018-20172309 "Program Design and Data Structure" Week 7 Learning Summary

Textbook learning content summary

  • inherit?
    1. Inheritance is an integral part of all OOP (Object Oriented Programming). In the Java language, the extends keyword is used to represent the inheritance relationship. When a class is created, it is always inherited. If the class to be inherited is not explicitly indicated, Object is always implicitly inherited. for example:

    >     class Thing
    >     {
    >       //whatever
    >      }
    > 与
    >      class Thing extends Object
    >      {
    >         //whatever
    >      }
    > 是一样的。
  1. In Java, only single inheritance is allowed. That is to say, a class can only explicitly inherit a parent class. But a class can be inherited by multiple classes, which means that a class can have multiple subclasses.
  2. Subclasses inherit the member variables of the superclass.

    When a subclass inherits a class, it can use the member variables in the parent class, but it does not completely inherit the member variables in the parent class. The rules are as follows:
    1. Can inherit the public and protected member variables of the parent class; cannot inherit the private member variables of the parent class; // The encapsulation of the protected modifier is between public and private. In addition to being referenced by subclasses, variables and methods declared as protected visibility can also be used by any class in the same package.
    2. For the package access member variable of the parent class, if the subclass and the parent class are under the same package, The subclass can inherit; otherwise, the subclass cannot inherit;
    3. For the parent class member variable that the subclass can inherit, if there is a member variable with the same name in the subclass, it will be hidden . The member variable will mask the member variable of the same name of the parent class. If you want to access the member variable of the same name in the parent class in the subclass, you need to use the super keyword to refer to it.

  3. Subclasses inherit methods from the superclass.

    Similarly, the subclass does not completely inherit the methods in the parent class. The rules are as follows:
    1. Can inherit the public and protected member methods of the parent class; cannot inherit the private member methods of the parent class;
    2. For the package access permission member method of the parent class, if the subclass and the parent class are under the same package, the subclass can inherit; otherwise, the subclass cannot inherit;
    3. For a parent class member method that a subclass can inherit, if a member method with the same name appears in the subclass, it is called an overriding phenomenon, that is, the member method of the subclass will override the member method of the same name of the parent class. If you want to access the member method of the same name in the parent class in the subclass, you need to use the super keyword to refer to it.
  • super reference?
    1. In a subclass, the constructor of the parent class cannot be used with the object name. The method name , but the constructor of the parent class can be called with a super reference.
    2. In the inheritance relationship, if you want to instantiate a subclass object, the parent class constructor will be called by default to initialize the properties in the parent class, and then the subclass constructor will be called to initialize the properties in the subclass .

      >      class A {
      >         public A (String msg) {  //父类的构建
      >         System.out.println("*********************");
      >                                            }
      >                }
      >      
      >
      >         class B extends A  {
      >                public B()   {   //子类的构造
      >                System.out.println("####################");
      >                                  }
      >                      }
      >
      >
      >         public class TestDemo   {
      >                public static void main(String []args)  {
      >                  B b =new B(); //实例化子类对象
      >                             }
      >             }
      > 运行结果:
      >     ******************************
      >     ##############################   //这个时候虽然实例化的是子类对象,但是发现它会默认先执行父类构造,调用父类构造的方法体执行,而后再实例化子类对象,调用子类的构造方法。而这个时候,对于子类的构造而言,就相当于隐含了一个super()的形式.

      3. There are two main usages of super:

      1. super. member variable/supe. member method;
      2. super(parameter1, paremeter2, ... );
      The first usage is mainly used to call the parent class's member variable or method of the same name in the subclass; the second is mainly used It is used in the constructor of the subclass to explicitly call the constructor of the parent class. It should be noted that if it is used in the constructor of the subclass, it must be the first statement of the constructor of the subclass.

  • Abstract class: abstract;
  1. In the class definition declaration header, a class can be declared as abstract using the abstract modifier. A class that contains one or more abstract methods must be declared abstract.
  2. Every abstract method uses the abstract modifier, but an abstract class does not have to contain abstract methods. abstract can only modify classes and methods, not variables!
  3. Generally, abstract classes are designed at a higher class level.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: Can a subclass call a private method in a parent class? How to call it?
  • Problem 1 solution: not much nonsense, the above example:

    >      public class Person {
    >             private String name;//name是私有的!!!
    >             public Person(String name) 
    >             {
    >               this.name=name;
    >             }
    >             public String getName()
    >             {
    >                return name;
    >             }
    >
    >
    >      public class Student extends Person { //Student类继承Person类
    >             private int id;
    >             public Student(String name, int id)
    >             {
    >               super(name)
    >               this.id = id ;
    >             }
    >       }//Student类内没name字段,但是它内部Person对象有,我们如何把它钓出来呢?
    >
    >
    >     public static void main(String []args) {
    >            Student s = new Student("你爸爸"99);
    >            System.out.println(s.getName());  //结果就是:你爸爸 
    >            System.out.println(s.name);         //此时出现错误:ERROR:name has private access in Person.
    >           } //我们不可以直接的得到name,但是可以间接的得到他 
  • Question 2: How to understand abstract class , what is its function?
  • Problem 2 Solution:
    Information 1 Information 2

Problems and solutions in code debugging

  • Question 1: When doing exercise PP9.1, there is a situation where the operation is successful, but all are negative.
    .
  • Problem 1 solution:

    I guess there may be two reasons for this:
    1. The coin toss method is not run at all, resulting in the value always being the initial value of 0.
    2. The coin toss method is run, and the other coins are also assigned 0 after the tails 0 are tossed.

To solve this problem, I think it only needs to run a few more times. If it has been negative for so long, the second situation can be ruled out (because as long as you vote, there will be positive situations.Unless you step on shit!) After a few tries, it can be determined that it is the first case. But where is the problem? Subclass code:
Running code:
After thinking for a long time, I found that neither of the above two cases is because there is no assignment! ! !
It causes its value to always be the initial value of 0

  • Question 2: There are two problems when doing PP9.3:
    • 1. I want to use an array to represent books, novels, magazines, academic publications .
      java > int [] list ={1,2,3,4}; //数组初始值表里面可以直接放数字,那我可不可以这样呢? > String [] list ={books, novel, magazine, journal };
      the answer is negative!
      Appears:
      The above error occurs because the elements inside are not initialized, just:
      1. The expected output is:

        books  有 100 页, 且有 200 个关键词。
        noval   有 200 页, 且有 300 个关键词。
        ...
    The address of the output, we should write a method to make the representation to be represented. That is: the toString() method
    will have the expected effect:

code hosting


Summary of last week's exam mistakes

  • Wrong question 1 and reasons, understanding the situation:

    For the first case, both a and b will be declared as arrays. For the second, both c and d will be declared as integers, but for d, it will be declared as an array.

  • Error question 2 and reasons, understanding the situation:

    "=" is a copy operator, it assigns the right side to the left side. Returns true if each element in array a and b are the same, otherwise false.

  • Wrong question 3 and reasons, understanding the situation:

    Arrays are objects, so they inherit from the object class. The object class has a toString method. However, the object's toString method does not return the value stored in the object, but the value of the referenced variable. Therefore, toString used in an array does not return the value stored in the array, but a meaningless set of characters.

  • Error 4 and reasons, understanding the situation:

    Indexes can only be natural integers! ! ! !

  • Error question 5 and reasons, understanding the situation:

    ArrayList is implemented as an array, as long as one of them just accesses the elements of the ArrayList, the efficiency is the same as that of an array. However, when insertions or deletions are made to the front part of the ArrayList, a lot of element copying occurs, reducing its efficiency.

Review Template:

  • Worth learning or questions from the blog:
    • xxx
    • xxx
    • ...
  • Worth learning or problems in the code:
    • xxx
    • xxx
    • ...
  • Based on the scoring criteria, I give this blog a score: XX points. The scores are as follows: xxx

Reviewed classmates blog and code

Others (perception, thinking, etc., optional)

This chapter is more difficult to learn than the previous chapter. Personally, I feel that the things in this chapter are too abstract.In fact, my own power is not enough.. This chapter really has abstract classes and abstract methods/(ㄒoㄒ)/~~ Although this chapter is difficult, I will not let myself give up! This week was a hectic one, as there were five more experiments than usual. The experiments are all things that I haven't been exposed to before. The one that impressed me the most is the fourth experiment, because for it, I stayed up until two o'clock,Swear not to get it right and not to sleep! !This experiment made me understand the importance of the toString() method, and also allowed me to solve the above problems that PP9.3 could not solve before. Ha ha ha ~ thief happy!

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 075/200 1/1 05/20
the second week 560/500 1/2 13/38
The third week 972/1000 1/4 21/60
the fourth week 694/1666 1/5 21/90
fifth week 1544/3095 1/6 30/90
Week 6 600/3627 1/7 30/120
Week 7 544/4200 2/9 20/140

References

1. Summary of inherited data
2. The difference between super and this in Java keywords
3. super keyword
4.

Guess you like

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