多态变化,static关键字
package com.beyondTest;
public class DuoTaiTest {
public static void main(String[] args) {
SiYangYuan syy=new Cat();
syy.Feed();
}
}
class SiYangYuan{
public void Feed(){
}
}
class Cat extends SiYangYuan{ //多态一定要实现继承,因为多态的发生条件是必须重写父类方法
public void Feed(){
System.out.println("给猫喂食");
}
}
class Dog extends SiYangYuan{
public void Feed(){
System.out.println("给狗喂食");
}
public void eatShit(){
System.out.println("eat shit!!!");
}
}
以上是多态的代码体现;
package com.beyondTest1;
public class SiYangYuan {
public void Feed(Animal a){
Cat cat=(Cat) new Animal();
a.eatFood();
}
public static void main(String[] args) {
T tt=new T();
Animal xx=new Dog();
// tt.test(xx);
//
Dog dog=new Dog();
dog.eatShit();
Dog dog1=(Dog)xx ;
dog1.eatShit();
}
}
class Animal{
public void eatFood(){}
public void slepp(){}
}
/* 在T类中编写test方法 将Animal绑定在(传入)方法,然后将Animal类转换成Cat类
* 这样的话在主方法中调用test方法,会将任何传入的值
*
* */
class T{
public void test(Animal a){ //静态绑定的例子 编译期就将Animal类绑定在方法test中,
Cat yyCat=(Cat)a;
yyCat.Feed();
}
}
class Cat extends Animal{ //多态一定要实现继承,因为多态的发生条件是必须重写父类方法
public void Feed(){
System.out.println("给猫喂食");
}
}
class Dog extends Animal{
public void Feed(){
System.out.println("给狗喂食");
}
public void eatShit(){
System.out.println("eat shit!!!");
}
}
以上是静态的代码体现;