20165318 2017-2018-2 "Java Programming" Fourth Week study summary

Source: Internet
Author: User
Tags instance method

20165318 2017-2018-2 "Java Programming" Fourth week study summary idea installation

According to Lou teacher Intellj idea Simple tutorial, I downloaded idea but because there is no registered school mailbox, I can not apply for free use, I found on the internet IntelliJ idea 2017 registration code free activation method successfully installed and run ideas.

Summary of the fifth Zhang Ziyi (class and inheritance of textbook learning
  • There can be only one parent class per subclass, and a parent class may have more than one child class. You can use keywords extends to define a subclass of a class:

    Class extends is the object class by default when no keyword is added, and the object class is the ancestor class of any class (except itself).

  • Inheritance of subclasses
    • When subclasses and parent classes are in the same package, subclasses inherit private member variables and methods that are not in the parent class, and their access rights remain unchanged.
    • When a subclass is not in the same package as the parent class, the subclass does not inherit and private the member variables and methods of the friendly access permissions are inherited and only protected public .
  • If a class A wants to invoke its variable or method by creating an object from a subclass C of another class B: ① to invoke the new definition within subclass C instead of inheriting, as long as C and a are the same package; ② to invoke C inheritance, as long as C inherits the ancestor class with a same package.

  • A subclass can manipulate an inherited variable by inheriting the method.

  • instanceof Operator: When the left operand is an object created by the right class or subclass, the result of the operation is true , otherwise false .

  • a method inherited by a subclass can only manipulate member variables that are inherited and hidden by subclasses . A subclass of a newly defined method can manipulate subclass inheritance and new declared member variables of subclasses, but cannot manipulate member variables that are hidden by subclasses.

  • Method overrides: Requires that the type of the new method be consistent with the corresponding method of the parent class or the type of the method of the parent class , and that the name of the method, the number of arguments, the type of the parameter, and the method of the father are identical. For example:

    class A {Object get() {    return null;}}class B extends A {Integer get() {             //Integer是object的子类    return new Integer(100);//返回一个Integer对象}}
  • The overriding method can manipulate inherited variables, methods, or manipulate the newly declared member variables, methods of the subclass, but cannot manipulate the member variables hidden by the quilt class . If a subclass wants to use a method or member variable that is hidden, use it super . Note that overriding the method does not allow you to reduce access to the method, but you can increase access rights .

  • When using overrides, be careful to distinguish between overrides, overloads, and syntax errors.

    float computer(float x,float y)//父类中的方法。float computer(float x,float y)//子类中的方法,是重写。double computer(float x,float y)//子类中的方法,语法错误,既不是重写,也不是v重载float computer(float x,float y,double z)//子类中的方法,是重载。
  • How to use Super : Use commands super.[成员变量] and super.[方法] to separate the hidden variables and methods.

  • Subclasses do not inherit the construction method of the parent class. Super must be the first statement of a subclass construction method . If the Super keyword is not written out in the subclass, it is super() called by default.

  • When you define multiple construction methods in a parent class, you should include a constructor without parameters, because Java does not provide a default construction method (without the constructor of the parameter), which causes an error when Super is omitted.

  • final keyword: The final keyword can modify local variables in classes, member variables, and methods.
    • Modifier class: A class that is decorated cannot have subclasses.
    • Methods to decorate the parent class: Subclasses cannot override a decorated method
    • Modifier variable: The modified variable becomes a constant, and the constant cannot change during operation.
  • On a transformed object: a reference to the object created by the handle class is assigned to the object created by the parent class to get the upper transformation object. On a transform object cannot manipulate the new member variables and methods of a subclass, but you can manipulate member variables that are inherited or hidden by subclasses, and invoke methods inherited by subclasses or overridden instance methods.

  • If a subclass overrides an instance method of a parent class, the on-transition object invokes an instance method that is overridden by the subclass.

  • Note that if a subclass overrides a static method of a parent class, the upper transformation object of the child class object cannot invoke the overridden static method of the subclass, only the static method of the parent class can be called.

  • Polymorphism: The method of the parent class when overridden by a quilt class, can each produce its own functional behavior.

  • Keyword abstract:
    • Modification method: The decorated method can only declare that it cannot run.
    • Modified class: There can be methods or methods in the class being decorated abstract 非abstract . Classes that are not modified can only have 非abstract methods.
  • final abstract a method or class is not allowed and decorated at the same time.

  • The adornment method is not allowed static abstract . (The abstract method must be an instance method)

  • abstractThe child class of the parent class 非abstract must override the method of the parent class abstract .

  • abstrcatClass can only declare an object but cannot create an object, but the object may become the upper-transition object of its subclass.

  • To transform objects and objects on:

    • One of the core techniques for programming with polymorphism is to use the on-Transition object (the abstract object declared by the class as the top-transforming object of its subclass object). abstractclass is useful in practice because it focuses on object-oriented rather than class-oriented.

    • The open-close principle is open to extensions and closed to modifications. The system is easy to maintain by observing the opening and shutting principle.

The sixth chapter interface and realization
  • Use keywords interface to define an interface. 接口=接口声明+接口体.

  • Only constants (with final adornments, permissions public , and static constants) and abstract methods ( public and abstract adornments) are used in the interface body.

  • implementsdeclaring the class with a keyword in a class declaration implements one or more interfaces.

  • If a non-abstract class implements an interface, the class must override all methods in the interface. When overriding an interface method, remove the abstract modifier, give the method body, and public modify it.

  • If an abstract class implements an interface, it can either override the methods in the interface or directly own the methods in the interface.

  • You can use the interface name to access the constants in the interface, or you can do so directly by implementing the class of the interface.

  • publicAn interface can be implemented by any class, and a friendly interface can be implemented by an interface in the same package.

  • Interfaces can also be extends inherited, and subinterfaces inherit all the methods and constants of the parent interface.

  • The import statement can introduce the package's class or the interface to the package.

  • Interface callback: Simply put, the interface callback is to be able to invoke a method in an interface. Concrete operation: Class A implements interface B;a creates object a;b declares variable b;b=a;

  • abstractClass-To-interface comparisons:
    • Interfaces and abstract classes can be used in both abstract methods
    • Interfaces can only have constants, cannot have variables, and abstract classes may have constants or variables
    • abstractA class can have non- abstract methods, and interfaces cannot.
Problems and solutions during this week's study
    • Issue 1: When compiling example5_6, the following prompt appears:

    • Workaround: How to solve the outdated API that is used or overwritten in Java? In the source code, the deprecated method is called when the class in the JDK is used. The so-called outdated approach is a method that has no real implementation or a potential problem. These methods are typically supported by the current JDK, but may be canceled in a future release. Therefore, the code that is used locally does not have to worry about this, and the program to move the value to another computer must solve the problem.

    • Question 2: The fifth chapter after the question Q: What is the purpose of the rewrite method?

    • Workaround: The P121 in the book is interpreted as: When a subclass overrides a method that can be inherited, it can write a new method body exactly as it intended to reflect the unique behavior of the overriding method. Overrides are explained in the role of rewriting: The rewrite is to enhance the reuse and reusability of the class, extensibility, the extension of the method in the class, because the inheritance uses the things of the parent class, and the rewrite not only gets the things of the parent class, but also adds its own things.

    • Question 3: Why use interfaces instead of just writing a method to implement them?

    • WORKAROUND: You can use the same interface when subclasses have different parent classes, but have the same behavior criteria.

Code Cloud Link:

Https://gitee.com/besti-is-java-2018/20165318_sun_xiaoxuan

Code

20165318 2017-2018-2 "Java Programming" Fourth Week 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.