Java Foundation II (read head first Java record)

Source: Internet
Author: User

write it in front.This section is based on the syntax of some content, such as the internal Java function library, inheritance polymorphism, etc.  "and" and "or" operators1. Short Operator (&&,| | )&& with, the expression must be true on both sides of the expression is true, if the left is false, will not be calculated to the right of the calculation directly return false, you can use && to avoid the operation of the contents of the null pointer variable case if ( Refvar!=null&&refvar.isvalidtype ()) {}   || Similarly, if the left side has returned true, the right side will not be evaluated to return true directly.2. Long operators (& and |)& | When you use a Boolean expression, you are forced to calculate the calculation on either side of the operator3. Parentheses can be used to enclose an expression to control the precedence of an operation  Package1. Classes will be organized using packages2. The complete name of the class consists of the package name and the class name, for example ArrayList is actually called Java.util.ArrayList3. Except for Java.lang, classes that use other packages need to specify the full name (System and string, etc. are Java.lang), Java.lang is a pre-referenced package4.import does not put the package or class into the package, just the left to write the name of the package in front of the class5.javax Package Description He used to be an expansion pack, and later became a standard package.  Inheritance1. Examples of inheritancePublic class Subclass extends parent class{You can define your own instance variablesYou can define your own methods, or you can override the methods of the parent class}2. When invoking a method, the Java virtual Opportunity selects the nearest method, looking from the bottom up, so if the subclass overrides a method of the parent class, the method in the subclass is used when the call3. Determine if the inheritance is reasonable, follow X is-a y, that is, X is Y is correct. Is-a is one-way4. Subclasses inherit the parent class through extends, and the subclass inherits the instance variables and methods of all public of the parent class (variables and methods that do not inherit private)5. Inherited methods can be overwritten, but instance variables are not overwritten. 6. Subclasses to implement a method that does not completely overwrite the parent class, you can use super to first execute the parent class's method, and then append the different contents of the subclass, for example:Public void Roam () {Super.roam ();Next is the subset of subclasses to be implemented separately     }7. The classes that can be inherited:access control, non-public can only be inherited by the same package class;A class using final is considered to be the end of the inheritance tree and cannot be inherited;classes that have only private constructors cannot be inherited8. Methods that use the final decoration cannot be overwritten  polymorphic1. Polymorphic implementation: Inheriting and overriding methods of the parent class2. Override overrides for a method of a parent class, must be the same as the parent class, the return value is compatible with the parent class (or the subclass of the parent class return value), otherwise it is overloaded instead of overriding3. Subclass overrides overrides do not reduce access to the method, such as public in the parent class, and the subclass becomes private is illegal4. A reference to a parent type can point to an object of a subtype, for example:Animal My dog=new dog ()Note: Using a polymorphic call method will first check if there is a method in the parent class, otherwise it will be an error. 5. Parent-child type up and down conversions:the subtype is converted to a parent type and does not require a strong turn, direct conversion:Dog a =new dog ();animal b =a;The parent type is converted to a subtype, requires casting, uses (subclass name) to strongly turn, and the parent type's reference must point to the object of the subclass:Animal A=new Dog ();dog b= (dog) A;  Method Overloadingthe meaning of overloading is to define the same method names, but with different parameters. can return different types of values1. Overloading and polymorphism are not related2. The return type can be different, but the parameters must be different (type or number)3. You can change access permissions  

Java Foundation II (read head first Java record)

Related Article

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.