【问题标题】:OOP in java - creating objectsJava中的OOP - 创建对象
【发布时间】:2012-06-28 08:07:52
【问题描述】:

好的,伙计们,到目前为止(因为我是初学者)我是基于过程式编程的 Java 编程,这很好,但现在是时候像老板一样使用 Java了。

我现在正在学习 OOP 概念,同时编写一些代码作为练习。 我不明白的是,如果我以这种方式创建一些对象:

    Contact first = new Contact(25, "Yosi", "Male");
    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

    Contact second = new Contact(22, "lisa", "Femal");
    System.out.println("Age of contact " + second.toString() + " is - "
            + second.getAge() + " " + second.getName());

    Contact third = new Contact(34, "Adam", "Male");
    System.out.println("Age of contact " + third.toString() + " is - "
            + third.getAge() + " " + third.getName());

结果将是:

Age of contact Contact@173f7175 is - 25 Yosi
Age of contact Contact@4631c43f is - 22 lisa
Age of contact Contact@6d4b2819 is - 34 Adam

但是,如果我再次尝试打印第一个联系人,它将获得最后创建的对象的值。我的意思是,对于这段代码:

    Contact first = new Contact(25, "Yosi", "Male");
    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

    Contact second = new Contact(22, "lisa", "Femal");
    System.out.println("Age of contact " + second.toString() + " is - "
            + second.getAge() + " " + second.getName());

    Contact third = new Contact(34, "Adam", "Male");
    System.out.println("Age of contact " + third.toString() + " is - "
            + third.getAge() + " " + third.getName());

    System.out.println("Age of contact " + first.toString() + " is - "
            + first.getAge() + " " + first.getName());

结果将是:

Age of contact Contact@173f7175 is - 25 Yosi
Age of contact Contact@4631c43f is - 22 lisa
Age of contact Contact@6d4b2819 is - 34 Adam
Age of contact Contact@173f7175 is - 34 Adam

我已经添加了对象字符串表示,以便您可以看到不同的对象。 我以为我正在创建一个新对象,每个对象都有自己的实例值? 可以给我解释一下吗?

这是联系人类:

public class Contact {

    private static int age = 0;
    private static String name = "Unknown";
    private static String gender = "Male";

    public Contact(int a, String n, String g) {
        age = a;
        name = n;
        gender = g;

    }

    public Contact() {
    }

    public static int getAge() {

        return age;
    }

    public static String getName() {

        return name;
    }

    public static String getGender() {

        return gender;
    }

    public static void setAge(int a) {

        age = a;

    }

    public static void setName(String n) {

        name = n;
    }

    public static void setGender(String g) {

        gender = g;
    }

}

请注意,如果我删除静态限定符,我会收到错误消息“无法对非静态字段进行静态引用”

【问题讨论】:

  • 请发布您的联系人类的代码。很可能,您在那里有静态属性。

标签: java oop object


【解决方案1】:

从您的实例变量和/或方法(age、getAge、name、getName)中删除 static 限定符。

【讨论】:

  • 谢谢,这是真的,我正在使用静态限定符,但如果我删除它们,我会收到一条错误消息,提示我无法对非静态字段进行静态引用
  • @Yosi199 请发布您的Contact 课程的代码。别人帮助你会更容易
  • 那么您在错误的一端进行修复——将访问类型更改为非静态,而不是使下游的所有内容都静态。我敢打赌,您将 Quick Fix 作为您的编程顾问,这是一种错误的方法。 Quick Fix 旨在帮助那些已经知道自己想要什么的人更快地完成任务。
  • BTW 你确定你的方法 getAge, getName 没有静态限定符
  • 谢谢大家都是对的和很大的帮助。我从 get 和 set 方法以及实例变量中删除了所有静态引用,它现在可以工作了。
【解决方案2】:

如果您错误地使用了静态变量,就会发生这种情况:

class Stat {
  static String name;

  Stat(String n) {
    name = n;
  }
}

在上面的示例类中,所有实例都将共享相同的 name 值。

对实例成员使用非静态变量:

class Stat {
  String name;

  Stat(String n) {
    name = n;
  }
}

【讨论】:

    【解决方案3】:

    好吧,Yosi.Static 限定符将您的字段(和方法)绑定到您的类而不是您的对象,并且您的类(联系人)的每个对象共享相同的值。

    每当您通过 New 对其进行实例化时,此值都会更新。因此,当您第四次打印时,它的值就是您第三次更新它的值。就是这样打印的。

    当我看到你的代码时,如果你从任何地方删除静态,你的代码就会按照你的意愿运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-01
      • 2012-06-08
      • 2017-11-29
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多