尝试在 Java 中实现 Bridge 模式时出错

发布于 2024-12-13 13:22:37 字数 2225 浏览 2 评论 0原文

我想将 Shape 对象传递给 ShapeImp 对象,例如 Vector 或 Raster。尝试从 Circle 和 Square 的内部构造函数传递“this”时会出现错误。我想将具体形状传递给矢量或光栅。

Netbeans 行错误

super(平台, x,y, this, "Circle999");

“在调用超类型构造函数之前无法引用 this 在构造函数中泄漏 this”

package dp.bridge;

//-------Abstraction-------//

//----Abstraction-Specialization----------//
abstract class Shape{

    protected ShapeImpl platform;
    protected String type;

    Shape(String p, int x, int y, Circle s, String type){
        this.type = type;
        if(p.equals("vector"))
            platform = new Vector(x,y,s);
         if(p.equals("raster"))
            platform = new Raster(x,y,s);
    }

    public String getType() {
        return type;
    }

    
     abstract public void draw();
}
class Circle extends Shape{



    Circle(String platform, int x, int y){
        super(platform, x,y, this, "Circle999");
        
    }

    public void draw(){
        System.out.println("Circle: draw()");
        platform.draw();
    }
    
}

class Square extends Shape{

     Square(String platform, int x, int y){
        super(platform, x,y,this, "Square778");
     }

    public void draw(){
        System.out.println("Square: draw()");
        platform.draw();
    }

}

//----Abstract-Implementation------//
interface ShapeImpl{
    public void draw();

}

//--------Concreate implemenations--------//
class Raster implements ShapeImpl{

    int _x;
    int _y;
    Shape s;
    Raster(int x, int y, Shape s){
        _x = x;
        _y = y;
        this.s = s;
    }

    public void draw(){
        System.out.println("Drawing Raster "+s.getType()+ " at (" +_x + "," + _y +")");

    }
}

class Vector implements ShapeImpl{

    int _x;
    int _y;
    Shape s;
    Vector(int x, int y, Shape s){
        _x = x;
        _y = y;
        this.s = s;

    }

    public void draw(){
        System.out.println("Drawing Vector "+s.getType()+ " at (" +_x + "," + _y +")");

    }


}

//-----Client-------//
class Client{

    
    public static void main(String atgsp[]){
       Shape[] shapes= {new Circle("raster", 10, 40), new Square("vector", 2,2)};

        for(Shape s:shapes){
            s.draw();
        }
    }
}

I want to pass a Shape object to ShapeImp object like Vector or Raster. Error comes when trying to pass "this" from inside constructors of Circle and Square. I want to pass the concrete shapes to either Vector or Raster.

Netbeans error at line

super(platform, x,y, this, "Circle999");

"cannot reference this before supertype constructor has been called
Leaking this in constructor"

package dp.bridge;

//-------Abstraction-------//

//----Abstraction-Specialization----------//
abstract class Shape{

    protected ShapeImpl platform;
    protected String type;

    Shape(String p, int x, int y, Circle s, String type){
        this.type = type;
        if(p.equals("vector"))
            platform = new Vector(x,y,s);
         if(p.equals("raster"))
            platform = new Raster(x,y,s);
    }

    public String getType() {
        return type;
    }

    
     abstract public void draw();
}
class Circle extends Shape{



    Circle(String platform, int x, int y){
        super(platform, x,y, this, "Circle999");
        
    }

    public void draw(){
        System.out.println("Circle: draw()");
        platform.draw();
    }
    
}

class Square extends Shape{

     Square(String platform, int x, int y){
        super(platform, x,y,this, "Square778");
     }

    public void draw(){
        System.out.println("Square: draw()");
        platform.draw();
    }

}

//----Abstract-Implementation------//
interface ShapeImpl{
    public void draw();

}

//--------Concreate implemenations--------//
class Raster implements ShapeImpl{

    int _x;
    int _y;
    Shape s;
    Raster(int x, int y, Shape s){
        _x = x;
        _y = y;
        this.s = s;
    }

    public void draw(){
        System.out.println("Drawing Raster "+s.getType()+ " at (" +_x + "," + _y +")");

    }
}

class Vector implements ShapeImpl{

    int _x;
    int _y;
    Shape s;
    Vector(int x, int y, Shape s){
        _x = x;
        _y = y;
        this.s = s;

    }

    public void draw(){
        System.out.println("Drawing Vector "+s.getType()+ " at (" +_x + "," + _y +")");

    }


}

//-----Client-------//
class Client{

    
    public static void main(String atgsp[]){
       Shape[] shapes= {new Circle("raster", 10, 40), new Square("vector", 2,2)};

        for(Shape s:shapes){
            s.draw();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2024-12-20 13:22:37

您正在将一个对象传递给其自身?你不需要这样做(显然你也不能这样做)。超类中的 this 仍将解析为当前对象。

因此,不必将 this 作为参数传递给超级构造函数,只需在超级构造函数中使用 this new Vector(x, s ,这个)

You are passing an object to itself? You don't need to do that (and you can't, obsiouly). this in the superclass will still resolve to the current object.

So instead of passing this as argument to the super-constructor, simply use this in the superconstructor: new Vector(x, s, this)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文