访问器方法的使用

发布于 2024-11-02 19:00:03 字数 1460 浏览 1 评论 0原文

这是我的类,包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小草泠泠 2024-11-09 19:00:03

有两种方法。

一,只需打印详细信息对象的每个属性:

System.out.println("FirstName :"+d.getFirstName()); 等。

或者,更好的选择是覆盖 toString() 方法在你的类中

public void toString() {
        return this.getFirstName()+ " " + this.getLastName()+" "+.... ;
}

,然后打印你的类 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 class

public void toString() {
        return this.getFirstName()+ " " + this.getLastName()+" "+.... ;
}

and then just print your class System.out.println(d);

内心激荡 2024-11-09 19:00:03

使用 toString() 方法

public String toString(){
   return this.firstName + " " + this.lastName + ", " + this.city + " " + this.country;
}

您需要在 Details 类中和

System.out.println(d.toString());

main 中

you need a toString() method in Details class:

public String toString(){
   return this.firstName + " " + this.lastName + ", " + this.city + " " + this.country;
}

and

System.out.println(d.toString());

in main

十雾 2024-11-09 19:00:03

如下所示重写详细信息中的 toString() 方法,然后调用以打印您想要的内容:

public String toString(){
   return this.firstName+" "+this.lastName+" "+this.city+" "+this.country;
}

在 main 中只需将其调用为 System.out.println(d);

Override toString() method in Details as follow and then just call to print what you want:

public String toString(){
   return this.firstName+" "+this.lastName+" "+this.city+" "+this.country;
}

in main just call it as System.out.println(d);

坚持沉默 2024-11-09 19:00:03

像这样的东西吗?

System.out.printf("%s %s (%s, %s)\n", d.getFirstName(), d.getLastName(), d.getCity(), d.getCountry());

我会将您的字段(名字、姓氏、城市和国家/地区)设为私有,否则使用 getter 和 setter 没有多大意义。

Something like this?

System.out.printf("%s %s (%s, %s)\n", d.getFirstName(), d.getLastName(), d.getCity(), d.getCountry());

I would make your fields (FirstName, LastName, City, and Country) private, otherwise there's not much point in using getters and setters.

别再吹冷风 2024-11-09 19:00:03

尝试添加方法(或类似的具有更多属性的方法):

public String asFirstnameLastname()
{
    return firstName + " " + lastName;
}

public String asLastNameFirstname()
{
   return lastName + " " + firstName;
}

toString() 也是一个不错的选择。

Try to add methods (or something similar with more properties):

public String asFirstnameLastname()
{
    return firstName + " " + lastName;
}

public String asLastNameFirstname()
{
   return lastName + " " + firstName;
}

toString() is also a good choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文