Java object-oriented Learning

Source: Internet
Author: User

Several classes can be defined in a file, but only one class can be set to public. The file name must have the same name as the public class.

When defining a category, there is a basic principle: minimize the exposure of information. It is to try to operate the object through methods without directly accessing the domain members inside the object.

This reference name is not available in static methods.

When Java is used to a class, it loads the class to the program. If you want to initialize a class before loading the class, you can use static to define a static block and write the initialization action during loading.

The type of the returned value cannot be used for method overloading.

When dealing with overload methods, packing problems, and indefinite length parameters, the compiler will look for the correct method in the following order:

1. Find a method that can meet the number and type of parameters before packing

2. A method that can meet the number and type of parameters after the boxing operation is attempted

3. Try to set a variable length parameter and the method can be met

4. the compiler cannot find an appropriate method and reports a compiler error.

Indefinite Length Parameter:

Publc class mathtool

{

Public static int sum (Int... Nums)

{

Int sum = 0;

For (INT num: Nums)

{

Sum + = sum;

}

Return sum;

}

}

In fact, Nums is an array. (That is, int [] Nums)

When using an indefinite length parameter in a method, remember to declare that the parameter must be set at the end of the Parameter Line.

For example:

Public void somemethod (INT arg1, int arg2, Int... vargs );

Public void somemethod (Int... vargs, int arg1, int arg2); // invalid

You cannot use more than two indefinite length parameters.

 

Garbage collection:

There is no destructor in Java, but there is a finalize () method, it is declared as protected, finalize () will be executed when the object is recycled, however, it cannot be used as an destructor because it does not know when object Resources will be recycled.

If you are sure that you no longer use an object, you can specify NULL on the object name to indicate that this name does not reference any object. Objects that are not referenced by any name will be recycled.

You can use the system. GC () Recommendation program for garbage collection. If it is recommended to be adopted, the object Resources will be recycled and the finalize () method will be executed before collection.

 

Inheritance

In Java, all classes directly or indirectly inherit the java. Lang. Object Class.

After a class is extended, the constructor of the parent class can be initialized together. The initialization action of the response has been completed. This can be achieved through the super () method, it indicates that the base class constructor is called. Super () must be called at the beginning of the constructor. In the constructor of the sub-class, if super () is not required to specify which constructor in the parent class to start, the constructor without parameters in the parent class is called by default.

 

Rewrite Method

You can use the super () method to call the constructors of the base class in the derived class, and use super. methodname () to call the methods of the parent class.

When rewriting a method, you must note that you can increase the method permission in the parent class, but you cannot abbreviated the method permission in the parent class. That is, if the original member is public, it cannot be rewritten as private or protected in the parent class.

 

Object Class

In Java, each class inherits directly or indirectly from the object class.

The object class defines several methods, including clone (), finalize () of the protected permission, equals (), tostring (), hashcode (), and Policy () of the public permission (), policyall (), wait (), getclass (), etc. Except getclass (), Policy (), policyall (), wait (), and other methods can be rewritten after inheritance, because these methods are declared as final and cannot be rewritten.

 

Method

Description

Tostring ()

Text description of an object

Getclass ()

Returns the class instance of the object during execution.

Getname ()

Get class name

Hashcode ()

Returns the hash code of the modified object)

Equals ()

Compare whether the object's memory address is the same

Clone ()

Copy object itself

To support copying objects, you must implement the cloneable interface when defining a class. If you do not implement this interface class, the clonenotsupportedexception exception will occur when the clone () method of the instance is called. The clone () method in Java inherits from the object. To facilitate direct calling, change the access permission from protected to public.

For example:

Public class Table implements cloneable // implement cloneable

{

...

Public object clone () throws clonenotsupportedexception

{

Table = (table) Super. Clone (); // call the clone () of the parent class to copy

}

...

}

Changing the content of the Copied object does not affect the original content.

 

Final keywords

The final keyword can be used to indicate that the value of the variable cannot be changed once the variable is set.

For example:

Final double Pi = 3.14159;

Public final double getradius (){....}

If you use final when defining a method member, it means that the method member cannot be overwritten by the subclass.

 

Polymorphism

Polymorphism refers to the use of the same implementation interface to implement different object instances. The object-oriented implementation of polymorphism aims to reduce the dependence on the Implemented interfaces, and thus increase the flexibility and maintainability of the program architecture.

To implement methods on objects, the corresponding type of implementation interface must be used. If an incorrect type is used to convert the implementation interface of objects, a java. Lang. classcastexception occurs.

 

Abstract class:

When defining a class in Java, you can declare only the method name without operating the logic in it. Such a method is called an abstract method. If a class contains abstract methods, the class is called an abstract class. Abstract classes have classes that do not implement methods. Therefore, they cannot be used to generate objects. They can only be inherited and extended, and can implement unfinished Abstract METHODS After inheritance. Abstract keywords must be used to declare abstract methods and abstract classes in Java.

 

Interface:

A class that implements an interface does not belong to a class. Multiple Interfaces can be implemented on an object.

The purpose of an interface is to define a set of methods that can be implemented. The class that implements the interface must implement all the methods defined by the interface. As long as an object implements an interface, you can use this interface to implement the corresponding methods on the object.

The interface keyword is used for interface declaration. The lifecycle is as follows:

[Public] interface Interface Name

{

Permission setting return type method (parameter column );

...

}

If the permission settings on the method are omitted when the interface is declared, the default value is public.

When defining a class, you can use the implements keyword to specify the interface to be implemented, and all the methods defined in the interface must be implemented.

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.