请教对象实例化的问题

szjeff2016 2018-07-12 10:04:25
在程序设计中,遇到一种场景,对某个对象要实例化,但在写程序的时候并不确定要实例化哪种对象

比如:
abstract class Parent{}
class Son1 extends Parent{}
class Son2 extends Parent{}

在设计时,先定义一个对象,如Parent p; 但要实例化时,目前并不知道是Son1还是Son2,想通过参数传递的方式来解决,请教大神用什么方法可以搞定,最好能给出实例化的代码,多谢了!
...全文
340 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
szjeff2016 2018-07-15
  • 打赏
  • 举报
回复
多谢各位,采用二楼的方法,用实例判断的方法,参数通过Object传递,然后再进行强制转换可以解决此问题
Frank6600 2018-07-14
  • 打赏
  • 举报
回复
if (条件1)
parent=new ActionA(ItemA(para));
else
parent=new ActionB(ItemB(para));
parent.createList(n);

实现Action构造函数
public Action(Item item){this.item=item}

实现Action.createList(int num){
for (int i=0; i<num; i++)
insertItem(item);
}

实现Action.insertItem(Item item){};



是这个意思?

Frank6600 2018-07-14
  • 打赏
  • 举报
回复
if (条件1)
parent=new ActionA(ItemA(para));
else
parent=new ActionB(ItemB(para));

实现Action构造函数
public Action(Item item){this.item=item}

实现Action.createList(int num){
for (int i=0; i<num; i++)
insertItem(item);

实现Action.insertItem(Item item);

是这个意思?

}
szjeff2016 2018-07-14
  • 打赏
  • 举报
回复
具体的场景如下,ActionA和ActionB都是继承Action的,里面的InsertItem除了要实例化有差别外,其它都一样,所以想在Action里面实现InsertItem,如果我用new ActionA.InsertItem(),就能直接插入ItemA,如果用new ActionB.InsertItem()就能直接插入ItemB。而不是象下面的程序重复太多:
abstract class Action{
void InsertItem(Item item){
}
}

class ActionA extends Action{
void CreateList(int num, int para){
for(int i = 0; i < num; i ++){
InsertItem(new ItemA(para));
}
}
}

class ActionB extends Action{
void CreateList(int num, double para){
for(int i = 0; i < num; i ++){
InsertItem(new ItemB(para));
}
}
}
abstract class Item{
}

class ItemA extends Item{
ItemA(int i){}
}

class ItemB extends Item{
ItemB(double d){}
}


二楼的思路通过新建一个类去实例化,但不知道如何传递对象构造时的参数。工厂模式感觉也无法解决构造参数的传递。
写代码的陈皮 2018-07-13
  • 打赏
  • 举报
回复
引用 5 楼 weixin_38500325 的回复:
这让我想起了工厂模式。
详细地址:http://www.runoob.com/design-pattern/factory-pattern.html
下面是具体的代码。
//创建一个接口
public interface Shape {
void draw();
}
//实现接口的实体类
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
//创建工厂,生产给定信息的实体类
public class ShapeFactory {
//使用 getShape 方法获取形状类型的对象
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}

}
//创建测试类
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
//获取 Circle 的对象,并调用它的 draw 方法
Shape shape1 = shapeFactory.getShape("CIRCLE");
//调用 Circle 的 draw 方法
shape1.draw();
//获取 Rectangle 的对象,并调用它的 draw 方法
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//调用 Rectangle 的 draw 方法
shape2.draw();
//获取 Square 的对象,并调用它的 draw 方法
Shape shape3 = shapeFactory.getShape("SQUARE");
//调用 Square 的 draw 方法
shape3.draw();
}
}

红色字体是你想要的部分
写代码的陈皮 2018-07-13
  • 打赏
  • 举报
回复
这让我想起了工厂模式。
详细地址:http://www.runoob.com/design-pattern/factory-pattern.html
下面是具体的代码。
//创建一个接口
public interface Shape {
void draw();
}
//实现接口的实体类
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
//创建工厂,生产给定信息的实体类
public class ShapeFactory {
//使用 getShape 方法获取形状类型的对象
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}

}
//创建测试类
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
//获取 Circle 的对象,并调用它的 draw 方法
Shape shape1 = shapeFactory.getShape("CIRCLE");
//调用 Circle 的 draw 方法
shape1.draw();
//获取 Rectangle 的对象,并调用它的 draw 方法
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//调用 Rectangle 的 draw 方法
shape2.draw();
//获取 Square 的对象,并调用它的 draw 方法
Shape shape3 = shapeFactory.getShape("SQUARE");
//调用 Square 的 draw 方法
shape3.draw();
}
}
maradona1984 2018-07-13
  • 打赏
  • 举报
回复
我只关心 "遇到一种场景" 这个场景是什么?
当然一般情况下,最好使用泛型能比较优雅的解决问题
stacksoverflow 2018-07-13
  • 打赏
  • 举报
回复

package test;

abstract class Parent{}
class Son1 extends Parent{}
class Son2 extends Parent{}

public class Test extends Object{

public Parent newInstanse(Class<? extends Parent> clz){
try {
Parent p = clz.newInstance();
return p;
} catch (Exception e) {
return null;
}
}

public static void main(String[] args){
Test test = new Test();
Parent s1 = test.newInstanse(Son1.class);
Parent s2 = test.newInstanse(Son2.class);

System.out.println(s1);
System.out.println(s2);
}
}


------------------------
新开知识星球(ID:7660766),欢迎大家捧场。
Java学习不走弯路-让初学者不走弯路简单快捷的完成Java的学习。
------------------------
qq_39099280 2018-07-13
  • 打赏
  • 举报
回复
class Son3{
public static Parent parent;
public static Parent getObject(Object obj) {
if(obj instanceof Son1) {
parent = new Son1();
}
if(obj instanceof Son2) {
parent = new Son2();
}
return parent;
}
}


class Son4{
public static void main(String[] args) {
//看实际情况使用
Parent parent = Son3.getObject(Son1.class);
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧