简体   繁体   English

Java 使用接口作为数据类型

[英]Java using interface as data type

Is it possible to access to field or function of a class object that implements an interface using interface object?是否可以访问 class object 的字段或 function 使用接口 ZA8CFDE633119EB2AC96B8 实现接口?

for example:例如:

public interface Node { // reference to all my trucks

    void collect(Package p);
    void deliver(Package p);
    void work();

}

public class Truck implements Node{...} 
public class Van extends Truck {...}

public class Tracking {

    private Node node;

    @Override
    public String toString() {
        return "time:" + time + " " + node + ", status=" + status;
    }
}

And from another class I try to print Tracking and get node to be specific function from Van class but instead the node return only the toString of the van function. And from another class I try to print Tracking and get node to be specific function from Van class but instead the node return only the toString of the van function. And I have no access to other functions.而且我无法使用其他功能。

    for (int i = 0; i < this.tracking.size(); i++) {
        System.out.println(this.tracking.get(i));
    }

The main problem to access to the truckID field访问 truckID 字段的主要问题

在此处输入图像描述

I would appreciate an explanation on how to solve this.我将不胜感激有关如何解决此问题的解释。

You can use instanceof to find the real class of the object, to be able to use its specific methods可以使用instanceof找到object的真正的class,能够使用它的具体方法

for (Tracking track : this.tracking) {
    if (track instanceof Van){
        Van v = (Van) track;
        v.methodOfVan();
        System.out.println(v.truckID);
    }
    else if (track instanceof Truck ){ // For any other Truck that isn't Van
        Truck t = (Truck) track;
        t.methodOfTruck();
        System.out.println(t.truckID);
    }
}

You are using inheritance here.您在这里使用 inheritance。 Node is the super class.节点是超级class。 And inside your code, you are having a Node instance.在您的代码中,您有一个 Node 实例。 That Node instance can be assigned to any subclass variable like a Van object.该节点实例可以分配给任何子类变量,例如 Van object。

Your question is why van specific method is not available in Node variable.您的问题是为什么在 Node 变量中没有 van 特定的方法。 See at compile time, Java complier won't know what type of subclass will get assigned into the Node variable.在编译时查看,Java 编译器不会知道将分配给 Node 变量的子类类型。 Suppose you have a Taxi class just like Van.假设您有一辆出租车 class,就像 Van。 So a Taxi object can be assigned to Node class.因此,可以将出租车 object 分配给节点 class。 As this runtime information is not available to a compiler, it will make sure Node variable can access only Node specific fields.由于编译器无法使用此运行时信息,因此它将确保 Node 变量只能访问 Node 特定字段。

If you know that only Van can be assigned to the Node variable, you can do a explicit casting.如果您知道只有 Van 可以分配给 Node 变量,您可以进行显式转换。

Van van = (Van) node;

Then you can access van specific fields or methods.然后,您可以访问 van 特定的字段或方法。 Compiler won't complain here as the coder has explicitly taking care of the class casting, so responsibility lies with the coder.编译器不会在这里抱怨,因为编码器已明确处理 class 转换,因此责任在于编码器。

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

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