【问题标题】:Writing methods and constructors编写方法和构造函数
【发布时间】:2012-09-12 02:12:10
【问题描述】:

好的,我需要有人向我解释从哪里开始这个项目。

首先,我需要通过向Person 添加一个默认(无参数)构造函数来重载构造函数,该构造函数定义一个名称为“N/A”且id 为-1 的对象。

然后我需要添加一个名为reset的setter方法,它可以用来将这个类的两个私有实例变量重置为作为参数传入的两个值。

然后我需要添加一个 getter 方法,名为 getName 和 getId 可以用来检索这两个私有变量

代码如下:

public class Person
{
private String name;
private int    id;
private static int personCount = 0;

// constructor
public Person(String pname)
{
name = pname;
personCount++;
id = 100 + personCount;
}



public String  toString()
{
 return "name: " + name + "  id: " + id
  + "  (Person count: " + personCount + ")";
}

// static/class method
public static int getCount()
{
  return personCount;
}

/////////////////////////////////////// /

public class StaticTest
{
  public static void main(String args[])
{
    Person tom = new Person("Tom Jones");
    System.out.println("Person.getCount(): " + Person.getCount());
    System.out.println(tom);
    System.out.println();

    Person sue = new Person("Susan Top");
    System.out.println("Person.getCount(): " + Person.getCount());
    System.out.println(sue);
    System.out.println("sue.getCount(): " + sue.getCount());
    System.out.println();

    Person fred = new Person("Fred Shoe");
    System.out.println("Person.getCount(): " + Person.getCount());
    System.out.println(fred);
    System.out.println();

    System.out.println("tom.getCount(): " + tom.getCount());
    System.out.println("sue.getCount(): " + sue.getCount());
    System.out.println("fred.getCount(): " + fred.getCount());
}
}

我不确定从哪里开始,我不想要答案。我正在找人解释清楚。

【问题讨论】:

  • Here 是个不错的起点 :-)

标签: class methods constructor


【解决方案1】:

首先,我需要通过向 Person 添加一个默认(无参数)构造函数来重载构造函数,该构造函数定义了一个名称为“N/A”且 id 为 -1 的对象。

阅读构造函数here

Person 类已经包含一个接受 1 个参数的 ctor。您需要做的是创建一个“默认 ctor”,它通常是一个不带任何参数的 ctor。

例子:

class x
{
   // ctor w/ parameter
   //
   x(int a)
   {
      // logic here
   }

   // default ctor (contains no parameter)
   //
   x()
   {
     // logic here
   }
}

然后我需要添加一个名为reset的setter方法,可以用来将这个类的两个私有实例变量重置为作为参数传入的两个值。

Setter 方法用于通过公共函数“设置”成员变量的值来“封装”成员变量。见here

例子:

class x
{
   private int _number;

   // Setter, used to set the value of '_number'
   //
   public void setNumber(int value)
   {
     _number = value; 
   }
}

然后我需要添加一个 getter 方法,命名为 getName 和 getId,可以用来检索这两个私有变量

Getter 则相反。它们不是“设置”私有成员变量的值,而是用于“获取”成员变量的值。

例子:

class x
{
   private int _number;

   // Getter, used to return the value of _number
   //
   public int getNumber()
   {
      return _number;
   }
}

希望对你有帮助

【讨论】:

    【解决方案2】:

    我强烈建议您咨询Java Tutorials,这在这里应该很有帮助。例如,constructors 上有一个部分详细说明了它们的工作原理,甚至给出了无参数形式的示例:

    虽然Bicycle 只有一个构造函数,但它可以有其他构造函数,包括无参数构造函数:

    public Bicycle() {
        gear = 1;
        cadence = 10;
        speed = 0;
    }
    

    Bicycle yourBike = new Bicycle(); 调用无参数构造函数来创建一个名为 yourBike 的新 Bicycle 对象。

    同样,也有专门针对defining methodspassing information to them 的部分。您的方法中甚至还有一个关于returning values 的部分。

    阅读以上内容,你应该能够完成你的作业:-)

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 2019-10-12
      • 1970-01-01
      • 2012-09-02
      • 2013-05-28
      • 2010-09-13
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多