Java:Setter Getter和constructor [英] Java :Setter Getter and constructor

查看:556
本文介绍了Java:Setter Getter和constructor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用getter / setters和constructors感到困惑。(参见下面的代码示例)

I'm a bit confused about the use of getter/setters and constructors (see the below code for an example)

    public class ExampleClass {

        private int value = 0; 

        public ExampleClass () {
            value = 0; 
        }

        public exampleClass (int i) {
            this.value=i;
        }

        public int getValue() {
            return value; 
        }

        public void setValue(int val) {
            this.value=val; 
        }

        public static void main(String[] args) {     
            ExampleClass example = new ExampleClass (20);
            example.setValue(20); 
            //Both lines above do same thing - why use constructor? 
            System.out.println(example.getvalue());
        }
   }

需要安全 getter / setter ,也可以使用稍后更改或编辑值。

All I've learned is that we need getters/setters for security and that they can also be used to change or edit values later on.

我的问题是,如果构造函数是初始化点,并且默认构造函数总是存在,为什么使用带参数的构造函数来初始化值而不是getter / setters?。不会使用getter和setter提供安全性以及能够轻松地在任何阶段更改值。请为我澄清这一点。

My question is that if the constructor is the point of initialization and a default constructor is always present, why use a constructor with parameters to initialize values instead of getters/setters?. Wouldn't using the getter and setter provide security as well being able to easily change values at any stage. Please clarify this point for me.

推荐答案


默认构造函数始终存在

default constructor is always there

其实它不总是在那里。默认构造函数是由编译器提供的(当然它是一个无参数的构造函数)只有在类中没有定义其他构造函数

Well actually its not always there. A default constructor is the one which is provided by the compiler (of course it is a no-arg constructor ) Only if there is no other constructor defined in the class


为什么我们使用带参数的构造函数来初始化值而不是set get

why we use constructor with parameters to initialize values instead of set get

可能存在这样的条件,即只有当在初始化本身时提供所有的值并且没有默认值时,才可以始终创建对象。因此,必须提供所有值,否则代码将无法编译。

Because there could be a condition that an object can always be created only when all the values are provided at the time of initialization itself and there is no default value. So all values must be provided otherwise code will not compile.

考虑这个 Book class

Consider this Book class

public class Book {

    private String title;
    private String author;

    public Book(String title, String author){
        this.title = title;
        this.author = author;
    }
     //getters and setters here 
}

只有在 title author 的情况下才能创建一本书。

Consider a condition where a book can be created only if it has title and author.


  • 你不能做 new Book(),因为没有参数构造函数,编译器不会提供一个,因为一个构造函数已定义。

  • 此外,您不能执行 new Book(),因为我们的条件不符合,因为每本书都需要标题和作者。 >
  • You cannot do new Book() because no-arg constructor is absent and compiler will not provide one because one constructor is already defined.
  • Also you cannot do new Book() because our condition does not meet as every book requires a title and author.

这是参数化构造函数有用的条件。

This is the condition where parameterized constructor is useful.

这篇关于Java:Setter Getter和constructor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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