偶在网上查找一些多态的例子,大部分都是对父类的无参数方法的覆盖,于是我就想实验一下有参数的方法来试验一下多态的机制,但是在运行的时候却只能访问父类的成员变量,代码如下class test1{
public static void main(String[] args){
   Cat c = new Cat();
   Dog d = new Dog();
   Lady l1 = new Lady(c);
   Lady l2 = new Lady(d);
   l1.e(c);
   l2.e(d);
   
}
}class Animal{
String name ="a",eyescolor ="black";
public void f(Animal x){
System.out.println("pets eyescolor ist: "+x.eyescolor);
}
}

class Cat extends Animal{
public String eyescolor ="yellow";
public void f(Animal x){
System.out.println("pets eyescolor ist: "+x.eyescolor);
}
}

class Dog extends Animal{
public String eyescolor ="red";
public void f(Animal x){
System.out.println("pets eyescolor ist: "+x.eyescolor);
}
}

class Lady{
  Animal pet;
    Lady(Animal x){
     this.pet = x;
     }
public void e(Animal x){pet.f(x);}
}请问我的代码哪里有错误?谢谢

解决方案 »

  1.   


    class test1{ 
    public static void main(String[] args){ 
      Cat c = new Cat(); 
      Dog d = new Dog(); 
      Lady l1 = new Lady(c); 
      Lady l2 = new Lady(d); 
      l1.e(c); 
      l2.e(d); 
      

    } class Animal{ 
    String name ="a",eyescolor ="black"; 
    public void f(Animal x){ 
    System.out.println("pets eyescolor ist: "+x.eyescolor); 

    } class Cat extends Animal{ 
    public String eyescolor ="yellow"; 
    public void f(Animal x){ 
    System.out.println("pets eyescolor ist: "+eyescolor); 

    } class Dog extends Animal{ 
    public String eyescolor ="red"; 
    public void f(Animal x){ 
    System.out.println("pets eyescolor ist: "+eyescolor); 

    } class Lady{ 
      Animal pet; 
        Lady(Animal x){ 
        this.pet = x; 
        } 
    public void e(Animal x){pet.f(x);} 

    /* output
    pets eyescolor ist: yellow
    pets eyescolor ist: red
      

  2.   

    变量——编译期绑定
    因为你的参数是Animal类型,因此x.eyescolor的值在编译时会被看成是Animal.eyescolor。
     
    方法——运行期绑定,即多态。通过方法的覆盖实现。
      

  3.   

    java中有两种绑定,静态绑定和动态绑定。变量用的是静态绑定,就是说在编译的时候已经绑定好了。而方法是在运行时去绑定的。静态绑定:变量和静态方法。 
    动态绑定:实例方法。
      

  4.   

    谢谢各位的回复,但是还有几个问题:
    1.在 l1.e(c); l2.e(d);里面不是把C 跟D传进去了吗?当中不是实现了多态了吗?
    2.按照2L的写法,我觉得public void f(Animal x){}跟public void f(){ }这样写法没有什么区别把?因为X在子类里面都没有用到.这样的话,其实就是实现了对父类方法的重写机制吧?
      

  5.   


    1. 是实现了多态2. public void f(Animal x){}跟public void f(){ } 有区别。我改了下 你看看
    class test1{ 
    public static void main(String[] args){ 
      Cat c = new Cat(); 
      Dog d = new Dog(); 
      Lady l1 = new Lady(c); 
      Lady l2 = new Lady(d); 
      l1.e(c); 
      l2.e(d); 
      

    } class Animal{ 
    String name ="a",eyescolor ="black"; 
    public void f(Animal x){ 
    System.out.println("pets eyescolor ist: "+x.eyescolor); 

    } class Cat extends Animal{ 
    public String eyescolor ="yellow"; 
    public void f(Animal x){ 
    System.out.println("Cat eyescolor ist: "+eyescolor); 

    } class Dog extends Animal{ 
    public String eyescolor ="red"; 
    public void f(Animal x){ 
    System.out.println("Dog eyescolor ist: "+eyescolor); 

    } class Lady{ 
      Animal pet; 
        Lady(Animal x){ 
        this.pet = x; 
        } 
    public void e(Animal x){pet.f(x);} 
    } /* outputCat eyescolor ist: yellow
    Dog eyescolor ist: red
      

  6.   


    1.既然实现了多态,为什么我的程序里面就只能打印父类的EYESCOLOR呢?
    2.你似乎没有改动过那程序.....不过我改动了一下public void f(Animal x){}跟public void f(){ } 的确是有区别。当我用public void f(){ }的时候,程序只打印了父类的EYESCOLOR,请问这是为什么呢?
    3.现在已经排除了其他问题,剩下唯一一个问题就是我的代码里面的之类都写了System.out.println("pets eyescolor ist: "+x.eyescolor);
      而你是System.out.println("Cat eyescolor ist: "+eyescolor); 为什么多了个"X."就不能正常执行程序呢?
      

  7.   

    现在把问题化简一下
    class test3{ 
    public static void main(String[] args){ 
      Cat c = new Cat(); 
      Lady l1 = new Lady(); 
      l1.e(c);   

    } class Animal{ 


    String name ="a",eyescolor ="black"; public void f( Animal x ){ 

       System.out.println("pets eyescolor ist: "+eyescolor); 
       
    } public void g(){

    System.out.println("Animals function");

    }
    } class Cat extends Animal{ 

    public String eyescolor ="yellow";    public void f( Animal x){ 
       System.out.println("Cat eyescolor ist: "+x.eyescolor);   
    }
       public void g()
       {
     System.out.println("Cats funktion"); 


    class Lady{  public void e(Animal x){
    x.f(x);
    x.g();

    } 为什么x.g()中能够实现多态,而x.f(x)中不能呢?
      

  8.   

    结果是这样的
    Cat eyescolor ist: black
    Cats funktion

    而楼主想要的是这样的吧
    Cat eyescolor ist: yellow
    Cats funktion
    难道没有实现多态吗?
    确实已经实现了,否则结果应该是这样:
    pets eyescolor ist: black
    Cats funktion
    问题的焦点是这句
    x.eyescolor
    没有使用子类的属性其实楼主可以这样理解:
    方法是可以重写的
    而属性是没有重写的
      

  9.   

    樓主你的子類方法中,x是animal類的,取得的自然是animal類的eyecolor..
      

  10.   

    真是受益匪浅。看了好久的多态,没真正理解过。现在有点眉目了。感谢ing
      

  11.   


    public class Test1 {
    public static void main(String[] args) {
    Cat c = new Cat();
    Dog d = new Dog();
    Lady l1 = new Lady(c);
    Lady l2 = new Lady(d);
    l1.e();
    l2.e(); }
    }class Animal {
    String name = "a", eyescolor = "black"; public void f() {
    System.out.println("pets eyescolor ist: " + eyescolor);
    }
    }class Cat extends Animal {
    public String eyescolor = "yellow"; public void f() {
    System.out.println("pets eyescolor ist: " + eyescolor);
    }
    }class Dog extends Animal {
    public String eyescolor = "red"; public void f() {
    System.out.println("pets eyescolor ist: " + eyescolor);
    }
    }class Lady {
    Animal pet; Lady(Animal x) {
    this.pet = x;
    } public void e() {
    pet.f();
    }
    }
      

  12.   


    public class Test1 {
    public static void main(String[] args) {
    Cat c = new Cat();
    Dog d = new Dog();
    Lady l1 = new Lady(c);
    Lady l2 = new Lady(d);
    l1.e();
    l2.e(); }
    }class Animal {
    String name = "a", eyescolor = "black"; public void f() {
    System.out.println("pets eyescolor ist: " + eyescolor);
    }
    }class Cat extends Animal {
    public String eyescolor = "yellow"; public void f() {
    System.out.println("pets eyescolor ist: " + eyescolor);
    }
    }class Dog extends Animal {
    public String eyescolor = "red"; public void f() {
    System.out.println("pets eyescolor ist: " + eyescolor);
    }
    }class Lady {
    Animal pet; Lady(Animal x) {
    this.pet = x;
    } public void e() {
    pet.f();
    }
    }
      

  13.   

    发生多态的时候,以谁的观点看待对象就调用谁的方法和属性。比如楼主的x.eyescolor,子类方法中,x是animal类的,也就是说是以animal观点来看待对象的,所以就要调用animal类的方法和属性。
    但是有例外!!!!:当父类方法被子类重写的时候,以父类观点来调用这个方法,会执行子类重写过的那个方法。
    说的有点乱,希望能够理解。
      

  14.   

    高人啊~~我怎么就没有看到Cat eyescolor ist: black 这句话呢~不过我还是有个疑问:为什么在class Lady{ public void e(Animal x){ 
    x.f(x); 
    x.g(); 


    X能够自动变成CAT,而到了CAT里面.当它要显示成员变量的时候却又变回了ANIMAL呢?我是不是可以这样认为:多态只可以应用于方法,而成员是不能被多态的呢?
      

  15.   

    package reverse;public class Test3 {
        public static void main(String[] args) {
            Cat c = new Cat();
            Dog d = new Dog();
            Lady l1 = new Lady(c);
            Lady l2 = new Lady(d);
            l1.e(c);
            l2.e(d);    }
    }class Animal {
        String name = "a"; 
        String eyescolor = "black";
        
        public String  getEyescolor(){
         return eyescolor;
        }
        public void f(Animal x) {
            System.out.println("pets eyescolor ist: " + x.eyescolor);
        }
    }class Cat extends Animal {
        public String eyescolor = "yellow";    
        public void f(Animal x) {
            System.out.println("pets eyescolor ist: " + x.getEyescolor());
             }
    }class Dog extends Animal {
        public String eyescolor = "red";    public void f(Animal x) {
            System.out.println("pets eyescolor ist: " + x.getEyescolor());
        }
    }class Lady {
        Animal pet;    Lady(Animal x) {
            this.pet = x;
        }    public void e(Animal x) {
            pet.f(x);
        }
    }/**
      pets eyescolor ist: black
      pets eyescolor ist: black*/
      

  16.   

    package reverse;public class Test3 {
        public static void main(String[] args) {
            Cat c = new Cat();
            Dog d = new Dog();
            Lady l1 = new Lady(c);
            Lady l2 = new Lady(d);
            l1.e(c);
            l2.e(d);    }
    }class Animal {
        String name = "a"; 
        String eyescolor = "black";
        
        public String  getEyescolor(){
         return eyescolor;
        }
        public void f(Animal x) {
            System.out.println("pets eyescolor ist: " + x.eyescolor);
        }
    }class Cat extends Animal {
        public String eyescolor = "yellow";    public String  getEyescolor(){
         return eyescolor;
        }
        public void f(Animal x) {
            System.out.println("pets eyescolor ist: " + x.getEyescolor());
             }
    }class Dog extends Animal {
        public String eyescolor = "red";
        public String  getEyescolor(){
         return eyescolor;
        }
        public void f(Animal x) {
            System.out.println("pets eyescolor ist: " + x.getEyescolor());
        }
    }class Lady {
        Animal pet;    Lady(Animal x) {
            this.pet = x;
        }    public void e(Animal x) {
            pet.f(x);
        }
    }/**
    *pets eyescolor ist: yellow
     pets eyescolor ist: red*/