构造函数和getter以及setter之间的区别 [英] Difference between constructor and getter and setter

查看:310
本文介绍了构造函数和getter以及setter之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为大学项目做作业。有一点,当你真正使用构造函数方法来实现相同的结果时,我对getter和setter的实际使用感到困惑。我搜索并找到了许多答案,但不能令人满意的解释。
我有laptop.java如下

I have been doing assignments for college projects. At one point, I am getting confused at what is actually the use of getter and setter when you can actually use constructor methods to achieve the same results. I have searched and found many answers but not satisfactory explanation. I have laptop.java as follows

public class laptop {
    private String model;

    public laptop(String brand){
     model=brand;
        }

    public String toString(){
        return "Laptop Brand is: "+ model;
    }
}

和调用构造函数的laoptopRecords.java为

and the laoptopRecords.java that is calling constructor as

public class laptopRecords {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        laptop laptop1=new laptop("Dell");
        System.out.println(laptop1);
    }

}

这里我没有使用getter和setter方法,我得到每个笔记本电脑对象的预期结果。

Here I am not using the getter and setter methods and I get desired result for every laptop object.

如果我使用laptopRecord.java中的getter和setter方法以另一种方式,如下所示,我得到相同的结果。但是我没有得到getter和setter方法的用法,实际上我们也可以用构造函数来实现结果。

If I do in another way using getter and setter methods in laptopRecord.java as follows, I get the same result. But I am not getting what is use of getter and setter methods if actually we can achive the results with constructor as well.

laptop.java with getter and setter

laptop.java with getter and setter

public class laptop {
    private String model;

    public laptop(String brand){
     model=brand;
        }
    public void setlaptop(String brand){
        model=brand;        
    }
    public String getlaptop(){
        return model;       
    }
    public String toString(){
        return "Laptop Brand is: "+ model;
    }
}


推荐答案

我告诉你一个简单的方法:

getters()和setters():

getters() and setters():


  • 实际上getter()和setter在POJO / Bean类中可用。

  • 使用的主要原因for getters()java类中的setters()是获取Java封装机制。

  • 在POJO / Bean类中,我们将所有属性声明为 private。这意味着这些类属性不能在其他类和包中使用,所以在这里,我们可以实现Encapsulation。

  • actually getters() and setters available in POJO/Bean classes.
  • The main reason used for getters() setters() in java classes is To get the Java Encapsulation Mechanism.
  • In POJO/Bean classes we declare all attributes as a private. that means these class attributes can't use in other classes and packages, so in this, we can achieve Encapsulation.

构造函数():

Constructors():


  • 我想你知道的定义构造函数,构造函数用于初始化给出我们自己的值而不是存储默认值的属性

  • 我们可以说另一种方式,即构造函数用于创造一个用于更改对象内部值的对象和setter,getters()用户获取值,这只是主要区别。

  • I think you know the definition of constructor, The constructor is used for initializing the attributes giving our own values rather than storing the default values
  • We can say another way i.e Constructor used for creating an object and setters used for changing the values inside object, getters() user for getting the values, this is only the main difference.

这篇关于构造函数和getter以及setter之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆