We used classes to create new types (type), and we used inheritance to facilitate the process of creating classes. I'll delve into the genre in this lecture and introduce the concept of polymorphism (polymorphism).
Type checking
Any variables and references in Java can be used by type declaration (type declaration). We've seen object data, class data, method parameters, method return values, and automatic variables within methods, all of which need to declare their type. Java is a strongly typed (strongly typing) language that checks for types. If we use the wrong type, we will cause an error.
The type does not match, sell Meng invalid
For example, in the test class below, we assign a cup class object to the Aperson class reference:
public class Test
{public
static void Main (string[] args)
{
Human Aperson;
Aperson = new Cup ();
}
Class Human
{
/**
* constructor * * Public
Human (int h)
{
this.height = h;
}
/**
* Accessor
/public int getheight ()
{return
this.height;
}
/**
* mutator
/public void growheight (int h)
{
this.height = this.height + H;
}
private int height;
}
Class Cup
{public
void Addwater (int w)
{
This.water = this.water + W;
}
public void Drinkwater (int w)
{
this.water = this.water-w;
}
private int water = 0;
}
Javac will return:
Found : Cup
required:human
Aperson = new Cup ();
^
1 Error
Basic Type Conversions
Java can type conversions on variables of the base type. The different base types have different lengths and storage ranges. If we convert from a high precision type to a low precision type, such as converting from float to int, then we may lose information. Such conversions are called narrowing transformations (narrowing conversion). In this case, we need to display the declaration type conversions, such as:
public class Test
{public
static void Main (string[] args)
{
int A;
A = (int) 1.23; Narrowing conversion
System.out.println (a);
}
}
If we convert from a low precision type to a high precision type, there is no concern about loss of information. Such transformations are called Loose Transformations (widening conversion). We do not need to display the required type conversion, Java can be automated:
public class Test
{public
static void Main (string[] args)
{
int a = 3;
Double b;
b = A; Widening conversion
System.out.println (a);
}
}
Basic Type Conversions
Upcast and polymorphism
In Java, references can also be type-converted, but with restrictions.
We can convert a derived class reference to its base class reference, which is called an upward transition (upcast) or a loose conversion. The following Brokencup classes inherit from the Cup class and cover Addwater () and Drinkwater () methods in the Cup class:
public class Test
{public
static void Main (string[] args)
{
Cup acup;
Brokencup Abrokencup = new Brokencup ();
Acup = Abrokencup; Upcast
Acup.addwater;//Method Binding
}
}
class Cup
{public
void Addwater (int w) c13/>{
this.water = this.water + W;
}
public void Drinkwater (int w)
{
this.water = this.water-w;
}
private int water = 0;
}
Class Brokencup extends Cup
{public
void Addwater (int w)
{
System.out.println ("Shit, broken Cup") ;
}
public void Drinkwater (int w)
{
System.out.println ("Om...num ..., no water inside");
}
Program Run Result:
As you can see above, there is no need for any display instructions, and we refer the derived class reference Abrokencup to its base class reference Acup. The type conversion will be done automatically by Java.
We then call the Addwater () method of Acup (which we declare it to be the cup type). Although Acup is a reference to the cup type, it actually calls the Brokencup Addwater () Method! That is, even if we pass the upcast and loosely refer to its base class, Java can still correctly recognize the type of the object itself and invoke the correct method. Java can recognize the true type of an object according to its current state, which is called polymorphism (polymorphism). Polymorphism is an important aspect of object-oriented.
Polymorphism is a mechanism of Java support, and it is also an important concept of object-oriented. This raises the question of taxonomy, where the subclass object is actually "a" parent object. For example, a bird, also an animal, a car, it must be a vehicle. Java tells us that a derived class object can be used as a base class object, and Java handles it correctly.
For example, the following inheritance relationship:
We can say drink water (Drinkwater) with cups (cup). In fact, the specific meaning of the action of drinking water will change greatly in the derivative class. For example, drinking water with a straw, and drinking water from a broken cup, these two action differences will be very large, although we are in the abstract of "drinking water." We can, of course, program each derivative class separately, calling a different Drinkwater method. However, as a programmer, we can program the cups to invoke the Drinkwater () method of the cup, regardless of what kind of Derivative cup the cup is. Java invokes the appropriate method, as we can see in the above program.
To see a more meaningful example, we add a drink () method to the human class, which receives a cup object and an integer as an argument. An integer indicating the amount of water to drink:
public class Test
{public
static void Main (string[] args)
{
Human guest = new Human ();
Brokencup Hiscup = new Brokencup ();
Guest.drink (Hiscup);
}
Class Human
{
void drink (Cup acup, int w)
{
acup.drinkwater (w);
}
}
Program Run Result:
In the definition of the drink () of the human class, we require that the first parameter be a reference to the cup type. But in practical use (test Class), the brokencup of the Cup is derived from the class object. This is actually the hiscup upward transformation called the Cup class, passed to the drink () method. In the method, we call the Drinkwater () method. Java discovered that this object is actually a Brokencup object, so the corresponding method of Brokencup is actually called.
Downcast
We can use a base class reference downward transition (downcast) as a reference to a derived class, but require that the base class reference the object to which it is pointing and is already the derived class object to downcast. For example, the above Hiscup can be converted up to the Cup class reference, and then downward transformation into a Brokencup class reference.
Object type
In Java, all classes actually have a common inherited ancestor, the object class. The object class provides methods, such as ToString (). We can override these methods in our own class definitions.
Object: Ancestors
We can write a program to manipulate object objects, and we can pass any object to the program through Upcast.
I'll dive into the object class later.
The implementation of polymorphism relies on the support of Rtti. I'll go deep in the future. )
Summarize
Basic Type Conversions
Polymorphism
Downcast
Object