Java Core Technology (iv)--Inheritance (2)

Source: Internet
Author: User

Following our systematic learning of classes and objects, we continue to learn another basic concept of oop: inheritance. With inheritance, you can construct a new class based on a class that already exists. Inheriting the existing classes is the method and domain of reusing (inheriting) these classes, on the basis of which new methods and domains can be added to meet the new requirements.
In addition, we will introduce the concept of reflection. Reflection is the ability to discover more classes and their properties while the program is running, but this powerful feature attracts the attention of developers of software tools, and the people who write the application are less concerned, so we have a cursory introduction.

3. Generic array list

ArrayList is a generic class that takes a type parameter , which is somewhat like an array, but has the ability to automatically adjust the array's capacity when adding or removing elements. In order to specify the element object type that the array list holds, you need to enclose the class name in <>, such as arraylist< Employee >.
As we declare and construct an array list that holds the employee object:

ArrayList<Employee> staff = new ArrayList<>();

(1) Use the Add method to add elements to the array list

staff.add(new Employee("Harry",···));

The array list manages an internal array of object references, and if you call add and the internal array is full, the array list automatically creates a larger array and copies all the objects from the smaller array to the larger array.

(2) If you can estimate the number of elements that may be stored in the array, you can allocate the initial capacity in the following two ways

staff.ensureCapacity(100);

Or

ArrayList<Employee> staff = new ArrayList<>(100);

(3) The size method can return the actual number of elements in the array list

staff.size();

(4) Once you confirm that the size of the array list is no longer changing, you can use the TrimToSize method to resize the storage area to the number of storage space required for the current number of elements, thus facilitating the garbage collector to reclaim excess storage space. At this point, adding new elements will take time to move the storage block again.

(5) The ArrayList class is not part of the Java programming language, it is a utility class written by some people and placed in a standard library, thus increasing the complexity of accessing elements while facilitating the expansion of capacity.
Use the Get and set methods to implement access or alter the operation of an array element, rather than using the [] syntax format.
Such as

staff.set(i, harry);

(6) Note that the following technique can be double benefit, which means that arrays can be expanded flexibly, and array elements can be accessed conveniently.
First, create an array and add all the elements

Then use the ToArray method to copy the array elements into an array

4. Object wrapper and Automatic packing

All basic types have a class corresponding to it, such as the class for the primitive type int is integer, which is usually called the wrapper.
The object wrapper class is immutable, that is, once constructed, changes to the value wrapped in it are not allowed. The object wrapper classes are also final, so it is not allowed to define their subclasses.

(1) As a result, you can see that if you want to define an integer array list, but the type parameters in <> above are not allowed to be basic types, you can use the corresponding object wrapper class for the base type, such as

ArrayList<Integer>List=new ArrayList<>();

Note, however, that because each value is wrapped in an object individually, ArrayList<Integer> it is much less efficient than int[], so it is best for constructing small collections.

(2) to make it easier to add or get array elements, the following call

list.add(3);

will be automatically transformed into

list.add(Integer.valueOf(3));

This change is called Automatic boxing .

(3) corresponding, when an integer object is assigned to an int value, it will be automatically unboxing , that is, the compiler will be the following statement

int n = list.get(i);

Translated into

int n = list.get(i).intValue();

(4) The = = operator can also be applied to an object wrapper object, except to detect whether the object points to the same storage area

5, variable number of parameters method

The current version of Java provides a method that can be called with a variable number of parameters, also known as a variable parameter method.
such as the printf method:

The user can also define the variable parameter method himself and specify the parameter as any type or even basic type, and the function of the following example calculates the maximum value of several numeric values

This method can be called directly

doublemax(3.144.4. -19);
6. Enumeration class

All enum types are subclasses of the Enum class, and they inherit many of the methods of the class.
As in the example mentioned earlier:

In fact, the type defined by this declaration is a class that has exactly four instances. Therefore, when you compare the values of two enumerated types, instead of calling equals, you use = = directly.
If you want, you can add some constructors, methods, and fields to the enumeration type. Of course, constructing it is called only when constructing an enumeration constant.

(1) The method toString can be used to return the enumeration constant name, such as Size.SMALL.toString () will return "SMALL".

(2) method valueof is the inverse method of ToString, such as

Enum.valueOf(Size.class"SMALL ");

Set S to Size.small.

(3) Each enumeration type has a static values method that returns an array that contains all the enumeration values, such as

values = Size.values();

Returns an array containing the element size.small,size.medium,,size.extra_large.

(4) The Ordinal method but the position of the enum constant in the enum declaration, where the position is counted from zero, for example Size.MEDIUM.ordinal () returns 1.

7. Techniques for succession design

(1) Placing public operations and domains in super-class
(2) do not use protected domains
This is because, first, the collection of subclasses is unrestricted, and any one can derive a subclass from a class, and write code to directly access the instance domain of protected, which destroys encapsulation. Second, in Java, all classes in the same package can access the protected domain, regardless of whether it is a subclass of this class.
(3) using integration to achieve "is-a" relationship
(4) do not use inheritance unless all inherited methods make sense
(5) do not change the expected behavior when overriding the method
(6) use polymorphic, not type information

Java Core Technology (iv)--Inheritance (2)

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.