English 中文(简体)
Java的固定方法
原标题:set methods in Java
  • 时间:2011-04-06 13:27:07
  •  标签:
  • java
  • set

Could anubody explain how to use set methods? Problem:

class Sonum {
   private int prior;
   public Sonum(int prior) {
      this.prior = prior;
   }
   public int getPrior() {
      return prior;
   }
   public void setPrior(int prior) {
      this.prior = prior;
   }

class Tel {
   // Please explain how can I set the value for prior? (for example 2)
}
问题回答

Well, first you need an instance of Sonum on which you want to set the prior value. For example:

class Test {
  public void foo() {
    Sonum sonum = new Sonum(5);
    // Use it with a prior of 5
    // ...
    sonum.setPrior(10);
    // Now use it with a prior of 10
  }
}
Sonum mySonum = new Sonum(1); //prior is currently 1
mySonum.setPrior(2); //now prior is 2

“更好的方法”是mag。 它们只是常规方法。 你们需要这个类别,然后,你可以把方法称作这一类。 同任何其他 Java物体一样。

既定方法涉及私人价值,我们要防止他直接使用客户,因为有办法。

The biggest advantage of get set methods is the control ability ! We can for example control a minimum age when we want to set an age, and many other simple examples.

例:

setAge (int age)
{
    if ( age < 0 )
    {
        System.out.println ( "Wrong age !!" );
    }
}

现在我认为你很容易理解这一HW:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签