简体   繁体   English

帮助第一个Polymorphism类

[英]Help with first Polymorphism class

I'm creating some random classes to understand Polymorphism better. 我正在创建一些随机类来更好地理解多态性。 Coding is as follows: 编码如下:

Poly1 : Poly1

public abstract class Poly1 {
    int comPoly;
}

SubPoly1 : SubPoly1

public class SubPoly1 extends Poly1 {
    String testPoly;
}

SubPoly2 : SubPoly2

public class SubPoly2 extends Poly1 {
    int x;
}

testPoly : testPoly

public class testPoly {
public static void main(String[] args) {
    Poly1[] testObj = new Poly1[2];
    testObj[0] = new SubPoly1();
    testObj[1] = new SubPoly2();
    testObj[1].x = 1;
    testObj[1].comPoly = 2;
    System.out.println("Test Output : " + testObj[1].x+ " and " + testObj[1].comPoly);
    testObj[0].testPoly = "Hello";
    testObj[0].comPoly = 8;
    System.out.println("Test Output : " + testObj[0].testPoly+ " and " + testObj[1].comPoly);
   }
}

But the program is not getting past the compilation stage as I get the symbol not found error whenever I try to access the variables from SubPoly1 or SubPoly2 (for example testObj[1].x would return an error). 但是当我尝试从SubPoly1SubPoly2访问变量时,程序没有超过编译阶段,因为我得到symbol not found错误(例如testObj[1].x会返回错误)。

Any help would be appreciated. 任何帮助,将不胜感激。

That's because you've declared testObj to be Poly1[] while x is not definied in Poly1 , but in SubPoly2 . 那是因为你已经声明testObjPoly1[]x不definied Poly1 ,但在SubPoly2 On a Poly1 reference you can only access comPoly . Poly1引用上,您只能访问comPoly

To fix this, you need to move x to Poly1 or to use SubPoly2[] instead or to cast a Poly1 reference to SubPoly2 (which is only possible when the instance is actually a SubPoly2 ). 要解决此问题,您需要将x移动到Poly1或使用SubPoly2[]代替或将Poly1引用转换为SubPoly2 (仅当实例实际上SubPoly2 )。 Which one the best solution is depends on the functional requirements. 哪一种最佳解决方案取决于功能要求。

See also: 也可以看看:

You have declared Poly1[] testObj = new Poly1[2]; 你已声明Poly1[] testObj = new Poly1[2]; which means whatever you put into that array, regardless of the actual type, can only access the members of the Poly1 class. 这意味着无论您放入该数组,无论实际类型如何,都只能访问Poly1类的成员。 Polymoprhism comes from methods, not variables. 多态性来自方法,而不是变量。

For example: 例如:

public class X
{
    public void foo()
    {
        System.out.println("hello from X");
    }
}

public class Y
    extends X
{
    public void foo()
    {
        System.out.println("hello from Y");
    }
}

public class Main
{
    public static void main(final String[] argv)
    {
        final X[] array;

        array = new X[2];
        array[0] = new X();
        array[1] = new Y();

        System.out.println(array[0].foo());
        System.out.println(array[1].foo());
    }
}

will do what you expect. 会做你期望的。

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. 工业中多态性的主要用法(面向对象编程理论)是属于不同类型的对象响应同名的方法,字段或属性调用的能力,每个调用根据适当的类型特定行为。

The keyword here would be same name . 这里的关键字名称相同

You could modify your example, and include a method in your base class, eg public void sayHello() . 您可以修改您的示例,并在基类中包含一个方法,例如public void sayHello() The subclasses, as they inherit from the base class, can respond to sayHello , but in case they are not implementing this method themselves, the call gets dispatched to the superclass' sayHello . 子类从基类继承,可以响应sayHello ,但是如果它们本身没有实现这个方法,则调用将被调度到超类' sayHello Polymorphism happens, if the subclasses implement own sayHello methods themselves. 如果子类本身实现自己的sayHello方法,则会发生多态性。

Maybe you find it easier to come acquainted with this concept, if you choose some more speaking examples. 如果你选择更多的说话例子,也许你会发现更容易熟悉这个概念。 One prominent illustration of polymorphism are shapes: You have one, possible abstract, basic shape and different subclasses, eg a line, a rectangle, a circle, etc. Now each shape will have a draw method, but of course these shapes will use different methods to draw themselves. 多态性的一个突出的例子是形状:你有一个,可能是抽象的,基本的形状和不同的子类,例如一条线,一个矩形,一个圆形等。现在每个形状都有一个draw方法,但当然这些形状将使用不同的绘制自己的方法。

Some starting points: 一些起点:

from a parent reference, you can refer to the common methods only. 从父引用中,您只能参考常用方法。 you are using the parent reference to access a property defined in the sub class 您正在使用父引用来访问子类中定义的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM