当我们编写一个小型的java控制台程序时,可以使用如下3种风格:
第一:
直接利用public class类,直接在public class类内部写我们需要的方法和成员变量。因为main方法是static的,因此我们的方法和成员变量也全部写成static。
import java.io.*;
public class Test {
   
    static int mynum=20; 
    public static int myfunction()
    {
     return mynum+1;
    } 
    public static void main(String[] args) {        
        System.out.println (mynum);
        System.out.println (myfunction());
    }
}
第二:
直接在public class类内部new一个public class类的对象,这样所有的方法和成员变量都不必一定要是static的,这种方法太怪。
import java.io.*;
public class Test {
   
    int mynum=20; 
    public int myfunction()
    {
     return mynum+1;
    } 
    public static void main(String[] args) {
        Test myclass=new Test(); 
        System.out.println (myclass.mynum);
        System.out.println (myclass.myfunction());
    }
}
第三种:
也是最正常的一种,建立一个class,然后在public class类内部new一个刚才建立的class。
import java.io.*;
class Mytest{
int mynum=20; 
    public int myfunction()
    {
     return mynum+1;
    } 
}
public class Test {
   
    
    public static void main(String[] args) {  
     Mytest f=new Mytest();      
        System.out.println (f.mynum);
        System.out.println (f.myfunction());
    }
}请说出各个方法的利弊^_^

解决方案 »

  1.   

    其实对于lz所谓的小型程序而言,这几方法都差不多,因为占用的资源都不多。只不过1是利用static而不会生成新的对象,而2、3是会生成对象。其实本质上而言,2和3根本就是一回事,只不过3中的Test是专门为了调用其他对象而拥有main方法,而2则是把生成对象和调用方法的功能放在一个类里面了。如果你能真正理解面向对象的含义的话,这两种方式并无本质的差别。当然,对于初学者而言,2的方式比较熟悉糊涂,而3的方式更容易懂,所以可以先写3的方式,等到以后对面向对象熟悉了以后写成2的方式也就能完全理解了。
      

  2.   

    第二种方法是“在类的内部new一个类的对象”,看如下程序:
    import java.io.*;
    class Mytest{
    int mynum=20; 
    public int myfunction()
        {
         return mynum+1;
        } 
        Mytest t=new Mytest();      
    }public class Test {    
        public static void main(String[] args) {  
         Mytest f=new Mytest();      
            System.out.println(f.mynum);
            System.out.println(f.myfunction());
        }
    }
    编译通过,但显然不能正常运行,我不明白,为什么要允许在类的内部new一个类的对象?
    如下程序是正确的:
    import java.io.*;
    class Mytest{
    int mynum=20; 
    public int myfunction()
        {
         Mytest t=new Mytest();
         return t.mynum;
        }           
    }public class Test {    
        public static void main(String[] args) {  
         Mytest f=new Mytest();      
            System.out.println(f.mynum);
            System.out.println(f.myfunction());
        }
    }
      

  3.   

    To zuilang(3木):你的两段代码语法上讲都没有问题,只是你都定义了Mytest t=new Mytest();这是没有必要的。你可以这么来考虑:main方法是一个启动程序的入口点,先不要管它在哪个类里面。在main方法里你声明了一个Mytest对象,Mytest f=new Mytest();那Mytest对象是什么呢?这个对象包含了一个成员属性,即mynum,还包含了方法myfunction()。main方法中声明了Mytest类型的f,当然可以调用f的属性和方法。Test中的main方法是静态的,是属于Test类而不是实例的。其实,main属于哪个类都一样可以执行。你回复里的两段代码实际上可以写为:class Mytest {
    int mynum = 20;
    public int myfunction() {
    return mynum + 1;
    }
    }public class Test {
    public static void main(String[] args) {
    Mytest f = new Mytest();
    System.out.println(f.mynum);
    System.out.println(f.myfunction());
    }
    }
    甚至是:public class Test {
    int mynum = 20;
    public int myfunction() {
    return mynum + 1;
    }
    public static void main(String[] args) {
    Test f = new Test();
    System.out.println(f.mynum);
    System.out.println(f.myfunction());
    }
    }
      

  4.   

    我觉得没有一种方法很怪啊.都是很正常的啊.多多理解一下static这个东西吧.
      

  5.   

    To brooksychen(初晨之阳) :
    谢谢你的详细解答,其实我只是不理解为什么允许在类里面声明一个改类的对象而已