Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类和具体类_Java_Oop_Polymorphism - Fatal编程技术网

Java 类和具体类

Java 类和具体类,java,oop,polymorphism,Java,Oop,Polymorphism,我想知道这些类之间的区别,在我的应用程序中,我需要设置一些参数并通过方法传递对象。我有一些只使用方法的接口(主要是DAO/工厂方法)。我对我的代码有一些疑问 我应该只使用接口和具体类吗 或者使用类(与Getter和Setter一起)并只实现方法 我应该把它们混在一起吗?这是个坏习惯吗 接口: public interface Animal { void roar(); } public interface UserDAOInterface { public int inser

我想知道这些类之间的区别,在我的应用程序中,我需要设置一些参数并通过方法传递对象。我有一些只使用方法的接口(主要是DAO/工厂方法)。我对我的代码有一些疑问

  • 我应该只使用接口和具体类吗
  • 或者使用类(与Getter和Setter一起)并只实现方法
  • 我应该把它们混在一起吗?这是个坏习惯吗
接口:

public interface Animal {
  void roar();
}
public interface UserDAOInterface {

    public int insert(Users user) throws SQLException;
    more DAO Operations...
}
具有接口中实现的方法的具体类

public Lion implements Animal {
@overrride
 void roar(){
    //roar like lion!
  }
}
具体类:我有一个用于构造对象以进行DAO操作的类

public class Lion {
  private String lionName;
  getter and setter...
  void roar(){
 //Roar like Lion.
 }
}
摘要:

abstract class Lion {
  abstract public void abstractRoar();
}

提前感谢,一如既往,我们非常感谢您的帮助。

它们都有不同的用例,您需要根据这些用例来决定哪些更适合您的需要


接口
的一般用途是定义一组相关方法,这些方法可以跨多个类实现和重用。以动物为例,在我看来,我会将
动物
作为父类,通过
狮子
老虎
等类进行扩展。此用例的接口可以是类似于
噪音
的东西。每种动物可能有不同的噪音,但是它们都会发出噪音。该界面可以有
咆哮
咆哮
嗅探
,等等。

它们都有不同的用例,您需要从中决定哪一个更适合您的需要


接口
的一般用途是定义一组相关方法,这些方法可以跨多个类实现和重用。以动物为例,在我看来,我会将
动物
作为父类,通过
狮子
老虎
等类进行扩展。此用例的接口可以是类似于
噪音
的东西。每种动物可能有不同的噪音,但是它们都会发出噪音。界面可以有
咆哮
咆哮
嗅探
,等等。

我有这样的东西:

类别用户:

public class Users {

private int userId;
private String userPassword;
private int userProfile;
private String userName;    
private String userPosition;
private int userCencos;
private String userOffice;

public Users(int userId, String userPassword, int userProfile, String userName, String userPosition, int userCencos, String userOffice) {
    Constructor..
}

public String getUserOffice() {
    return userOffice;
}
more getter/setter ...
接口:

public interface Animal {
  void roar();
}
public interface UserDAOInterface {

    public int insert(Users user) throws SQLException;
    more DAO Operations...
}
DAO用户:

public class UserDAOConcrete implements UserDAOInterface {

private int result;    
public PreparedStatement query;

@Override
public Users login(int userId, String password) throws NumberFormatException , SQLException{

    SQL sql = new SQL();
    try {            

        PreparedStatement loginQuery = sql.createPStatement(cf.SELECT_USER_BY_LOGIN_DATA);
        loginQuery.setInt(1, userId);
        loginQuery.setString(2, password);
        System.out.println(loginQuery.toString());

        try {
            ResultSet resultSet = loginQuery.executeQuery();
            if (!resultSet.isBeforeFirst()) {
                loginQuery.close();
                sql.dbConection().close();
                throw new NullPointerException("Usuario No encontrado");
            }
            while (resultSet.next()) {
                Users newLoggedUser = new Users();

                newLoggedUser.setUserId(resultSet.getInt("UserId"));
                newLoggedUser.setUserCencos(resultSet.getInt("CostCenter_CostCenterId"));
                newLoggedUser.setUserProfile(resultSet.getInt("Profiles_ProfileId"));                    
                newLoggedUser.setUserName(resultSet.getString("UserName"));
                newLoggedUser.setUserPosition(resultSet.getString("UserPosition"));
                newLoggedUser.setUserOffice(resultSet.getString("UserOffice"));
                resultSet.close();
                loginQuery.close();
                sql.dbConection().close();
                return newLoggedUser;
            }
        } catch (SQLException | NullPointerException | NumberFormatException e) {                
            throw e;
        } finally {
            try {
                loginQuery.close();
                sql.dbConection().close();
            } catch (SQLException e) {
                System.out.println("SQL Error: " + e);
            }
        }
        return null;
    } catch (SQLException ex) {
        System.out.println("SQL Error: " + ex);
    }
    return null;
}    
道场:

public class UserDAOFactory {

public static UserDAOConcrete getUserDAO(String type) { 
    if (type.equalsIgnoreCase("user")) {
        return new UserDAOConcrete();
    } else {
        return new UserDAOConcrete();
    }
}
调用方法:

UserDAOConcrete userDAO = UserDAOFactory.getUserDAO("user");
Users userData = userDAO.login(Integer.parseInt(loginUserId.getText()), String.valueOf(userLoginPassword.getPassword()));    
//Do whatever you need with the Object userData.

正如您所看到的,我混合了类、Getter/setter和接口,工作正常,我应该这样做所有代码吗?

我有这样的东西:

类别用户:

public class Users {

private int userId;
private String userPassword;
private int userProfile;
private String userName;    
private String userPosition;
private int userCencos;
private String userOffice;

public Users(int userId, String userPassword, int userProfile, String userName, String userPosition, int userCencos, String userOffice) {
    Constructor..
}

public String getUserOffice() {
    return userOffice;
}
more getter/setter ...
接口:

public interface Animal {
  void roar();
}
public interface UserDAOInterface {

    public int insert(Users user) throws SQLException;
    more DAO Operations...
}
DAO用户:

public class UserDAOConcrete implements UserDAOInterface {

private int result;    
public PreparedStatement query;

@Override
public Users login(int userId, String password) throws NumberFormatException , SQLException{

    SQL sql = new SQL();
    try {            

        PreparedStatement loginQuery = sql.createPStatement(cf.SELECT_USER_BY_LOGIN_DATA);
        loginQuery.setInt(1, userId);
        loginQuery.setString(2, password);
        System.out.println(loginQuery.toString());

        try {
            ResultSet resultSet = loginQuery.executeQuery();
            if (!resultSet.isBeforeFirst()) {
                loginQuery.close();
                sql.dbConection().close();
                throw new NullPointerException("Usuario No encontrado");
            }
            while (resultSet.next()) {
                Users newLoggedUser = new Users();

                newLoggedUser.setUserId(resultSet.getInt("UserId"));
                newLoggedUser.setUserCencos(resultSet.getInt("CostCenter_CostCenterId"));
                newLoggedUser.setUserProfile(resultSet.getInt("Profiles_ProfileId"));                    
                newLoggedUser.setUserName(resultSet.getString("UserName"));
                newLoggedUser.setUserPosition(resultSet.getString("UserPosition"));
                newLoggedUser.setUserOffice(resultSet.getString("UserOffice"));
                resultSet.close();
                loginQuery.close();
                sql.dbConection().close();
                return newLoggedUser;
            }
        } catch (SQLException | NullPointerException | NumberFormatException e) {                
            throw e;
        } finally {
            try {
                loginQuery.close();
                sql.dbConection().close();
            } catch (SQLException e) {
                System.out.println("SQL Error: " + e);
            }
        }
        return null;
    } catch (SQLException ex) {
        System.out.println("SQL Error: " + ex);
    }
    return null;
}    
道场:

public class UserDAOFactory {

public static UserDAOConcrete getUserDAO(String type) { 
    if (type.equalsIgnoreCase("user")) {
        return new UserDAOConcrete();
    } else {
        return new UserDAOConcrete();
    }
}
调用方法:

UserDAOConcrete userDAO = UserDAOFactory.getUserDAO("user");
Users userData = userDAO.login(Integer.parseInt(loginUserId.getText()), String.valueOf(userLoginPassword.getPassword()));    
//Do whatever you need with the Object userData.

正如您所看到的,我混合了类、Getter/setter和接口,工作正常,我应该这样做吗?

如果我理解正确,您的问题是:

是否每次创建类时都需要实现接口

答案是否定的。接口只用于抽象。如果几个类共享公共方法,使用接口将简化代码

有关接口的更多信息(我应该何时使用,如何使用),请参阅《有效Java》(第19项)。以下是一个链接:


希望能解决您的问题。

如果我理解正确,您的问题是:

是否每次创建类时都需要实现接口

答案是否定的。接口只用于抽象。如果几个类共享公共方法,使用接口将简化代码

有关接口的更多信息(我应该何时使用,如何使用),请参阅《有效Java》(第19项)。以下是一个链接:



希望这能解决您的问题。

您不能在接口中实现方法,我认为您缺少
抽象类
example@cricket_007哦,对不起,我的错。Java 8支持接口内部的方法实现。我错了@cricket_007吗?你的问题似乎与使用设计模式的实践有关。我建议您阅读
创造性设计模式
,因为您不能在接口中实现方法,我认为您缺少
抽象类
example@cricket_007哦,对不起,我的错。Java 8支持接口内部的方法实现。我错了@cricket_007吗?你的问题似乎与使用设计模式的实践有关。我建议您阅读《感谢澄清》中的
创造性设计模式。我可以使用我的类Lion及其getter/setter来构造对象和处理接口吗?。或者我需要在实现
Animal
的类中设置实例变量吗?@JuanSebastianOsorio我对你的意思有点困惑。你需要设置一个类的实例变量,或者在构造函数中,或者在实例化后通过setter设置。谢谢你的帮助。谢谢你的澄清。我可以使用我的类Lion及其getter/setter来构造对象和处理接口吗?。或者我需要在实现
Animal
的类中设置实例变量吗?@JuanSebastianOsorio我对你的意思有点困惑。你需要设置一个类的实例变量,或者在构造函数中,或者在实例化后通过setter设置。谢谢你的帮助。谢谢,我在看这本书。非常感谢你的回答对我帮助很大。谢谢,我在看这本书。非常感谢你的回答对我帮助很大。