2

我对多态性感到困惑,我想知道这是否考虑多态性?

我觉得它看起来有点奇怪,但它仍然可以正确编译。

public class Family {
    void FamilyInfo() {
        System.out.println("This is a family super class");
    }
}

public class Grandparents extends Family {
    void FamilyInfo() {
        System.out.println("Graparents are the elders in the family and they are the sub class of family");
    }
}

public class Parents extends Grandparents {
    void FamilyInfo() {
        System.out.println("The parents are the children of the grandparents and they are the sub sub class for family");
    }
}

public class FamilyDemo {
    public static void main(String ary[]) {
        Grandparents Gp = new Grandparents();
        Parents P1 = new Parents();


        Gp.FamilyInfo();
        P1.FamilyInfo();

    }
}
4

8 回答 8

3

您的方法FamilyInfo在层次结构中的所有三个类中都被覆盖。这是一个例子polymorphism

调用时Gp.FamilyInfo();:调用Grandparents类中实现的方法并打印Graparents are the elders in the family and they are the sub class of familyP1.FamilyInfo();调用Parents类中的方法并打印The parents are the children of the grandparents and they are the sub sub class for family

因此你可以看到同一个方法FamilyInfo()有两种不同的行为,这就是多态行为。

您的示例与此处教程中提到的示例非常相似:Java 教程:多态。所以不要混淆。

于 2012-12-18T04:14:29.877 回答
1

该示例没有演示多态性,而我只能看到简单的面向对象继承。为了使用多态性的概念,代码应如下所示。

public class FamilyDemo 
{
    public static void main(String ary[]) 
    {
        Family Gp = new Grandparents();
        Family P1 = new Parents();

        Gp.FamilyInfo();
        P1.FamilyInfo();
    }
}

即使Gp是 type Family,它的行为也像 type ,Grandparents因为它是用该类型的对象初始化的。

那么,可能会出现以下情况: 祖父母是家庭中的长辈,他们是家庭的子阶层。父母是祖父母的孩子,他们是家庭的子子类。

于 2012-12-18T04:27:40.787 回答
1

我们的培训师说,使用extends更像是继承的一个例子。但是如果我们使用implements(interface),我们可以说它是多态的,因为我们可以实现很多接口。

例如

interface Horse {
    void run();
}

interface Eagle {
    void fly();
}

public class Pegasus implements Horse, Eagle {
    // Implement methods
     public void run() {
         // do run
     }
     public void fly() {
         // do fly
     }
 }
于 2012-12-18T04:31:07.443 回答
0

从技术上讲,这是多态性。但是,您选择了一个糟糕的示例,并且您似乎不太了解多态性背后的想法。一个更好的例子是这样的。

public abstract class Shape {
  public abstract void drawShape();
}

public class Rectangle extends Shape {
  public void drawShape() {
    // code for drawing rectangle
  }
}

public class Circle extends Shape {
  public void drawShape() {
    // code for drawing circle
  }
}

public class FilledRectangle extends Rectangle {
  public void drawShape() {
    super.drawShape();
    // code for filling rectangle
  }
}

那么负责绘图的类就不需要知道如何绘制每个单独的形状。相反,它可以做到这一点

public void drawAllShapes(Shape[] myShapes) {
  for (int i = 0; i < myShapes.length; ++i) {
    myShapes[i].drawShape();
  }
}

目标是抽象出具体的实现和所有的细节,而只呈现一个通用接口。正如您在上面的最后一个方法中所见,这使得使用不同的类变得更加容易。

于 2012-12-18T04:40:54.750 回答
0

多态性的一个很好的例子是:

public static void print(Family[] family){
    for(int i=0; i< family.length; i++){
        family[i].FamilyInfo();
    }
}

public static void main(String args[])
{
    Family[] family = new Family[2];
    Grandparents Gp = new Grandparents();
    Parents P1 = new Parents();
    family[0] = Gp;
    family[1] = P1;

    //and then send the array to another method which 
    //doesn't "know" which entry in the array is a parent and which is grandparent
    //and there you can loop the array calling family[i].FamilyInfo();
    //THIS is the whole idea of polymorphism in a nutshell
    print(family);

}
于 2012-12-18T04:17:04.743 回答
0

多态性的字典定义是指生物学中的一个原则,其中一个有机体或物种可以有许多不同的形式或阶段

基本概念是给定对象像另一个对象一样工作。这是通过在 Java 中使用接口和继承来实现的。

一个更好的例子是(以您的代码为基础)

public class FamilyDemo {
    public static void main(String ary[]) {
        Family gp = new Grandparents();
        Family p1 = new Parents();

        dump(gp);
        dump(p1);
    }

    public static void dump(Family family) {
        family.FamilyInfo();
    }
}

这基本上允许Gradparents并按Parents原样“行动”Family

于 2012-12-18T04:19:48.240 回答
0

1.什么是多态性?

在面向对象的编程中,多态性(来自希腊语的意思是“具有多种形式”)是能够在不同的上下文中为某事物分配不同的含义或用法的特征——具体来说,允许一个实体,如变量、函数,或者一个对象有多个形式。

2. 两种多态性

a) 静态或编译时多态性

调用哪个方法仅在编译时决定。方法重载就是一个例子。例如

public class calculation
{
    public int add(int x, int y)
    {
        return x + y;
    }
    public int add(int x, int y, int z)
    {
        return x + y + z;
    }
}

在这里你可以看到有两个同名但签名不同的函数

b) 动态或运行时多态性。

运行时多态性也称为方法覆盖。在这种机制中,如果基类包含被覆盖的方法,则在运行时(而不是在编译时)解决对覆盖函数的调用。

Class BaseClass
{
Public void show ()
{ 
Console.WriteLine("From base class show method");
}
}
Public Class DynamicDemo : BaseClass
{
Public void show()
{
Console.WriteLine("From Derived Class show method");
}
Public static void main(String args[])
{
DynamicDemo dpd=new DynamicDemo ();
Dpd.show();

}
}
于 2012-12-18T04:31:11.123 回答
0

是的,在您的示例程序中,您使用的是继承和多态,实际上两者都是封闭相关的。

您正在使用继承,因为您从 Grandparents 类扩展了一次 Family,并且曾经使用 Parents 类扩展了 Grandparents,并且您还使用了多态性,因为您在子类FamilyInfo中编写了在超类中编写的方法 void。

你应该@Override这样使用:

public class Parents extends Grandparents {
    @Override
    void FamilyInfo() {
        System.out.println("The parents are the children of the grandparents and they are the sub sub class for family");
    }
}
于 2014-03-24T00:00:11.407 回答