Java 如何正确组织班级代码?
如果需要通过私有方法修改数据,那么在类中组织代码的首选方法是什么 例如,这样做是否更好:Java 如何正确组织班级代码?,java,code-organization,Java,Code Organization,如果需要通过私有方法修改数据,那么在类中组织代码的首选方法是什么 例如,这样做是否更好: private String data1; callMethod(data1); private void callMethod(String stuff) { // Do stuff to data1 } 或者这个: private String data1; callMethod(); private void callMethod() { // Do stuff to d
private String data1;
callMethod(data1);
private void callMethod(String stuff) {
// Do stuff to data1
}
或者这个:
private String data1;
callMethod();
private void callMethod() {
// Do stuff to data1
}
我已经看到它是以各种方式完成的,我正试图了解什么是行业标准的最佳实践,因为我是发展中国家的新手。如果数据是对象私有的,则该方法可以完全访问它。你不应该把它传进来
对象将状态和行为封装到单个软件模块中。操作应操纵状态。如果数据是对象的私有数据,则该方法具有对该数据的完全访问权限。你不应该把它传进来
对象将状态和行为封装到单个软件模块中。操作应该操纵状态。如果您知道这个参数是类私有成员,那么我不认为使用参数创建私有函数有什么意义。如果函数是特定的,并且其结果取决于成员状态,那么我将始终使用第二个选项。如果您知道此参数是类私有成员,我不认为使用参数创建私有函数有什么意义。如果函数是特定的,其结果取决于成员的状态,那么我将始终选择第二个选项。它实际上取决于数据、方法和您尝试执行的操作。换句话说,这是课程设计的一部分 这个私有方法如何修改数据?如果它执行的特定计算仅对
data1
字段有意义,那么您只需使用callMethod()
另一方面,如果您的callMethod()
可能是类中的一个小实用程序(可能可以在两个不同的字段data1
和data2
上执行相同的计算),那么不使用两个单独的方法而是将要修改的成员作为参数传递是有意义的
如果您有一个具体的例子,那么我们可能会提供更多帮助,这实际上取决于数据、方法和您正在尝试的操作。换句话说,这是课程设计的一部分 这个私有方法如何修改数据?如果它执行的特定计算仅对
data1
字段有意义,那么您只需使用callMethod()
另一方面,如果您的callMethod()
可能是类中的一个小实用程序(可能可以在两个不同的字段data1
和data2
上执行相同的计算),那么不使用两个单独的方法而是将要修改的成员作为参数传递是有意义的
如果您有一个特定的示例,那么我们可能会提供更多帮助如果方法已经知道它必须访问的成员,那么在内部传递引用是没有意义的
class FooHolder {
private Foo foo;
private void ensureInitialized() {
if (foo == null)
foo = new Foo();
}
public Foo getFoo() {
ensureInitialized();
return foo;
}
}
但是,如果您可以通过这种方式防止代码重复,那么这样做有时是有用的。这些内部实用程序方法有时可能是静态的,如下所示:
class FooAndBar {
private List<Foo> foos;
private List<Bar> bars;
public void addFoo(Foo foo) {
foos = ensureList(foos);
foos.add(foo);
}
public void addBar(Bar bar) {
bars = ensureList(bars);
bars.add(bar);
}
// assume this method is not generically useful and therefore not better off in a utility class
private static <T> List<T> ensureList(List<T> list) {
return list != null ? list : new ArrayList<T>();
}
}
class FooAndBar{
私人名单;
私人酒吧名单;
公共无效添加Foo(Foo-Foo){
foos=保险清单(foos);
添加(foo);
}
公共无效添加栏(栏){
钢筋=钢筋清单(钢筋);
条。添加(条);
}
//假设此方法在一般情况下不有用,因此在实用程序类中也不会更好
私有静态列表ensureList(列表列表){
返回列表!=null?列表:新建ArrayList();
}
}
有时他们不能/不应该这样做
class FooFoos {
private final Map<String, List<Foo>> fooMap = new HashMap<String, List<Foo>>();
private List<Foo> getListForKey(String key) {
List<Foo> list = fooMap.get(key);
if (list == null) {
list = new ArrayList<Foo>();
fooMap.put(key, list);
}
return list;
}
public void addFoo(String key, Foo foo) {
getListForKey(key).add(foo);
}
public List<Foo> getList(String key) {
return getListForKey(key);
}
}
class foos{
私有最终映射fooMap=newhashmap();
私有列表getListForKey(字符串键){
List=fooMap.get(键);
if(list==null){
列表=新的ArrayList();
fooMap.put(键、列表);
}
退货清单;
}
公共void addFoo(字符串键,Foo-Foo){
getListForKey(key).add(foo);
}
公共列表getList(字符串键){
返回getListForKey(键);
}
}
请注意,getListForKey
没有传递对fooMap
的引用。没有必要,因为这部分已经很清楚了,在每个方法中键入它只会使代码混乱
如果您可以通过这种方式实现更少的代码重复和一些内部封装,请将引用传递给您的私有方法。但是如果这会导致更多的代码,请不要这样做,因为每个方法都必须再次指定引用
还注意到,许多通过方法封装内部的功能意味着您应该考虑将该功能重构为另一个类。对于最后一个例子,考虑使用/创建一些类似于
的方法,如果该方法已经知道要访问的成员,则在内部传递引用是没有意义的。class FooHolder {
private Foo foo;
private void ensureInitialized() {
if (foo == null)
foo = new Foo();
}
public Foo getFoo() {
ensureInitialized();
return foo;
}
}
但是,如果您可以通过这种方式防止代码重复,那么这样做有时是有用的。这些内部实用程序方法有时可能是静态的,如下所示:
class FooAndBar {
private List<Foo> foos;
private List<Bar> bars;
public void addFoo(Foo foo) {
foos = ensureList(foos);
foos.add(foo);
}
public void addBar(Bar bar) {
bars = ensureList(bars);
bars.add(bar);
}
// assume this method is not generically useful and therefore not better off in a utility class
private static <T> List<T> ensureList(List<T> list) {
return list != null ? list : new ArrayList<T>();
}
}
class FooAndBar{
私人名单;
私人酒吧名单;
公共无效添加Foo(Foo-Foo){
foos=保险清单(foos);
添加(foo);
}
公共无效添加栏(栏){
钢筋=钢筋清单(钢筋);
条。添加(条);
}
//假设此方法在一般情况下不有用,因此在实用程序类中也不会更好
私有静态列表ensureList(列表列表){
返回列表!=null?列表:新建ArrayList();
}
}
有时他们不能/不应该这样做
class FooFoos {
private final Map<String, List<Foo>> fooMap = new HashMap<String, List<Foo>>();
private List<Foo> getListForKey(String key) {
List<Foo> list = fooMap.get(key);
if (list == null) {
list = new ArrayList<Foo>();
fooMap.put(key, list);
}
return list;
}
public void addFoo(String key, Foo foo) {
getListForKey(key).add(foo);
}
public List<Foo> getList(String key) {
return getListForKey(key);
}
}
class foos{
私有最终映射fooMap=newhashmap();
私有列表getListForKey(字符串键){
List=fooMap.get(键);
if(list==null){
列表=新的ArrayList();
fooMap.put(键、列表);
}
退货清单;
}
公共void addFoo(字符串键,Foo-Foo){
getListForKey(key).add(foo);
}
公共列表getList(字符串键){
返回getListForKey(键);
}
}
注意getList