尝试在 Java 中实现 Bridge 模式时出错
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将一个对象传递给其自身?你不需要这样做(显然你也不能这样做)。超类中的
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 usethis
in the superconstructor:new Vector(x, s, this)