Java Summary-Chapter 6

Source: Internet
Author: User

Chapter 6
Java class design
Extends class inheritance
The object class is the top parent class of all Java classes.
Java only supports single inheritance and does not allow multiple inheritance
One subclass has only one parent class. One parent class can have multiple child classes.
Public default protected public
Method Rewriting
The method inherited by the parent class can be transformed as needed in the subclass-method Rewriting
The override method must have the same name as the method to be overwritten. Parameter List return value type rewrite method cannot use access permission rewrite method that is stricter than the method to be overwritten cannot cause more exceptions than the parent class
The method declaration must be outside the main function
Java uses super to reference the components of the parent class
The construction method of the parent class can be traced back to the direct parent class.
This class super parent class
Polymorphism
The subclass objects in Java can be used instead of the objects in the parent class.
An object can only have a certain data type.
Person P = new student ();
Object o = new person ();
O = new student ();
Subclass after parent class
Polymorphism
If a referenced type variable is declared as the parent class type but actually references a subclass object, the variable cannot access the newly added attributes and methods in the subclass.
Virtual method call
Person A = new student ();
A. getinfo ();
Instanceof Operator
Public class person extends object {};
Public class student extends person {};
Public class graduate extends person {};

Public void method (person e) // instantiate E
{
If (E instanceof student)
{
}
Else if (E instanceof garduate)
{
}
Else {
// Process person objects
}
}
The forced type conversion for Java objects is called modeling.
Type conversion from subclass to parent class can be performed automatically
The type conversion from the parent class to the subclass must be achieved through styling (forced type conversion ).
The conversion between reference types without inheritance relationships is invalid.
Definitions of public class test {// person and student classes already exist.
Public void method (person e) {// instantiated E
System. Out. pritnln (E. getschool (); // The Student variable is invalid.
If (E intstanceof student ){
Student me = (student) E; // forcibly convert the person Class E to the Student Class
System. Out. pritnln (Me. getschool (); // you can call
}
}
Method Name overload
Multiple methods with the same name can be defined in a class -- Method Name overload
The parameter list of the overload method must be different.
The Return Value Type of the overload method can be the same or different.
Public void printl (int I ){};
Public void printl (float I ){};
Public int printl (int I ){};
The list of parameters to be overloaded by the constructor must be different.
The first line of the constructor can call other overloaded constructor using the this keyword.
Public class person
{
Public Person (int ){};
Public Person (int A, int B ){};
}

Public class person
{Private string name;
Private intage;
Private date birthdate;
Public Person (string name, intage, date D)
{This. Name = Name;
This. Age = age;
This. Birthdate = D;
}

Public Person (string name, int age)
{
This (name, age, null );
}
Public Person (string name, date D)
{
This (name, 30, d );
}
Public Person (string name)
{This (name, 30);} // This is used in the case of heavy load
}
Constructor cannot be inherited
Subclass inherits all member variables and member methods of the parent class, but does not inherit the constructor of the parent class.
Java constructor Source
No parameter structure by default
Explicitly define one or more constructor Methods
When the subclass calls the constructor of the parent class, super () is used to call the constructor of the parent class.
Public class person
{
Public int;
Public int B;

Public Person (int A, int B) {This. A = A; this. B = bl ;}
Public Person (int A) {This ();}

}
Public class student extends person {
Public date m;
Public student (int A, int B, date m)
{
Super (A, B );
This. M = m;

}
}

The object class is the root parent class of all Java classes.
If the class does not declare the parent class, the default parent class is the object class.
= Differences between operators and equals Methods
The equals () method can only compare reference types
The comparison type and content of the file string date class and the encapsulation class
Whether the reference is the same instance
= Can compare the reference type and basic type
The Data Types on both sides of the symbol must be consistent
Int x = 10;
Int y = 10;
Person A = new person (10 );
Person B = new person (10 );

If (x = y) {// equals
System. Out. println ("dengyu ");
} Else {
System. Out. println ("budengyu ");
}
If (A = B) {// not equal
System. Out. println ("dengyu ");
} Else {
System. Out. println ("budengyu ");
}
If (A. Equals (B) {// not equal
System. Out. println ("dengyu ");
} Else {system. Out. println ("budengyu ");}

}
The tostring () method is defined in the object class and its return value is of the string type.
The tostring () method of the corresponding encapsulation class is called when the basic data type is converted to the string type.
Eight basic type reference types-encapsulation class
Basic Data Type Encapsulation
Boolean
Byte byte
Short short
Int integer
Long long
Char character
Float float
Double double
Encapsulation usage
Int I = 500;
Integer T = new INTEGER (I );
Int J = T. intvalue ();
String S = T. tostring ();
String S1 = integer. tostring (314 );

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.