MVC in Java only needs to point in the right direction

I'm not a rookie in programming I have written in one language or another for 20 years Just not in Java My main language is objective C on IOS and OS X

I'm developing a Java application and I'm trying to implement the MVC mode enforced by IOS That is, my UI, the data model is a separate class and the third class, and the controller is' glue '

What I want to do is super simple in target C. I've been searching Google all night trying to find the same pattern in Java

For example, I have two classes Class A and B Class a implements main() Main instantiates class B I need class B to call instances of class a methods I am Objective-C. class B has a pointer to class A as an invar Therefore, when class a instantiates class B, it sets the pointer to itself, so class B now has a reference to class A and can call its public method

But it seems that I can't do this from class A. it will give up in class B (in class a main()) NetBeans complain that they can't access non - static variables from static methods to similar things It seems to be the main () need to stay still

I think maybe I can add another class simulation for appdeleagte in cocoa So I have a main () in some other classes, which instantiates MVC

I have no obvious restrictions on statics The only time I encounter it is that the local needs to retain its value through continuous method calls

I have never declared a method static, nor have I encountered any problems accessing any variables that are usually in scope

It looks simple. I think I missed something obvious

PS I'm using NetBeans 7.01, and I'm just sneaking into Java I've tried to get help from Yahoo chat room and friends from FB, but he had to run

I thank anyone for any help

Thank you for reading this walk and having a wonderful night

Solution

Here are a few questions that I will try to break down and answer at least some of them:

First, in order to call the instance method of a, you need to provide an instance to B when instantiating This is the case with the following example:

public class A {
    public static void main(String[] args) {
        new A().run();
    }
    public void run() {
        B b = new B(this);
        b.run();
    }

    public void foo() {
        System.out.println("2.");
    }
}

public class B {
    private final A a;
    public B(A a) { this.a = a; }

    public void  run() {
        System.out.println("1.");
        a.foo();
    }
}
// Output:
// 1.
// 2.

The important difference here is that Java does not magically access the call stack or "parent" of the current execution target This means that if you want to call them, you really need to forward everything to the new instance

Second, as NetBeans points out, you cannot access non - static members from a static context It is best to use some codes to display:

public class A {
    public static void foo() {}
    public void bar() {}

    public static int i = 0;
    public int j = 1;
}

public class B {
    public static void main(String[] args) {
        A.foo();       // works because foo() is static
        A.bar();       // compile error
        new A().bar(); // works because Now you're calling
                       // non-static instance's bar() method.

        // Pretty much the same applies to fields;
        A.i = 2;       // works
        A.j = 2;       // compile error,once again
        new A().j = 2; // works
    }
}

Static in Java is almost equivalent to a global class (which makes it a very dangerous keyword in OOP. To be honest, it should be avoided at all costs) Almost everything else needs to be accessed in OOP mode, or in other words, through object instances

Although it may sound reasonable to have a global state for some things (such as the configuration of an application), it is not in the long run - you can create a seemingly separate object containing the configuration state, but you can dynamically modify the needs; In this way, you can avoid restarting the application when the configuration changes

I hope this will help and answer your questions. If necessary, I can ask more questions without hesitation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>