Android Training (Java article) 5th day-Object Oriented (bottom)

Source: Internet
Author: User

    1. Super Keyword:

      Syntax: Super. Member variables and methods of the parent class;

      Super ();//constructor method for calling parent class

      Usage: If the constructor of the parent class is called, super () must be placed on the first row;

      Note: must exist in the subclass, and this cannot be placed in a static method; The parent class's construction method is not overwritten by the construction method of the class (note that it does not require the output of each constructor method of the parent class);

Example:

public class A {

Public A () {}

Public A (int a) {}

}

Class B extends A {

Public B () {

Super (4);//super (); all can.

}

}


The 2.Object class is the base class for all classes;

3.toString (): Returns the string sequence of the object;

Note: toString () has a return value, want to output return value must have a (assignment statement);

By default, the output object name is equivalent to calling the ToString () method;

Example:

public class A {

Public String ToString () {//is equivalent to overriding toString ();

Return "";

Default: Is to return super.tostring (), call the ToString () method inside the object class;

The output is: Package name + class name [email protected]+ hash code value;

}

}


4.equals: Compare the values of two different objects for equality

= = (double equals) differs from equals:

= = for the same storage space, so different space can only equals;

Recommendation: eight basic types with = =, others with equals;


5. Object Transformation:

Upward transformation: The reference of the parent class is directed to the object of the subclass;

Downward transformation: A reference to a subclass refers to the object of the parent class; (Must have an upward transition, no upward no downward)

Note: The capacity of the parent analogy subclass is large; (so the object transformation can also be understood as a strong turn); But the object must be fully referenced in the past, not as overflow as a strong turn;

Example:

public class A {

public static void Main (string[] args) {

A a=new B ();//Upward transformation

b b= (b) (new A ());//error, because overflow;

b b= (b) A; Down transformation

}

}

Class B extends A {

}


Instanceof: Indicates whether the object belongs to the class or subclass of the class;

Syntax: Object instanceof class name; return Boolean type;


6. Polymorphism: Three conditions must be met (inheritance, rewriting, upward transformation)

Role: Good extensibility, strong security;

Note: Polymorphism can implement subclasses overriding methods overriding the parent class override method, (only polymorphic can override methods of the parent class)


7. Abstract class:

Syntax: Permissions +abstract+class+ class Name {}

Precautions:

    1. When you decorate a class with the abstract keyword, this class is called an abstract class, and the method is called an abstract method when it is modified with an abstract method.

    2. Classes containing abstract methods must be declared as abstract classes; Abstract classes must be inherited; Abstract methods must be rewritten and, if not rewritten, should be declared abstract;

    3. Abstract classes cannot be instantiated;

    4. Abstract methods need to be declared, but not implemented;


8.final Keywords:

    1. The value of the final member variable cannot be changed;

    2. The final method cannot be rewritten;

    3. The final class cannot be inherited;

















This article is from the "Kun" blog, please be sure to keep this source http://linyingkun.blog.51cto.com/2393912/1574797

Android Training (Java article) 5th day-Object Oriented (bottom)

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.