在Jgrapht示例中DefaultEdge.class是什么意思 [英] what does DefaultEdge.class mean in jgrapht example

查看:400
本文介绍了在Jgrapht示例中DefaultEdge.class是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点类在传递给构造函数的参数中的意义



我使用 jgrapht 。我有这个问题



我们传递给DefaultDirectedGraph类的构造函数?
我的意思是DefaultEdge.class是什么意思?在该类中没有具有该名称的静态字段。我的意思是实际传递给那个类的结构者。

  DirectedGraph< String,DefaultEdge> g = 
new DefaultDirectedGraph< String,DefaultEdge>(DefaultEdge.class);


解决方案

如果问题只针对语法,想要参考网站部分, nofollow>检索类对象。一般来说 DefaultEdge.class 是一个表示 class DefaultEdge 的对象。这是 类型的对象, java.lang.Class ,并且包含类本身的信息 - 例如,这个类包含的字段和方法。



在这种情况下,这个 Class 对象由jgrapht在内部使用,以便创建边的实例。当 Graph#addEdge( V,V) 方法被调用,这个对象将被内部使用来创建一个边 - code> DefaultEdge 实例。



有很多可能的方法可以如何完成,但它可以归结为调用 Class#newInstance() 从给定的获取构造函数,并调用 构造函数#newInstance(... ) ,传递给定顶点作为参数。



/ p>

对于 DefaultDirectedGraph 的特定情况,边缘的创建使用 EdgeFactory - 从两个顶点创建边缘实例的简单 Factory 。此工厂用于 addEdge 方法中:

  @覆盖public E addEdge(V sourceVertex,V targetVertex)
{
...
E e = edgeFactory.createEdge(sourceVertex,targetVertex);
...
}

EdgeFactory 对象(它可以是 DefaultEdge.class )在构造函数中创建/ code> ,例如):

  public DefaultDirectedGraph(Class< ;? extends E> edgeClass)
{
this(new ClassBasedEdgeFactory< V,E>(edgeClass));
}

ClassBasedEdgeFactory 反过来,我做了我已经提到的:它使用给定的对象来创建一个新的实例:

  public class ClassBasedEdgeFactory< V,E>实现EdgeFactory< V,E> ;, ... 
{
...
private final Class<延伸E> edgeClass;

public ClassBasedEdgeFactory(Class< ;? extends E> edgeClass)
{
this.edgeClass = edgeClass;
}

...
@Override public E createEdge(V source,V target)
{
try {
return edgeClass。 newInstance();
} catch(Exception ex){
throw new RuntimeException(Edge factory failed,ex);
}
}
}






总结:可以向图构造函数传递 Class (例如, DefaultEdge.class ),简单地告诉他:每当我想添加一个新的边缘,然后创建一个新的边缘类的实例。


What does dot class mean in the parameter passed to Constructor

I am using jgrapht for the first time . I have this question

What are we passing to the constructor of the DefaultDirectedGraph class? I mean what does DefaultEdge.class means ? There is no static field with that name inside that class. I mean what is actual being passed to the contructor of that class. What does the dot class mean there ?

DirectedGraph<String, DefaultEdge> g =
            new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);

解决方案

If the question only aims at the syntax, you might want to refer to the section The .class Syntax of the site about Retrieving Class Objects from the oracle documentation. In general DefaultEdge.class is an object that represents the class DefaultEdge. This is an object of the type java.lang.Class, and contains information about the class itself - e.g., which fields and methods this class contains.

In this case, this Class object is used internally, by jgrapht, in order to create instances of edges. When the Graph#addEdge(V,V) method is called, this Class object will be used internally to create an edge - particularly, to create a DefaultEdge instance.

There are various possible ways how this can be done exactly, but it will either boil down to calling Class#newInstance(), or to obtaining a Constructor from the given Class and invoking Constructor#newInstance(...), passing the given vertices as parameters.

Extended in response to the comment:

For the particular case of the DefaultDirectedGraph, the creation of the edges is done with an EdgeFactory - a simple Factory that creates edge instances from two vertices. This factory is used in the addEdge method:

@Override public E addEdge(V sourceVertex, V targetVertex)
{
    ...
    E e = edgeFactory.createEdge(sourceVertex, targetVertex);
    ... 
}

This EdgeFactory is created in the constructor, from the given Class object (which may be DefaultEdge.class, as in the example) :

public DefaultDirectedGraph(Class<? extends E> edgeClass)
{
    this(new ClassBasedEdgeFactory<V, E>(edgeClass));
}

The ClassBasedEdgeFactory, in turn, does what I already mentioned: It uses the given Class object to create a new instance:

public class ClassBasedEdgeFactory<V, E> implements EdgeFactory<V, E>, ...
{
    ...
    private final Class<? extends E> edgeClass;

    public ClassBasedEdgeFactory(Class<? extends E> edgeClass)
    {
        this.edgeClass = edgeClass;
    }

    ...    
    @Override public E createEdge(V source, V target)
    {
        try {
            return edgeClass.newInstance();
        } catch (Exception ex) {
            throw new RuntimeException("Edge factory failed", ex);
        }
    }
}


So to summarize: One can pass a Class to the graph constructor (for example, DefaultEdge.class), to simply tell him: "Whenever I want to add a new edge, then create a new instance of this edge class."

这篇关于在Jgrapht示例中DefaultEdge.class是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆