java 正确理解 OOP 的概念
java getting concept of OOP right
大家好,我已经搜索了很多,但对我的搜索结果并不满意。希望这是问这个问题的正确地方。
我现在正在做 Java 一小段时间(从 C 改过来),但在掌握如何构建最适合 OOP 的代码方面遇到了问题。
举个简单的例子:
如果我使用一些预定义的字符串(比方说文件路径或错误消息),我目前正在创建自己的 class 做类似的事情:
private static final String libPath = "\this\is\a\path\";
private static final String notFoundMessage = "This hasn't been found";
public static String getLibPath() {
return libPath;
}
public static final String getNotFoundMessage() {
return notFoundMessage;
}
...
创建一个地图,将所有内容添加到其中并通过键获取它会更好吗?
还是我完全错了?
第二个例子:
假设我 return 某处出现错误字符串
public String getSomething() {
if (something != null) {
return something;
} else {
//handle error, return string below
}
return "I HAVE AN ERROR";
}
在我程序的其他任何地方,我正在检查 return 值:
if (!string.equals("I HAVE AN ERROR")) {
//do something
}
else {
// handle error
}
一旦错误消息发生变化,必须将代码更改两次,这显然是一种糟糕的方法。是的,我可以像在第一个示例中那样定义错误字符串,但是因为我对那个不满意,所以我走到了死胡同。
很高兴听到您对如何正确执行 OOP 的一些建议!
第一个例子:
private static final String libPath = "\this\is\a\path\";
private static final String notFoundMessage = "This hasn't been found";
public static String getLibPath() {
return libPath;
}
public static final String getNotFoundMessage() {
return notFoundMessage;
}
...
在这种情况下,无需创建地图。这是正确的做法。请注意 libPath
最好这样定义:
private static final Path libPath = Paths.get("this", "is", "a", "path");
(class Path
存在于 Java 7,当前版本是 Java 8)
第二个例子:
public String getSomething() {
if (something != null) {
return something;
} else {
//handle error, return string below
}
return "I HAVE AN ERROR";
}
否:从不 return Java 中的错误代码 。更喜欢使用异常。
示例:
public class ElementNotFoundException extends Exception {
...
}
public String getSomething() {
if (something == null) {
throw new ElementNotFoundException();
} else {
return something;
}
}
然后,你像这样处理异常:
try {
myObject.getSomething();
} catch(ElementNotFoundException e) {
//handle error
}
第一个例子,看看国际化:http://docs.oracle.com/javase/tutorial/i18n/
您可以使用静态或地图,但迟早需要以多种语言显示消息。
对于第二个示例,最好使用异常,因为它们旨在在发生异常情况(如错误)时使用。
无论如何,注意不要将其用作流控制结构:Why not use exceptions as regular flow of control?
以下是在代码中处理常量的一些示例:
1。 Class
public final class MyConstants {
public static final int ERROR_CODE = -1;
}
if (getSomething() == MyConstants.ERROR_CODE) {
// ...
}
2。界面
public interface MyConstantsHolder {
int ERROR_CODE = -1;
}
public MyClass implements MyConstantsHolder {
public void myMethod() {
if (getSomething() == ERROR_CODE) {
// ...
}
}
}
大家好,我已经搜索了很多,但对我的搜索结果并不满意。希望这是问这个问题的正确地方。
我现在正在做 Java 一小段时间(从 C 改过来),但在掌握如何构建最适合 OOP 的代码方面遇到了问题。
举个简单的例子:
如果我使用一些预定义的字符串(比方说文件路径或错误消息),我目前正在创建自己的 class 做类似的事情:
private static final String libPath = "\this\is\a\path\";
private static final String notFoundMessage = "This hasn't been found";
public static String getLibPath() {
return libPath;
}
public static final String getNotFoundMessage() {
return notFoundMessage;
}
...
创建一个地图,将所有内容添加到其中并通过键获取它会更好吗? 还是我完全错了?
第二个例子: 假设我 return 某处出现错误字符串
public String getSomething() {
if (something != null) {
return something;
} else {
//handle error, return string below
}
return "I HAVE AN ERROR";
}
在我程序的其他任何地方,我正在检查 return 值:
if (!string.equals("I HAVE AN ERROR")) {
//do something
}
else {
// handle error
}
一旦错误消息发生变化,必须将代码更改两次,这显然是一种糟糕的方法。是的,我可以像在第一个示例中那样定义错误字符串,但是因为我对那个不满意,所以我走到了死胡同。
很高兴听到您对如何正确执行 OOP 的一些建议!
第一个例子:
private static final String libPath = "\this\is\a\path\";
private static final String notFoundMessage = "This hasn't been found";
public static String getLibPath() {
return libPath;
}
public static final String getNotFoundMessage() {
return notFoundMessage;
}
...
在这种情况下,无需创建地图。这是正确的做法。请注意 libPath
最好这样定义:
private static final Path libPath = Paths.get("this", "is", "a", "path");
(class Path
存在于 Java 7,当前版本是 Java 8)
第二个例子:
public String getSomething() {
if (something != null) {
return something;
} else {
//handle error, return string below
}
return "I HAVE AN ERROR";
}
否:从不 return Java 中的错误代码 。更喜欢使用异常。
示例:
public class ElementNotFoundException extends Exception {
...
}
public String getSomething() {
if (something == null) {
throw new ElementNotFoundException();
} else {
return something;
}
}
然后,你像这样处理异常:
try {
myObject.getSomething();
} catch(ElementNotFoundException e) {
//handle error
}
第一个例子,看看国际化:http://docs.oracle.com/javase/tutorial/i18n/
您可以使用静态或地图,但迟早需要以多种语言显示消息。
对于第二个示例,最好使用异常,因为它们旨在在发生异常情况(如错误)时使用。
无论如何,注意不要将其用作流控制结构:Why not use exceptions as regular flow of control?
以下是在代码中处理常量的一些示例:
1。 Class
public final class MyConstants {
public static final int ERROR_CODE = -1;
}
if (getSomething() == MyConstants.ERROR_CODE) {
// ...
}
2。界面
public interface MyConstantsHolder {
int ERROR_CODE = -1;
}
public MyClass implements MyConstantsHolder {
public void myMethod() {
if (getSomething() == ERROR_CODE) {
// ...
}
}
}