"Crazy Java Lectures" 5

table of Contents

toString method

final

Abstract classes and abstract methods


toString method

    First look at a program:

image

The results are as follows:

image

As you can see, the above program is not the same as what we want. The above actually outputs the Person object referenced by p. The result we want is "Java", and the toString() method is involved here. So the following two lines of code have the same effect:

image

The results are as follows:

image

This method is an instance method in the Object class, and all Java classes have this method. The toString() method provided by the Object class always returns the "class name+@+hashCode" value of the object's implementation class, but this return value is not what we want. If we want to output the string "Java", we have to rewrite the toString() method in the Object class.

Give a chestnut:

image

The results are as follows:

image

    In fact, the toString() method is a "self-describing" method, but without rewriting, it cannot be described as the result we want to see.

final

    1. Final modified member variables must be explicitly assigned initial values ​​by the programmer;

    2. Formal parameters modified with final cannot be assigned initial values;

    3. Final repair the difference between basic type variables and reference type variables:

    We use the program to illustrate:

image

image

The results are as follows:

image

The two commented out codes that are considered illegal are reported as follows:

image

image

    This is easy to understand, mainly depends on who is modified by final, who can not be modified by modification, but others can be changed. Simple variables are easy to understand. When referring to variables, you must pay attention to the reference address, not the reference object.

4. Executable final variables of "macro substitution"

    For a final, whether it is a class variable, a strength variable, or a local variable, as long as the variable satisfies the following three conditions, the final variable is no longer a variable, but a direct quantity:

(1) Modified by final modifier;

(2) The initial value is specified when the variable is defined;

(3) The initial value can be determined at compile time.

eg: final int age = 5; operation expressions are not, except for this directly to a worthy situation, if the equal sign is followed by an operation expression, simple connection, no access to ordinary variables, call methods, it is also regarded as " Macro variables".

for example:

image

The results are as follows:

image

Let's look at another chestnut:

image

The results are as follows:

image

    In other words, c and d are not equal. Here a, b, and c can be determined at compile time, but because d refers to ordinary variables (a and b are ordinary variables, not macro variables), they are not determined at compile time. But if you turn a and b into macro variables (add modifier final), d can be determined:

image

The result is:

image

Abstract classes and abstract methods

1.  Both abstract methods and abstract classes are decorated with abstract. Classes with abstract methods can only be defined as abstract classes, but abstract classes can have no abstract methods.

2. The  abstract class cannot be instantiated, and the new keyword cannot be used to call the constructor of the abstract class to create an instance of the abstract class.

3. Abstract classes cannot be used to create instances, they can only be inherited by subclasses as a parent class. When a subclass inherits, all the abstract methods of the parent class must be implemented.

Give a chestnut:

image

image

The results are as follows:

image

END

image

[2017.07] This is to make up for yesterday. The learning progress cannot be stopped. If you are empty, you have to make up for it. It doesn't matter if you work hard, I feel at ease. I took my brother to the university campus yesterday, wanting him to feel the university in advance and stimulate his study. But I don’t force anything, after all, I’m still young, and I only now know the meaning of learning, and only then have the passion and desire to learn, little hedgehog, come on!

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113985887