Java 如何创建扩展类的实例?
我有一个名为Java 如何创建扩展类的实例?,java,object,inheritance,extends,Java,Object,Inheritance,Extends,我有一个名为Item的类。它具有以下参数: 用户所有者,类别和字符串描述 接下来,我有一个名为Painting的子类,它扩展了Item类。绘制有两个参数:标题和绘制 在代码中的某个时刻,我想创建一个绘画对象。应该可以从如下所示的测试文件运行代码: User u1 = new User ("test@test.com"); Category cat = new Category("cat2"); Item painting = sellerMgr.offerPainting(u1, cat, "S
Item
的类。它具有以下参数:
用户所有者
,类别
和字符串描述
接下来,我有一个名为Painting
的子类,它扩展了Item类。绘制
有两个参数:标题
和绘制
在代码中的某个时刻,我想创建一个绘画对象。应该可以从如下所示的测试文件运行代码:
User u1 = new User ("test@test.com");
Category cat = new Category("cat2");
Item painting = sellerMgr.offerPainting(u1, cat, "Selfportret", "Rembrandt");
Painting p = new Painting(u1, cat, title, painter);
public class Painting extends Item {
private String title;
private String painter;
public Painting(User owner, Category category, String title, String painter){
super(owner, category);
this.title = title;
this.painter = painter;
}
}
类sellerMgr
中应该有代码,它应该能够注册(创建)该项目(作为绘画),因此我可以在数据库中使用它。我该如何准确地调用该代码?无论何时创建新的项目
或新的绘画
,以及在创建代码中添加哪些参数,我都会感到困惑。您可能希望这样:
User u1 = new User ("test@test.com");
Category cat = new Category("cat2");
Item painting = sellerMgr.offerPainting(u1, cat, "Selfportret", "Rembrandt");
Painting p = new Painting(u1, cat, title, painter);
public class Painting extends Item {
private String title;
private String painter;
public Painting(User owner, Category category, String title, String painter){
super(owner, category);
this.title = title;
this.painter = painter;
}
}
假设您的类定义如下所示:
User u1 = new User ("test@test.com");
Category cat = new Category("cat2");
Item painting = sellerMgr.offerPainting(u1, cat, "Selfportret", "Rembrandt");
Painting p = new Painting(u1, cat, title, painter);
public class Painting extends Item {
private String title;
private String painter;
public Painting(User owner, Category category, String title, String painter){
super(owner, category);
this.title = title;
this.painter = painter;
}
}
也许你想要这样的东西:
User u1 = new User ("test@test.com");
Category cat = new Category("cat2");
Item painting = sellerMgr.offerPainting(u1, cat, "Selfportret", "Rembrandt");
Painting p = new Painting(u1, cat, title, painter);
public class Painting extends Item {
private String title;
private String painter;
public Painting(User owner, Category category, String title, String painter){
super(owner, category);
this.title = title;
this.painter = painter;
}
}
假设您的类定义如下所示:
User u1 = new User ("test@test.com");
Category cat = new Category("cat2");
Item painting = sellerMgr.offerPainting(u1, cat, "Selfportret", "Rembrandt");
Painting p = new Painting(u1, cat, title, painter);
public class Painting extends Item {
private String title;
private String painter;
public Painting(User owner, Category category, String title, String painter){
super(owner, category);
this.title = title;
this.painter = painter;
}
}
绘制
构造函数应将类别
和用户
作为参数,并将它们传递给调用“Item”构造函数的super
比如:
公共绘画(用户,类别){
超级(用户、类别);
…
}
绘制
构造函数应将类别
和用户
作为参数,并将它们传递给调用“项”构造函数的超级
比如:
公共绘画(用户,类别){
超级(用户、类别);
…
}
在这种情况下,sellerMgr.OfferPaint应创建并返回绘画类型的对象。绘画可以投射到一个项目,但在大多数情况下,您应该使用扩展类,它比项目具有更多的功能 在这种情况下,sellerMgr.OfferPaint应创建并返回一个绘画类型的对象。绘画可以投射到一个项目,但在大多数情况下,您应该使用扩展类,它比项目具有更多的功能 你有了一个新班级
public class Painting extends Item
您需要一个提供两个新参数的构造函数:字符串标题和用户画师
public Painting(User owner, Category category, String description, String title, User painter){
super(owner, category, description);
this.title = title;
this.painter = painter
}
每当您需要一个新的Painter实例时,您都可以调用此方法,该方法将为您设置任何项变量,同时引入所需的两个新参数。一个电话可能看起来像
Item paintingAsItem = new Painting(u1, cat, "desc", "Selfportret", "Rembrandt"); //Generic
Painting painting = new Paining(u1, cat, "desc", "Selfportret", "Rembrandt");
你有一个新班级
public class Painting extends Item
您需要一个提供两个新参数的构造函数:字符串标题和用户画师
public Painting(User owner, Category category, String description, String title, User painter){
super(owner, category, description);
this.title = title;
this.painter = painter
}
每当您需要一个新的Painter实例时,您都可以调用此方法,该方法将为您设置任何项变量,同时引入所需的两个新参数。一个电话可能看起来像
Item paintingAsItem = new Painting(u1, cat, "desc", "Selfportret", "Rembrandt"); //Generic
Painting painting = new Paining(u1, cat, "desc", "Selfportret", "Rembrandt");
你在你的提供绘画方法中尝试了什么?听起来你已经有了正确的想法…你在你的提供绘画方法中尝试了什么?听起来你已经有了正确的想法…我忘记的部分是把项目参数放在绘画的构造器中。我已经想知道如何通过创建一幅绘画将这些参数的值输入到项目中。非常感谢。我忘记的部分是把项目参数放在绘画的构造器中。我已经想知道如何通过创建一幅绘画将这些参数的值输入到项目中。非常感谢。