Java: Polymorphism in the downward transition +instanceof keyword __java

Source: Internet
Author: User

Animal.java:

Package com.imooc.animal;

public class Animal {
     private String name;
     private int month;
     
     
     Public Animal () {
    	 
     } public
     
     Animal (String name,int month) {
    	 this.setname (name);
         This.setmonth (month);
     }

     Eat () public
     void Eat () {System.out.println () the
    	 ability of animals to eat again. ");
     }
     
    Getter/setter public
	String GetName () {return
		name;
	}

	public void SetName (String name) {
		this.name = name;
	}

	public int getmonth () {return
		month;
	}

	public void setmonth (int month) {
		this.month = month;
	}
     
     
     
     
}


Cat.java:

Package com.imooc.animal;

public class Cat extends Animal {
      
	private String name;
	private int month;
	private double weight;
	
	Public Cat () {
		
	} public
	
	cat (String name,int month,double weight) {
		super (Name,month);//super Parent Class Property constructor method assignment
		setweight (weight);
	}
	
	Run public
	void Run () {
		System.out.println ("Cat will Run");
	}
	
	Method overrides
	@Override public
	void Eat () {
		System.out.println ("Cat eats fish.") ");
	}
	
	
	Getter/setter public
	String GetName () {return
		name;
	}

	public void SetName (String name) {
		this.name = name;
	}

	public int getmonth () {return
		month;
	}

	public void setmonth (int month) {
		this.month = month;
	}

	Public double Getweight () {return
		weight;
	}

	public void Setweight (double weight) {
		this.weight = weight;
	}
	
	
}

Test.java:

Package com.imooc.test;

Import Com.imooc.animal.Animal;
Import Com.imooc.animal.Cat;
Import Com.imooc.animal.Dog;

public class Test {public

	static void Main (string[] args) {
		Animal one=new Animal ();//1 Common Creation Instance
		one.eat (); 
  animal two=new Cat ();//2. Upward transition
		two.eat ();
		System.out.println ("=====================");
		
		*
		 * Downward transition (Forced transformation)
		 * Subclass references to instances (objects) of the parent class, where it must be customary to force type conversions
		 * This instance can invoke a subclass-specific method
		 * must satisfy the transition conditions to convert ()
		 * *
		cat temp= (CAT) two;//3. Downward transition, the original two is animal type, forcing type conversion to Cat
		temp.eat ();
		Temp.run ();
		Temp.setmonth (3);
		System.out.println ("Age:" +temp.getmonth ());
                /* Dog temp2= (Dog) two;
		Temp2.eat ();
		Temp2.sleep ();
		Temp2.getsex ();
		The above operation although the system does not complain, but the run throws an exception, the reason: two in the creation of the root is pointing to Cat (), so the above class to reduce to cat can be but turned into a dog will not do
		*}

}

Status of implementation


To solve the above problem:

Introduction of the instanceof keyword : instanceof is a Java, PHP two-dollar operator (operator), and ==,>,< is the same kind of things. Because it is made up of letters, it is also a reserved keyword in java. Its role is to determine whether its left object is an instance of its right class, and returns a Boolean type of data. Can be used to determine whether an instance of a subclass in an inheritance is an implementation of a parent class.


Instanceof generally placed in front of the type conversion, reasonable avoidance of abnormal production.


Package com.imooc.test;
Import Com.imooc.animal.Animal;
Import Com.imooc.animal.Cat;

Import Com.imooc.animal.Dog;
		
		public class Test {public static void main (string[] args) {Animal one=new Animal ();//1 Common Create Instance one.eat ();
		Animal two=new Cat ();//2. Upward transition two.eat ();
		
		System.out.println ("====================="); * * Downward transition (forced transformation) * Subclass references to instances (objects) of the parent class. Here must be used to force type conversions * This instance can invoke a subclass-specific method * must satisfy the transition conditions to convert (), the subclass can not be arbitrarily cast, but the subclass reference point to the parent class instance, you can strong Conversion * instanceof Operator: Returns TRUE/FALSE,/if (two instanceof cat) {cat temp= (cat) two;//3. Downward transition, originally two is animal type, coercion type conversion
		For Cat Temp.eat ();
		Temp.run ();
		Temp.setmonth (3);
		System.out.println ("Two can be converted to cat type");
		} if (two instanceof Dog) {Dog temp2= (Dog) two;
		Temp2.eat ();
		Temp2.sleep ();
		Temp2.getsex ();
		System.out.println ("Two can be converted to dog type");
		} if (two instanceof Animal) {System.out.println ("Two can be converted to Animal type");
		} if (two instanceof object) {System.out.println ("Two can be converted to type Object"); }
		
	}

}



Implementation status:



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.