What is polymorphic
Speaker: Wang Shaohua QQ Group No. : 483773664
First, Sub-class to the parent class'sConversion (upward transformation) (i) Type conversions between basic data 1. Automatic type conversion
// Assign the value of int type constant or variable to double type variable, you can do type conversion automatically
int I = 5;
Double dl = i;
2. Conversions of coercion types
Type conversion operators are parentheses, and the use of type conversion operators is
(type)variable
// Assign the value of a double type constant or variable to an int variable for coercion type conversion
Double d2 = 3.14;
int a = (int) D2;
3. Basic data type Conversion considerations
Conversions between basic types can only be performed between numeric types, where the numeric types are: integer, character, and float. There is no type conversion between a numeric type and a Boolean type.
650) this.width=650; "height=66src=" Http://s3.51cto.com/wyfs02/M02/7E/D7/wKioL1cK95XCfyV5AAAS6zJGA10256.png "V: shapes= "Picture _x0020_1" >
(ii) Reference Data Type transformation
Data type conversions exist not only between basic data types but also between reference types, but there are rules for referencing data type transformations
1. Conversions between reference types can only be done between the two classes that have an inheritance relationship.
Person person = new Dog (); //error
2. Will A Parent class ReferencePoints to a subclass object, called the transformation ( upcasting) ,Type conversions are performed automatically.
Pet Pet= new Dog ();
Second, use the parent class as theMethod formal Parameters
using the parent class as the formal parameter of the method is one of the main ways to implement and use Polymorphism in Java.
(i) Solve the problem of feeding the pet 1. Master
Modify Master class, Annotate feed (dog dog) and feed( PenguinPenguin) method to add unique Feed (Pet Pet) method, as shown below
Public class Master {
Private String name = "" ; // Master Name
Private intmoney = 0; // Number of ingot
/**
* a method for constructing a parameter.
*
* @ param name
* Master Name
* @ param money
* Number of ingot
*/
public Master (String name, int money ) {
this . name = name;
this . Money = money;
}
public void setName(String name) {
The second section refers to coercion type conversions for variables