构造函数中的对象参数 [英] Object Parameters in a Constructor

查看:179
本文介绍了构造函数中的对象参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,如果我的问题很难理解,我道歉。我有一个很难的时间,试图解释什么我需要帮助。我是新的Java和通过引用的概念等。

first of all I apologize if my question is difficult to understand. I'm having a hard time trying to explain exactly what I need help with. I am new to Java and the concept of passing by reference etc.

基本上,我需要知道为什么下面的代码是不正确的。我如何告诉Java使用一个方法作为构造函数的参数传入的对象?再次感谢您阅读!

Basically, I need to know why the below code is incorrect. How do I tell Java to use a method for an object passed in as a parameter of the constructor? Apologies again, and thanks for reading!

public ClassOne(ClassTwo twoObject){

}

public boolean OneMethod(){
    twoObject.MethodName(); // twoObject cannot be resolved.
}


推荐答案

在另一种方法it isnt工作,你可以创建一个全局对象来保存它,然后使用...

You are using a local object in another method it isnt working, You can create an global object to save it and then use it...

public class classOne{

    Classtwo object;
    public ClassOne(ClassTwo twoObject){
        object = twoObject;
    }

    public boolean OneMethod(){
        object.MethodName(); 
    }
}

希望它能帮助你:)

这篇关于构造函数中的对象参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆