Java - 有一个方法可以将私有字段值传递给另一个类的方法吗?

时间:2013-04-09 10:27:25

标签: java class parameter-passing field private-members

我有一个包含大量字段的类,所有字段都是私有的(子类使用受保护的getter访问一些字段)。我需要将大多数这些字段传递给另一个类中的方法,该方法将格式化它们并生成输出。在类中有一个方法可以将它们全部传递出来吗?或者这些情况是否表明我应该在两个类之间实现其他一些关系,因为它们似乎因为这个而紧密耦合?

更多信息:A类代表员工,B类唯一的责任是格式化程序的输出。

7 个答案:

答案 0 :(得分:6)

您是否在询问是否可以执行以下操作?

public class A {
 private B myB = new B();
 private String myUnformattedName = "some information";

 public String getFormattedInfo() {
   return myB.formatInfo(myUnformattedName);
 }
}

那完全没问题。

Marking a field as private just means that only the containing class should be able to access it...

如果你的意思是别的,最好在你的问题中弹出一些代码给人们上下文


好的,所以没有办法在这里设置值,但你可以看到两种不同的方法来调用格式化程序。当参数列表超过三个或四个项目时,就很难阅读。

在这种情况下,我只是将A传递给格式化程序,并为您希望B能够读取的每个值都有一个get方法。

public class A {
 private B myB = new B();
 private String myUnformattedName = "some information";
 private String myUnformattedNameOne = "some information";
 private String myUnformattedNameTwo = "some information";
 private String myUnformattedNameThree = "some information";
 private String myUnformattedNameFour = "some information";
 private String myUnformattedNameFive = "some information";
 private String myUnformattedNameSix = "some information";

 public String getFormattedInfo() {
   //pass the object itself and use get methods
   return myB.formatInfo(this); 
 }

 public String getFormattedInfoLong() {
   //this is OK but gets difficult to read the longer the 
   //parameter list gets
   return myB.formatInfo(myUnformattedName, myUnformattedNameOne, 
      myUnformattedTwo, myUnformattedNameThree, myUnformattedNameFour,
      myUnformattedNameFive, myUnformattedNameSix); 
 }

 //getters
 public String getUnformattedName() {
    return myUnformattedName;
 }

 public String getUnformattedNameOne() {
    return myUnformattedNameOne;
 }

 //etc

}

答案 1 :(得分:5)

我实际上会建议访客模式。

A类有一个接受访问者的方法,访问者又有一个明确定义的公共方法,例如:

首先,被访问类允许一些具有良好定义接口的类,不会将它自己的数据传递给外部。

public class A {
    int data;

    public void getFormattedBy(Formatter f) {
       f.format(data);
    }
}

访问者的界面,允许多个格式化程序

public interface Formatter {
    void format (int data);
}

格式化程序,允许进入被访问类。

public class B implements Formatter {
    public void format(int data) {
        // do the formatting and printing
    }
}

这样你只需拨打

A a = new A();
B b = new B(); // the formatter
a.getFormattedBy(b);

最后,访问者(格式化程序)可以访问许多允许访问者访问的类(可能通过自己实现一个接口),并且许多访问者都可以访问访问过的类。

答案 2 :(得分:2)

我认为传递它们是完全正确的,只要它们具有原始类型或不可变。

如果被调用者可以在不应该修改时修改它们,那么你就会遇到设计问题。

答案 3 :(得分:1)

只要B级没有修改它们就可以了。如果该类确实如此,则将不可变实例传递给它。

答案 4 :(得分:1)

您可以将B类设为实用程序类,并且只在其上使用静态方法。

然后在你的A级里面,你可能会像:

public String formatMyVariables(){

返回B.format(a,b,c,d);

}

我假设你提到的输出是一个字符串,但它可以是真的。

答案 5 :(得分:1)

您应该考虑,如果您的Employee字段是原始值(intboolean)或不可变字段(例如String),那么您可以让其他字段上课时不必担心。

使用private保护字段是不公开对象内部工作的方式,只能通过其public API进行访问。但是,当您的类真正表示业务对象(即一组标识实体的值)时,让其他人读取内部字段是完全安全的。

答案 6 :(得分:-1)

传输数据的最佳方式是DTO对象。

这些对象只包含实例变量(带有setter和getter)作为要传输的数据!

此课程中不应有任何行为

例如,如果要传递Employee数据,请执行以下操作

class EmployeeBean
{
private String name;
private String age;

public void setName(String n)
{
name=n;
}

public String getName()
{
return name;
}


public void setAge(int n)
{
age=n;
}

public int getAge()
{
return age;
}

}

您现在可以创建EmployeeBean类,在其实例变量中填充数据,然后将此对象作为参数传递给可以格式化的其他类中的方法

相关问题