public class Person public class Man Extends Person public class DogPerson p = new Person(); Man m = new Man(); Dog d = new Dog();Person p2 = m; 自动向上转型 Man m = Man(p2); //p2 instanceof Man == true 可以向下强制转型 Man m2 = Man(p); //p instanceof Man == false 不能强制转型 Person p3 = (Person)d; //p instanceof Person == false 不能强制转型
abstract class Fruit{ //define the abstract class Fruit,it contains two abstract methods abstract protected void price(); abstract protected boolean isRipe(); }class Apple extends Fruit{ //who extends abstract class,will have to implement all its abstract methods protected void price(){} protected boolean isRipe(){ return true; } }class Pear extends Fruit{ //so this Pear class too prtected void price(){} protected boolean isRipe(){ return false; } }public class Man{ public String buy(Fruit fruit){ if ( fruit instanceof Apple ) { return "I have determined to buy it!"; } else if ( fruit instanceof Pear ) { return "No,the fruit hasn't riped yet!"; } } public static void main(String[] args){ // your code,such as Man fanskit = new Man(); System.out.println(fanskit.buy()); } }
//ER,I am sorry,the full code is as follow: abstract class Fruit{ abstract protected void price(); abstract protected boolean isRipe(); }class Apple extends Fruit{ protected void price(){} protected boolean isRipe(){ return true; } }class Pear extends Fruit{ prtected void price(){} protected boolean isRipe(){ return false; } }public class Man{ public String buy(Fruit fruit){ if ( fruit instanceof Apple ) { Apple apple = (Apple)fruit; if ( apple.isRipe() ) return "I have determined to buy it!"; } else if ( fruit instanceof Pear ) { Pear pear = (Pear)fruit; if ( pear.isRipe() ) return "No,the fruit hasn't riped yet!"; } } public static void main(String[] args){ // your code,such as Man fanskit = new Man(); System.out.println(fanskit.buy(new Apple())); System.out.println(fanskit.buy(new Pear())); } }
public class Man Extends Person
public class DogPerson p = new Person();
Man m = new Man();
Dog d = new Dog();Person p2 = m; 自动向上转型
Man m = Man(p2); //p2 instanceof Man == true 可以向下强制转型
Man m2 = Man(p); //p instanceof Man == false 不能强制转型
Person p3 = (Person)d; //p instanceof Person == false 不能强制转型
//define the abstract class Fruit,it contains two abstract methods
abstract protected void price();
abstract protected boolean isRipe();
}class Apple extends Fruit{
//who extends abstract class,will have to implement all its abstract methods
protected void price(){}
protected boolean isRipe(){
return true;
}
}class Pear extends Fruit{
//so this Pear class too
prtected void price(){}
protected boolean isRipe(){
return false;
}
}public class Man{
public String buy(Fruit fruit){
if ( fruit instanceof Apple ) {
return "I have determined to buy it!";
}
else if ( fruit instanceof Pear ) {
return "No,the fruit hasn't riped yet!";
}
}
public static void main(String[] args){
// your code,such as
Man fanskit = new Man();
System.out.println(fanskit.buy());
}
}
abstract class Fruit{
abstract protected void price();
abstract protected boolean isRipe();
}class Apple extends Fruit{
protected void price(){}
protected boolean isRipe(){
return true;
}
}class Pear extends Fruit{
prtected void price(){}
protected boolean isRipe(){
return false;
}
}public class Man{
public String buy(Fruit fruit){
if ( fruit instanceof Apple ) {
Apple apple = (Apple)fruit;
if ( apple.isRipe() )
return "I have determined to buy it!";
}
else if ( fruit instanceof Pear ) {
Pear pear = (Pear)fruit;
if ( pear.isRipe() )
return "No,the fruit hasn't riped yet!";
}
}
public static void main(String[] args){
// your code,such as
Man fanskit = new Man();
System.out.println(fanskit.buy(new Apple()));
System.out.println(fanskit.buy(new Pear()));
}
}