【发布时间】:2015-10-17 01:04:12
【问题描述】:
我真的被这段代码困住了,因为我似乎无法理解如何将数据成员访问到 java 中的另一个类中。我需要有人正确地向我解释这一点。
我的客户类包含以下数据成员:
name
startLocation
endLocation
当我尝试在单独的 .java 文件中访问这些数据成员时,我在这一行遇到错误:
System.out.println("Driver #" + ID + " has dropped off " + getname + " at " + getendLocation);
我不能只拥有该代码而不将数据成员带入另一个 .java 文件。有人可以解释我如何获取此代码并使其在我的单独文件中可见吗?
public class Customer
{
private String name;
private String startLocation;
private String endLocation;
public Customer (String name, String startLocation, String endLocation)
{
this.name = name;
this.startLocation = startLocation;
this.endLocation = endLocation;
}
public String getname()
{
return name;
}
public String getstartLocation()
{
return startLocation;
}
public String getendLocation()
{
return endLocation;
}
}
现在这是我卡住的地方。当我去我学校的辅导中心时,那个学生插手帮我告诉我我需要做一个主构造函数。所以我做到了,就是这样。他告诉我我还需要在其中添加一个名称 = 新名称,但是当我添加时,他突然告诉我忘记他告诉我的一切并回到我的做事方式(我没有方法已经解决了,只是在胡闹)。有人能解释一下他想向我展示的内容吗?
public Customer()
{
this.name = name;
this.StartLocation = startLocation;
this.endLocation = endLocation;
}
【问题讨论】:
-
类名应该是“客户”,而不是“客户”。注意——这些事情很重要。 Getter 和 setter 方法也被正确命名。编程中的小事很重要。
-
你问的是如何实例化一个类,如何调用一个实例的方法?
-
getName和getendlocation是方法而不是变量,在创建Customer的实例后调用它们例如:instance.getName() -
Because it's all here. 我只需要搜索 java 对象。
标签: java data-members