class Animal {
String name;
Animal(String name) {this.name = name;}
}
class Cat extends Animal {
String eyecolor;
Cat(String name,String color) {super(name); this.eyecolor = color;}
}
class Dog extends Animal {
String furcolor;
Dog(String name,String color) {super(name); this.furcolor = color;}
}
public class OCast {
public static void main(String [] args) {
Animal a = new Animal("animal");
Cat c = new Cat("cat","blue");
Dog d = new Dog("dog","black");
System.out.println( a instanceof Animal);//return true
System.out.println( c instanceof Animal);//return true
System.out.println( d instanceof Animal);//return true
System.out.println( a instanceof Cat); //return false
System.out.println(a.name); //return animal
a = new Dog("yellowdog","yellow"); //object Dog upcast to Animal
System.out.println(a.name); //return yellowdog
System.out.println(a instanceof Animal); //return true
System.out.println(a instanceof Dog); //return true
//System.out.println(a.furcolor); //error! because a was regarded as Animal
Dog d1 = (Dog)a; // because "a instanceof Dog" is true,so force cast Animal a to Dog
System.out.println(d1.furcolor); //return yellow
}
}
class Animal {
String name;
Animal(String name) {
this.name = name;
}
}
class Cat extends Animal {Cat(String name) {super(name);}}
class Dog extends Animal {Dog(String name) {super(name);}}
public class TestCast {
public static void main(String [] args) {
TestCast t = new TestCast();
Animal a = new Animal("animal");
Animal c = new Cat("cat");
Animal d = new Dog("dog");
t.sing(a);t.sing(c);t.sing(d);
}
void sing(Animal a) {
if ( a instanceof Cat) {
Cat cat = (Cat)a;
System.out.println("cat is singing");
} else if(a instanceof Dog) {
Dog dog = (Dog)a;
System.out.println("dog is singing");
} else {
System.out.println("not an instance of animal");
}
}
}
void sing(Animal a) {
if ( a instanceof Cat) {
Cat cat = (Cat)a;
System.out.println("cat is singing");
} else if(a instanceof Dog) {
Dog dog = (Dog)a;
System.out.println("dog is singing");
} else {
System.out.println("not an instance of animal");
}
}
class Animal {
private String name;
Animal(String name) {this.name = name;}
public void sing(){System.out.println("animal sing...");}
}
class Cat extends Animal {
private String eyeColor;
Cat(String n,String c) {super(n); eyeColor = c;}
public void sing() {System.out.println("cat sing...");}
}
class Dog extends Animal {
private String furColor;
Dog(String n,String c) {super(n); furColor = c;}
public void sing() {System.out.println("dog sing...");}
}
class Lady {
private String name;
private Animal pet;
Lady(String name,Animal pet) {this.name = name; this.pet = pet;}
public void myPetSing(){pet.sing();}
}
public class DuoTai {
public static void main(String args[]){
Cat c = new Cat("catname","blue");
Dog d = new Dog("dogname","black");
Lady l1 = new Lady("l1",c);
Lady l2 = new Lady("l2",d);
l1.myPetSing();
l2.myPetSing();
}
}
cat sing... dog sing...
Lady(String name,Animal pet) {this.name = name; this.pet = pet;}
public void myPetSing(){pet.sing();}
class Animal {public void sing(A);}
class Dog extends Animal {public void sing(B);}
public class Test {
Animal a = new Dog(); //父类引用变量a指向子对象Dog,此时将向上转型
a.sing(); //使用父类引用变量a引用被重写的方法sing(),执行时将动态绑定到Dog的sing()
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有