The second section refers to coercion type conversions for variables

Source: Internet
Author: User

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

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.