偶在网上查找一些多态的例子,大部分都是对父类的无参数方法的覆盖,于是我就想实验一下有参数的方法来试验一下多态的机制,但是在运行的时候却只能访问父类的成员变量,代码如下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);}
}请问我的代码哪里有错误?谢谢
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);}
}请问我的代码哪里有错误?谢谢
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
因为你的参数是Animal类型,因此x.eyescolor的值在编译时会被看成是Animal.eyescolor。
方法——运行期绑定,即多态。通过方法的覆盖实现。
动态绑定:实例方法。
1.在 l1.e(c); l2.e(d);里面不是把C 跟D传进去了吗?当中不是实现了多态了吗?
2.按照2L的写法,我觉得public void f(Animal x){}跟public void f(){ }这样写法没有什么区别把?因为X在子类里面都没有用到.这样的话,其实就是实现了对父类方法的重写机制吧?
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
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."就不能正常执行程序呢?
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)中不能呢?
Cat eyescolor ist: black
Cats funktion
而楼主想要的是这样的吧
Cat eyescolor ist: yellow
Cats funktion难道没有实现多态吗?
确实已经实现了,否则结果应该是这样:
pets eyescolor ist: black
Cats funktion问题的焦点是这句
x.eyescolor
没有使用子类的属性其实楼主可以这样理解:
方法是可以重写的
而属性是没有重写的
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();
}
}
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();
}
}
但是有例外!!!!:当父类方法被子类重写的时候,以父类观点来调用这个方法,会执行子类重写过的那个方法。
说的有点乱,希望能够理解。
x.f(x);
x.g();
}
}
X能够自动变成CAT,而到了CAT里面.当它要显示成员变量的时候却又变回了ANIMAL呢?我是不是可以这样认为:多态只可以应用于方法,而成员是不能被多态的呢?
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*/
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*/