Java 如何创建类的对象

Java 如何创建类的对象,java,Java,我认为这足以解释我的问题: public class Model { public static Model [] findAllBySQL(String SQL){ //this is simplified. It should really query the DB and then fill model(s) with the DB values, and return the model(s). sql query can return more than o

我认为这足以解释我的问题:

public class Model {
    public static Model [] findAllBySQL(String SQL){
        //this is simplified.  It should really query the DB and then fill model(s) with the DB values, and return the model(s).  sql query can return more than one row
        return new this(); //sytax error here
    }
}


public class UserModel extends Model {

}


UserModel.findAllBySQL("firstname=john") //How do I design the above so this returns a UserModel object?
我对Java比较陌生。我的背景主要是PHP。我正在尝试创建一个简单的自制活动记录系统。。我知道这是在重塑车轮,但我就是这样了解的:)


编辑:你们大多数人误解了我。我知道如何简化到
newusermodel()
。我更改了代码以使其更清晰。

为什么不简单地使用
new
操作符作为

UserModel userModel = new UserModel();
要了解在java中创建对象的不同方法,请参阅以下线程:


编辑

根据您的编辑,您可以做什么

public static Model findBySQL(String SQL){
        Model model = new Model();
        // Now query in db to get data. then use setter methods of Model to set data in object
        // i.e. model.setXXX(XXX);
        // and finally return that model object.
        return model;
    }

编辑2

UserModel可能有getFullName() 将第一个名称与 数据库中的姓氏。我会的 需要能够访问此方法 马上就返回的对象 来自findBySQL

您可以这样尝试:在UserModel中

public String getFullName(){
    Model model = findBySql("str='str'");
    return model.getFirstName()+"  "+model.getLastName();
}

为什么不简单地使用
new
操作符作为

UserModel userModel = new UserModel();
要了解在java中创建对象的不同方法,请参阅以下线程:


编辑

根据您的编辑,您可以做什么

public static Model findBySQL(String SQL){
        Model model = new Model();
        // Now query in db to get data. then use setter methods of Model to set data in object
        // i.e. model.setXXX(XXX);
        // and finally return that model object.
        return model;
    }

编辑2

UserModel可能有getFullName() 将第一个名称与 数据库中的姓氏。我会的 需要能够访问此方法 马上就返回的对象 来自findBySQL

您可以这样尝试:在UserModel中

public String getFullName(){
    Model model = findBySql("str='str'");
    return model.getFirstName()+"  "+model.getLastName();
}

您应该使用构造函数:

public class Model {
    //Constructor
    public Model()
    {
    // Do initialization stuff here
    }
}


public class UserModel extends Model {
    //Constructor
    public UserModel()
    {
    // Do initialization stuff here
    }
}
要创建新对象,可以这样称呼它:

UserModel myUserModel;    // Declare new object reference
myUserModel = new UserModel();   // create new object of this class
编辑:

如果将方法声明为返回
Model
s的方法数组,则不能返回单个模型,可以返回带有一个
Model
Model
数组,但不能返回单个对象

比如说

public static Model [] findAllBySQL(String SQL){
    // find how many models do you have
    Model[] models = new Model[numberOfModels];
    for (Model model : models)
    {
         model = new Model();
         //do what you want with it...
    }
    return models; //sytax error here
}

您应该使用构造函数:

public class Model {
    //Constructor
    public Model()
    {
    // Do initialization stuff here
    }
}


public class UserModel extends Model {
    //Constructor
    public UserModel()
    {
    // Do initialization stuff here
    }
}
要创建新对象,可以这样称呼它:

UserModel myUserModel;    // Declare new object reference
myUserModel = new UserModel();   // create new object of this class
编辑:

如果将方法声明为返回
Model
s的方法数组,则不能返回单个模型,可以返回带有一个
Model
Model
数组,但不能返回单个对象

比如说

public static Model [] findAllBySQL(String SQL){
    // find how many models do you have
    Model[] models = new Model[numberOfModels];
    for (Model model : models)
    {
         model = new Model();
         //do what you want with it...
    }
    return models; //sytax error here
}

这就是你使用它的方式

UserModel m = new UserModel();
UserModel m现在拥有来自
UserModel的所有函数/值+来自Model的函数/值。

范例

public class Model {
    public Model()
    {
    // Do initialization stuff here
    }

    public getVal(){
        return 1+1;
    }
}


public class UserModel extends Model {
    public UserModel()
    {
    // Do initialization stuff here
    }

    public getValue2(){
        return 2+2;
    }
}


UserModel m = new UserModel();

m.getVal(); //Will return 2
m.getValue2(); // Will return 4

这就是你使用它的方式

UserModel m = new UserModel();
UserModel m现在拥有来自
UserModel的所有函数/值+来自Model的函数/值。

范例

public class Model {
    public Model()
    {
    // Do initialization stuff here
    }

    public getVal(){
        return 1+1;
    }
}


public class UserModel extends Model {
    public UserModel()
    {
    // Do initialization stuff here
    }

    public getValue2(){
        return 2+2;
    }
}


UserModel m = new UserModel();

m.getVal(); //Will return 2
m.getValue2(); // Will return 4
听起来你需要这个。如果您有一个模型超类和几种类型的模型,那么您可以使用createModel()方法将对象创建封装在ModelFactory类中,该方法根据参数将适当类型的模型返回给该方法

 class ModelFactory(){
       public Model createModel(String sql, String type){
            //execute SQL

            if(type.equals("user")){
                 UserModel model = new UserModel();
                 //set the attributes here
                 return model;
            }

            if(type.equals("other")){
                 OtherModel model = new OtherModel();
                 //set attributes here
                 return model;
            }

             //etc
       }
 }
要实际获取模型对象,您现在可以:

       ModelFactory factory = new ModelFactory();
       Model m = factory.createModel("select * from mytable", "user");
听起来你需要这个。如果您有一个模型超类和几种类型的模型,那么您可以使用createModel()方法将对象创建封装在ModelFactory类中,该方法根据参数将适当类型的模型返回给该方法

 class ModelFactory(){
       public Model createModel(String sql, String type){
            //execute SQL

            if(type.equals("user")){
                 UserModel model = new UserModel();
                 //set the attributes here
                 return model;
            }

            if(type.equals("other")){
                 OtherModel model = new OtherModel();
                 //set attributes here
                 return model;
            }

             //etc
       }
 }
要实际获取模型对象,您现在可以:

       ModelFactory factory = new ModelFactory();
       Model m = factory.createModel("select * from mytable", "user");

您可以这样做:

import java.util.ArrayList;
import java.util.List;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class Generics<T> {

    //method that returns a generic list
    public List<T> findAllBySQL(String SQL) {
        throw new NotImplementedException();
    }

    public static void main(String[] args) {
        //base class that returns a list of childs
        Generics result = new ExtendedGenerics(null);
        List<ExtendedGenerics> genericArray = result.findAllBySQL(null);
        for (ExtendedGenerics item : genericArray) {
            System.out.println(item);
        }
    }
}

//extend base class and specify generic type
class ExtendedGenerics extends Generics<ExtendedGenerics> {

    private String myMessage;

    public ExtendedGenerics(String yourMessage) {
        myMessage = yourMessage;
    }

    @Override
    //overriding base method, so it return a instances list of this class 
    public List<ExtendedGenerics> findAllBySQL(String SQL) {
        ArrayList<ExtendedGenerics> result = new ArrayList<ExtendedGenerics>();
        result.add(new ExtendedGenerics("I am first"));
        result.add(new ExtendedGenerics("I am second"));
        result.add(new ExtendedGenerics("I am third"));
        return result;
    }

    @Override
    public String toString() {
        return "ExtendedGenerics{" + myMessage + '}';
    }
}

您可以这样做:

import java.util.ArrayList;
import java.util.List;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

public class Generics<T> {

    //method that returns a generic list
    public List<T> findAllBySQL(String SQL) {
        throw new NotImplementedException();
    }

    public static void main(String[] args) {
        //base class that returns a list of childs
        Generics result = new ExtendedGenerics(null);
        List<ExtendedGenerics> genericArray = result.findAllBySQL(null);
        for (ExtendedGenerics item : genericArray) {
            System.out.println(item);
        }
    }
}

//extend base class and specify generic type
class ExtendedGenerics extends Generics<ExtendedGenerics> {

    private String myMessage;

    public ExtendedGenerics(String yourMessage) {
        myMessage = yourMessage;
    }

    @Override
    //overriding base method, so it return a instances list of this class 
    public List<ExtendedGenerics> findAllBySQL(String SQL) {
        ArrayList<ExtendedGenerics> result = new ArrayList<ExtendedGenerics>();
        result.add(new ExtendedGenerics("I am first"));
        result.add(new ExtendedGenerics("I am second"));
        result.add(new ExtendedGenerics("I am third"));
        return result;
    }

    @Override
    public String toString() {
        return "ExtendedGenerics{" + myMessage + '}';
    }
}
您可以使用plus:

您可以使用plus:



@Jonah-请看编辑后的答案,这就是您的意思吗?您是否希望它查询SQL数据库并返回一个填充了该数据库中的值的对象?如果是这样的话,请查看名为“Hibernate”的库。Java没有任何内置的SQL集成。@Bill我听说过Hibernate,但我宁愿创建自己的库,以便更好地学习Java,因为我现在需要一些非常简单的东西。另外,我正在使用java.SQL。在这种情况下,您需要使用SQL类库(like)来执行查询,然后创建一个与表中的属性(变量)完全相同的类。然后手动将查询中的每个变量复制到类中。这正是他们创建hibernate的原因,hibernate就是这么做的。我正在考虑将查询结果存储到模型中的HashMap中。然后在模型类中会有一个getAttribute(String属性)方法,该方法返回属性。我不能说这是比Hibernate更好的方法。。但它并不要求您将所有表属性列为类attributes@Jonah-请参阅编辑后的答案,这就是您的意思吗?您是否希望它查询SQL数据库并返回一个填充了该数据库中的值的对象?如果是这样的话,请查看名为“Hibernate”的库。Java没有任何内置的SQL集成。@Bill我听说过Hibernate,但我宁愿创建自己的库,以便更好地学习Java,因为我现在需要一些非常简单的东西。另外,我正在使用java.SQL。在这种情况下,您需要使用SQL类库(like)来执行查询,然后创建一个与表中的属性(变量)完全相同的类。然后手动将查询中的每个变量复制到类中。这正是他们创建hibernate的原因,hibernate就是这么做的。我正在考虑将查询结果存储到模型中的HashMap中。然后在模型类中会有一个getAttribute(String属性)方法,该方法返回属性。我不能说这是比Hibernate更好的方法。。但它并不要求您将所有表属性列为类属性。这将不行,因为我无法在每个模型子类中重新定义findBySQL()。。试图在这里保持干燥..@Jonah-这就是你想要的吗?这不行,因为我不能在每个模型子类中重新定义findBySQL()。。在这里尽量保持干燥。@Jonah-这就是你想要的吗?我不这么认为。。这将返回一个模型对象。。但我需要一个调用者类型的对象。@Jonah:因为
UserModel
扩展了
Model
您可以轻松地将其解析为调用者。或者您可以将返回类型更改为caller。不确定您的意思(记住,我是Java新手)。我需要