对java构造函数感到困惑

对java构造函数感到困惑,java,constructor,Java,Constructor,所以我正在学习Java。我接触构造函数,处理类。我在理解他们的工作/目的时遇到了一些问题?我相信它们的用法类似于函数调用,调用时将参数传递给函数 我的想法正确吗 例如: class test{ void durp(String input){ System.out.print(input); } } Test test = new Test("hey"); test.durp() // prints "hey" (without quote) 如果我在我的主类

所以我正在学习
Java
。我接触
构造函数
,处理
。我在理解他们的工作/目的时遇到了一些问题?我相信它们的用法类似于函数调用,调用时将参数传递给函数

我的想法正确吗

例如:

class test{
    void durp(String input){
        System.out.print(input);
    }
}
Test test = new Test("hey");
test.durp() // prints "hey" (without quote)
如果我在我的主类中创建这样的对象:

测试对象=新测试(“嘿”)

它将作为字符串传递给
durp()


这是正确的吗

您需要学习核心java basic,它告诉您有关带参数的构造函数的信息

class Test{

     // this is a constructor (name is same as class and no return type)
    public Test(String input){
      // some code here
   }

  // this is a method 
  public void durp(String input){
   // some code
  } 

 public static void main(String[] args){
       Test test = new Test("hey"); // calls constructor
  }

}
class test{

public test(String s)
{
   durp(s);
}

 void durp(String input){

  System.out.print(input);

} 
  public static void main(String args[])
  {
   test obj=new test("hey");
 }
 }

您需要学习核心java basic,它告诉您关于带参数的构造函数

class test{

public test(String s)
{
   durp(s);
}

 void durp(String input){

  System.out.print(input);

} 
  public static void main(String args[])
  {
   test obj=new test("hey");
 }
 }
如果我在我的主类中创建这样一个对象:testobject=newtest(“嘿”);它会将“hey”作为字符串传递给durp(),对吗

不,因为您的方法
durp()
不是构造函数。它只是属于类的一个方法,可以从创建的活动对象调用它

public class Test {
    /** this is a constructor */
    public Test() {
    } 

    /** this is also a constructor with a parameter */
    public Test(String arg1) { 
        System.out.println(arg1); 
    } 

    /** this is a method of Test */
    public void derp() {
    }
}
你可以阅读

如果我在我的主类中创建这样一个对象:testobject=newtest(“嘿”);它会将“hey”作为字符串传递给durp(),对吗

不,因为您的方法
durp()
不是构造函数。它只是属于类的一个方法,可以从创建的活动对象调用它

public class Test {
    /** this is a constructor */
    public Test() {
    } 

    /** this is also a constructor with a parameter */
    public Test(String arg1) { 
        System.out.println(arg1); 
    } 

    /** this is a method of Test */
    public void derp() {
    }
}

您可以阅读java中用于构造对象的构造函数,例如,您想要设置durp的值(不是durp(),因为这是一种方法),您可以使用构造函数来执行此类任务

我想你想要这样的东西:

class Test {
    private String durp;

    public Test(String durp) {
        //set value of durp
        this.durp = durp;
    }

    //function for getting durp string.
    //Use getDurp() rather than durp() in java.
    public String getDurp() {
        return durp;
    }
}

java中的构造函数用于构造对象,例如,如果要设置durp的值(而不是durp(),因为这是一个方法),则可以将构造函数用于此类任务

我想你想要这样的东西:

class Test {
    private String durp;

    public Test(String durp) {
        //set value of durp
        this.durp = durp;
    }

    //function for getting durp string.
    //Use getDurp() rather than durp() in java.
    public String getDurp() {
        return durp;
    }
}

Java构造函数看起来只是函数*,但实际上它们非常不同:

  • 当对象完全初始化时调用方法;当对象尚不存在时调用构造函数
  • 方法不能改变类的
    final
    变量;对于建设者来说,这是他们目的的一部分
  • 方法可能会返回不同的东西;构造函数不返回任何内容
  • 方法调用可以在各种表达式中使用;构造函数只能作为
    new
    表达式的一部分调用
构造函数必须遵循特殊的命名约定:它们的名称必须与类的名称匹配。在您的情况下,这将是
test
。通常,构造函数的任务是设置类的成员变量。考虑到您的
test
类没有这样的变量,您不需要构造函数**:这样一个简单的调用就足够了:

new test().drup("hello");

*在Java中,“函数调用”的恰当术语是“方法调用”,尽管具有其他编程语言背景的程序员经常交替使用这两个术语


**当类未定义自定义构造函数时,会为您提供一个不带参数的默认构造函数。

Java构造函数看起来像函数*,但实际上它们非常不同:

  • 当对象完全初始化时调用方法;当对象尚不存在时调用构造函数
  • 方法不能改变类的
    final
    变量;对于建设者来说,这是他们目的的一部分
  • 方法可能会返回不同的东西;构造函数不返回任何内容
  • 方法调用可以在各种表达式中使用;构造函数只能作为
    new
    表达式的一部分调用
构造函数必须遵循特殊的命名约定:它们的名称必须与类的名称匹配。在您的情况下,这将是
test
。通常,构造函数的任务是设置类的成员变量。考虑到您的
test
类没有这样的变量,您不需要构造函数**:这样一个简单的调用就足够了:

new test().drup("hello");

*在Java中,“函数调用”的恰当术语是“方法调用”,尽管具有其他编程语言背景的程序员经常交替使用这两个术语


**当类未定义自定义构造函数时,将为您提供不带参数的默认构造函数。

在Java中,将构造对象。每次创建一个新对象时,至少有一个 构造函数被调用。每个类都有一个构造函数,但如果不创建 一个是显式的,编译器将为您构建一个。关于这个问题有很多规则 构造函数,让我们关注基本的声明规则。下面是一个简单的例子:

 class Tets{
   protected Test() { } // this is Test's constructor
   protected void Test() { } // this is a badly named,
   // but legal, method
 }
首先要注意的是,构造函数看起来非常像方法。A. 关键的区别是,构造函数不能永远,永远,永远,有一个返回类型…永远! 但是,构造函数声明可以具有所有正常访问修饰符,并且 它们可以接受参数(包括var args),就像方法一样。另一个大的 规则是,要理解构造函数,它们必须和 在其中声明它们的类。构造函数不能被标记为静态(它们是 毕竟与对象实例化关联),它们不能被标记为final 或抽象(因为它们不能被重写)。这里有一些合法的和非法的 构造函数声明:

 class Foo2 {
    // legal constructors
    Foo2() { }
    private Foo2(byte b) { }
    Foo2(int x) { }
    Foo2(int x, int... y) { }
    // illegal constructors
    void Foo2() { } // it's a method, not a constructor
    Foo() { } // not a method or a constructor
    Foo2(short s); // looks like an abstract method
    static Foo2(float f) { } // can't be static
    final Foo2(long x) { } // can't be final
    abstract Foo2(char c) { } // can't be abstract
    Foo2(int... x, int t) { } // bad var-arg syntax
}

访问

在Java中,对象被构造。每次创建一个新对象时,至少有一个 构造函数被调用。每个类都有一个构造函数,但如果不创建 一个是显式的,编译器将为您构建一个。关于这个问题有很多规则 构造函数,让我们关注基本的声明规则。下面是一个简单的例子:

 class Tets{
   protected Test() { } // this is Test's constructor
   protected void Test() { } // this is a badly named,
   // but legal, method
 }
首先要注意的是,构造函数看起来非常像方法。A. 关键的区别在于构造函数不能有一个返回