These days have been preparing for the exam, really no time, has passed so long, finally to the object-oriented!
Let's see what the Kotlin in the class look like.
Can see Kotlin in the class is still very common, mostly similar to Java, the more special are:
Each constructor must have an initial value assigned to each member variable.
Primary constructor, the head of this constructor is immediately following the class name, the function body is in the class, is a block of code contained by the INIT keyword, this function head and function body separate writing is still very few, in fact, these two parts will be integrated into a constructor, Using Jd-gui to decompile the class file, you can see the following final generated constructor:
In Kotlin this is a special constructor that can only be defined one and once it is defined, other constructors must call it.
Wait, there seems to be something wrong, this constructor is the first two lines of code, in fact, the two code is written in the initialization value of the member variable there, the last compiler to plug it to the beginning of the Init block. If the init block is not defined, the member variable initializer code is executed by any one of the constructors.
Next we look at the other constructors, in the constructor header using this call other constructors, here we see a constructor without a function body, compile run is not wrong, in fact, this function is only one code, called the other constructors just.
Here we see a keyword open,open keyword can be used to allow a class to be inherited, yes, do not write open the class default is final, cannot be inherited.
And the same function is final by default, cannot be overridden, in order to override the parent class function, the parent function must use the open definition.
Moreover, in Kotlin, the function parameters are final by default.
Next look at inheritance, we define a subclass for this class.
Subclasses do not inherit the parent class's constructor, you can use the Super keyword to invoke the parent class function in a subclass, overriding the parent class function with override, overriding the parent class function can not reduce the function visibility.
Just now we said that each constructor must give the initial value to each member variable, let's do some damage, we call the Show method before This.z is initialized, the show method needs to access the This.z field, and this.z is not initialized yet, what happens? Let's test it.
The results of the operation are as follows:
You can see that the final output represents z=0, stating that Z is automatically initialized to 0 by the compiler, but the compiler cannot always solve these problems for you, assuming our problems are more complicated, and we'll discuss them later.:-)
Then polymorphic, let's define a parent class and two subclasses:
We have each rewritten the ToString function and finally called its Show method
Both of these variables are contained in a shape3d type, so let's look at the results of the operation.
You can see the polymorphic implementation.
Now let's do some mischief, as we said before, if the member variable is not initialized, the compiler automatically assigns a default initial value to the member variable, and now we change the Triangle3d constructor to look like this:
Call the show function when THIS.C is not initialized:
The results of the operation are as follows:
The result is an exception, which means that the compiler does not always give you a default initial value for your member variable. For complex objects, the compiler is helpless.
So we'd better handle these details carefully and reduce the reliance on compiler-specific behavior.
Kotlin Tutorial Learning-Object oriented