在java中创建一个委托对象

时间:2018-04-02 17:15:59

标签: java object delegation

这是我在搜索委派时发现的代码的一部分。我不明白一部分。我们必须创建一个委托对象吗?或者我们只能将它用于我们想要使用方法的类的变量类型?

 msg.attachments([ 
        new builder.ThumbnailCard(session)
        .title('Brian Mathew')
        .subtitle('CIO, Logistics')
        .text('San Francisco Bay Area - 30 Mutual Connections')
        .images([
            builder.CardImage.create(session, 'https://media.licdn.com/dms/image/C5103AQHt4Rp2MT0quA/profile-displayphoto-shrink_800_800/0?e=1527022800&v=alpha&t=gQbe7EXQX1S3wYr8QpCjXqeWyKzM0vQWE0M51WGL8n4')
        ])
        .buttons([
            builder.CardAction.openUrl(session, 'https://google.com', 'More Details')
        ])

1 个答案:

答案 0 :(得分:0)

是变量定义的Point类型吗?

它是一个变量,但此时它没有初始化(一个空变量)。

是否可以或应该如下定义?

您必须初始化变量(在此处,创建构造函数,初始化变量并初始化xCoord和yCoord变量。

public class Point {
    private double xCoord;
    private double yCoord;

    public double getXCoord(){
        return xCoord;
    }

    public double getYCoord(){
        return yCoord;
    }
}

public class Circle {
    /*
        Is it a variable defined type of Point?
        Is it possible or should it have been defined as below.
        What is the difference?
    */
    private Point center;
    // Point center = new Point();

    public Circle(double x, double y) {    //    Constructor
        center = new Point();              //    Initialise variable
        center.xCoord = x;                 //    Set x
        center.yCoord = y;                 //    Set y
    }

    public double getCenterX(){
        return center.getXCoord(); // Delegation
    }

    public double getCenterY(){
        return center.getYCoord(); // Delegation
    }
}

有什么区别?

什么区别?