访问器方法的使用
这是我的类,包含 setter 和 getter
package Pack;
public class Details {
String FirstName,LastName,City,Country;
public Details(String firstName, String lastName, String city,
String country) {
super();
FirstName = firstName;
LastName = lastName;
City = city;
Country = country;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
}
===================================== ==========================================
这是我的 main()
package Pack;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Details d = new Details("Hari","L","Bangalore","India");
}
}
==== =================================================== ====================
我知道我的 main() 不完整。我应该写什么来显示“d”的内容?
This is my class containing setters and getters
package Pack;
public class Details {
String FirstName,LastName,City,Country;
public Details(String firstName, String lastName, String city,
String country) {
super();
FirstName = firstName;
LastName = lastName;
City = city;
Country = country;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
}
===========================================================================
This is my main()
package Pack;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Details d = new Details("Hari","L","Bangalore","India");
}
}
==========================================================================
I know my main() is incomplete. What should i write to display the contents of "d"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有两种方法。
一,只需打印详细信息对象的每个属性:
System.out.println("FirstName :"+d.getFirstName());
等。或者,更好的选择是覆盖
toString()
方法在你的类中,然后打印你的类
System.out.println(d);
There are two ways.
One, just print each property of your details object :
System.out.println("FirstName :"+d.getFirstName());
etc..Or, a better option would be to override
toString()
method in your classand then just print your class
System.out.println(d);
使用 toString() 方法
您需要在 Details 类中和
main 中
you need a toString() method in Details class:
and
in main
如下所示重写详细信息中的 toString() 方法,然后调用以打印您想要的内容:
在 main 中只需将其调用为
System.out.println(d);
Override toString() method in Details as follow and then just call to print what you want:
in main just call it as
System.out.println(d);
像这样的东西吗?
我会将您的字段(名字、姓氏、城市和国家/地区)设为私有,否则使用 getter 和 setter 没有多大意义。
Something like this?
I would make your fields (FirstName, LastName, City, and Country) private, otherwise there's not much point in using getters and setters.
尝试添加方法(或类似的具有更多属性的方法):
toString() 也是一个不错的选择。
Try to add methods (or something similar with more properties):
toString() is also a good choice.