【问题标题】:creating an instance of a class type variable创建类类型变量的实例
【发布时间】:2012-08-30 18:42:29
【问题描述】:

我是 OOP 的新手,我想知道如何设置不同于 int、string、double 等的东西。

我有两个类,Foo 和 Bar,以及一些实例变量 如何设置 Bar 类型的实例变量?

public class Foo
{
    //instance variables
    private String name;        
    Bar test1, test2;

    //default constructor
    public Foo()
    {
        name = "";
        //how would I set test1 and test 2?
    }
}

public class Bar
{
    private String nameTest; 

    //constructors
    public Bar()
    {
        nameTest = "";
    }
}

【问题讨论】:

  • Bar test1 = new Bar();酒吧 test2 = 新酒吧();这就是你如何创建 Bar 类的实例。

标签: java oop constructor instance-variables


【解决方案1】:

与其他方法相同,创建一个 Bar 的实例并将其设置在实例属性上。

您可以在构造函数中创建这些实例,将它们传递给构造函数,或者使用 setter 进行设置:

public class Foo {

    private Bar test1;

    public Foo() {
        test1 = new Bar();
    }

    public Foo(Bar bar1) {
        test1 = bar1;
    }

    public void setTest1(Bar bar) {
        test1 = bar;
    }

    public static void main(String[] args) {
        Foo f1 = new Foo();
        Foo f2 = new Foo(new Bar());
        f2.setTest1(new Bar());
    }

}

【讨论】:

  • 刚刚在 Eclipse IDE 中发现了一个为您生成 getter 和 setter 的功能。但很高兴知道其他情况,谢谢戴夫和其他人
  • @user1615805 正确,大多数 IDE 都会有各种代码模板,尽管这与问题无关。 IIRC Eclipse 也有一个 ctor 生成,它将采用选定的属性。
【解决方案2】:

您需要使用new 运算符创建Bar 的新实例,并将它们分配给您的成员变量:

public Foo() {
  name = "";
  test1 = new Bar();
  test2 = new Bar();
}

参考资料:

【讨论】:

    【解决方案3】:

    如果你想在你的默认构造函数中设置一个 Bar,你必须实例化它。

    这是使用new operator 完成的。

    Bar someBar = new Bar();

    您还可以创建带参数的构造函数。

    下面是创建一个将字符串作为参数的 Bar 构造函数的方法:

    class Bar {
    
        private String name;
    
        public Bar(String n) {
            name = n;
        }
    
    }
    

    下面是如何在 Foo's 默认构造函数中使用新的 Bar 构造函数:

    class Foo {
    
        private String name;
        private Bar theBar;
    
        public Foo() {
            name = "Sam";
            theBar = new Bar("Cheers");
        }
    
    }
    

    更聪明的是,你可以创建一个新的 Foo 构造函数,它接受两个参数:

    class Foo {
    
        private String name;
        private Bar theBar;
    
        public Foo(String fooName, String barName) {
            name = fooName;
            theBar = new Bar(barName);
        }  
    
    }
    

    【讨论】:

      【解决方案4】:

      试试这个例子

      public class Person {
      
          private String name;
          private int age;
      
          public Person(String name, int age) {
              this.name = name;
              this.age = age;
          }        
          //getters and setters        
      }
      
      
      public class Student {
      
          private String school;
          private Person person;
      
          public Student(Person person, String school) {
              this.person = person;
              this.school = school;
          }        
          //code here
      }
      
      class Main {
      
         public static void main(String args[]) {
            Person p = new Person("name", 10);
            Student s = new Student(p, "uwu");
         }
      
      }
      

      String、Integer、Double 等还有 person 等类

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-10
        • 2020-10-25
        相关资源
        最近更新 更多