【问题标题】:What's the importance in using the set method when the attributes have already been defined in the Used Defined Constructor? [duplicate]当属性已经在 Used Defined Constructor 中定义时,使用 set 方法的重要性是什么? [复制]
【发布时间】:2016-09-05 18:30:31
【问题描述】:

在下面的代码中,我已经声明了 room = r;主题 = s;和时间 = t;在用户定义的构造函数中,为什么必须在 set 方法中再次这样做,我的讲师特别要求我们为房间主题和时间添加 set 方法,但它是冗余代码,因为当我将其注释掉时它仍然有效。当没有使用定义的构造函数时,您只需要包含 set 方法吗?让他们在那里设置方法有什么好处?

class LectureTest{
public static void main (String [] args){

Lecture l1 = new Lecture(140, "Comp", 5);
l1.display();

Lecture l2 = new Lecture(280, "Sports", 3);
l2.display();

Lecture l3 = new Lecture(101, "Business", 5);
l3.display();

Lecture l4 = new Lecture(360, "Shooting", 4);
l4.display();

Lecture l5 = new Lecture();
l5.display();

 }
}//end of LectureTest

class Lecture{
private int room;
private String subject;
private int time;

Lecture(int r, String s, int t){
    room = r;
    subject = s;
    time = t;
    }

Lecture(){}

public void setroomNumber(int r){
  room = r;
}
public void setSubject(String s){
  subject = s;
}
public void setTime(int t){
   time = t;
}
public int getroomNumber(){
  return room;
}
public String getSubject(){
  return subject;
}
public int getTime(){
  return time;
}

  public void display(){
  System.out.printf("\n" + "Room Number: " + getroomNumber()  + "\n" +     "Subject: " + getSubject()  + "\n" + "Time " + getTime() + "\n");
  }
}

【问题讨论】:

  • 除非你的类型应该是不可变的,否则你需要一种方法来改变它。

标签: java oop object


【解决方案1】:

构造函数“初始化”你的值。 假设你有...

public class Person {
   public String name;
   public int age;

   public Person (String name, int age) {
      this.name = name;
      this.age = age;
   }

   public void setName(String name) {
      this.name = name;
   }

   public void setAge(int age) {
      this.age = age;
   } 

   public String toString() {
      String str;
      str = "My name is "+name+" and I am "+age+" years old!";
      return str;
   }
}//End of Person


public class Main {
   public static void main(String [] args) {
      Person person = new Person("Bob", 15);
      System.out.println(person.toString());
      System.out.println("Switching my name...");
      person.setName("Joe");
      System.out.println(person.toString());
   }
}//End of main

你看出区别了吗?如果要创建对象的新实例,则应使用构造函数。这样,您可以一次设置对象的所有字段,而无需调用 490832490 个设置器(在这种情况下,一个用于名称,一个用于年龄......)。然后,当您想在创建对象之前更改字段的值时,可以使用 setter 方法。

我在这个论坛上做了所有这些,所以我可能有语法错误,所以小心...如果你想测试它,请不要使用 IDE

【讨论】:

  • 我的坏习惯......应该在字段中使用“私人”而不是“公共”
【解决方案2】:

set 方法使您的对象可变。如果您没有set 方法并且您的变量是private,那么Object 将是不可变的。构造后您将无法更改值...如果需要更改值,您必须创建一个 new Object

【讨论】:

    【解决方案3】:

    “Setters”允许您在实例化后修改对象的私有属性。例如:

    Lecture l1 = new Lecture(140, "Comp", 5);
    //Since "room" is private you can't write l1.room = 4 
    //and have to use the setter method instead:
    l1.setroomNumber(4);
    l1.display();
    

    如果您想在属性更改时执行某些操作,它们也非常有用。

    假设您正在使用 Observers,那么您可以在您的 setter 方法中调用 notifyObservers()setChanged(),而不必担心这些方法不会被调用如果你的属性发生变化。

    【讨论】:

      猜你喜欢
      • 2021-05-27
      • 2011-04-14
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 2021-02-08
      • 2017-06-26
      • 2013-01-18
      • 2020-03-17
      相关资源
      最近更新 更多