可以将同一类的对象作为参数传递给Java中同一类的方法吗?

huangapple go评论69阅读模式
英文:

is it safe to pass object of same class, as parameter, to a method of same class in java

问题



我开始学习Java,并且想知道在同一个类中将一个相同类的对象作为参数传递给方法是否正常/安全?我尝试过,它可以工作,只是想知道这是否是一种标准做法。

下面是代码片段:

class App{

    private int databaseFlag = 0;
    private String s;

    public App() {
        // TODO Auto-generated constructor stub
        s = "value";
    }

    public static void main(String[] args) {
        App app = new App();

        app.init(app);

        app.methodOne();

        app.methodTwo();
    }

    public void init(App app){ 

        DatabaseClass dbc = new DatabaseClass();

        app.databaseFlag = dbc.callDatabaseMethodAndGetFlag(s);
    }

    public void methodOne(){
        // do something with databaseFlag
        
    }

    public void methodTwo(){
        // do something more then reset databaseFlag=0;
    }
}
英文:

I am starting to learn Java and was wondering if I it's normal/safe to pass an object of same class as a parameter to a method in the same class? I tried it and it works, just wanted to know if it's a standard practice.

code snippet below;



class App{

	private int databaseFlag=0;
	private String s;
	public App() {
		// TODO Auto-generated constructor stub
		s = "value";
	}
	
	
public static void main(String[] args) {
    App app = new App();
    
    app.init(app);
   
    app.methodOne();
   
    app.methodTwo();
    }

public void init(App app){ 
	
	DatabaseClass dbc = new DatabaseClass();
	
	app.databaseFlag = dbc.callDatabaseMethodAndGetFlag(s);
}

public void methodOne(){
    // do something with databaseFlag
	
}

public void methodTwo(){
	// do something more then reset databaseFlag=0;
}

}

答案1

得分: 0

虽然在对象的方法中,您可以通过其名称调用其他方法,例如:

class App {
    private void init() {
        //一些代码
        someOtherMethod();
    }

    private void someOtherMethod() {
        //一些其他代码
    }

    public static void main(String[] args) {
        App a = new App();
        a.init();
    }
}

注意如何从init中调用someOtherMethod?您不需要在此处使用类引用。

要回答您的具体问题,在Java中,每个对象都可以访问其引用,称为this,您不需要传入app对象。如果您绝对需要引用对象变量,可以这样做:

class App {

    private int databaseFlag = 0;
    private String s;

    public App() {
        // TODO Auto-generated constructor stub
        s = "value";
    }

    public static void main(String[] args) {
        App app = new App();
        app.init();
        app.methodOne();
        app.methodTwo();
    }

    public void init() {
        DatabaseClass dbc = new DatabaseClass();
        this.databaseFlag = dbc.callDatabaseMethodAndGetFlag(s);
    }

    public void methodOne() {
        // 使用databaseFlag做一些操作
    }

    public void methodTwo() {
        // 做更多操作,然后重置databaseFlag=0;
    }
}

请注意,这里只是您提供的代码的中文翻译部分。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

Even though, with in object's method, you can call other method by its name i.e.,

class App {
        private void init () {
            //some code
            someOtherMethod();
        }
    
        private void someOtherMethod() {
            //someother code
        }
    
        public static void main(String[] args) {
            App a = new App();
            a.init()
        }
    }

See how someOtherMethod is being called from init? You don't need your class reference here.

To answer your specific question, In Java, every object has access to its reference and that is called this, you don't need to pass in the app object. if you absolutely have to refer the object variables, you can do it by this

class App{

    private int databaseFlag=0;
    private String s;
    public App() {
        // TODO Auto-generated constructor stub
        s = "value";
    }
    
    public static void main(String[] args) {
        App app = new App();
        app.init();
        app.methodOne();
        app.methodTwo();
    }

    public void init(){ 

        DatabaseClass dbc = new DatabaseClass();

        this.databaseFlag = dbc.callDatabaseMethodAndGetFlag(s);
    }

    public void methodOne(){
        // do something with databaseFlag

    }

    public void methodTwo(){
        // do something more then reset databaseFlag=0;
    }
}

huangapple
  • 本文由 发表于 2020年4月7日 00:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定