关于Java类&接口&类的修饰符的整理

来源:互联网 发布:视频配音变声软件 编辑:程序博客网 时间:2024/05/16 06:51

还是系统的整理一下这些的用法。随用随记。

1.static 修饰符的作用,加static的是静态成员,在类加载时就定义,不需要创建对象(即不需要new),有点类似全局变量。

       public static DataTable name(string xx,string yy)
{    }

public DataTable name(string xx,string yy)
{    }
有什么不同呢,我现在只知道如果是第一个,可以直接用  类名.name调用
而第二个必须 classname xx=new classname()后
用xx.name调用,既然这么这样麻烦点,为什么不总是用第一种方法呢??
都是返回了一个DataTable,第一个还相对简单点


比如你定义一个学生类
public class student
{
   public string name;
   public string grade;
   ......
}
学校的每个同学都是里面的一个实例
student zhangsan = new student();
zhangsan.name="zhangsan";
zhangsan.grade="2";

student lisi= new student();
lisi.name="lisi";
lisi.grade="3";

而静态的
public class student
{
   public static string name;
   public static string grade;
   ......
}
你只能够
student.name="zhangsan";
student.grade="2";

重新赋值的时候就丢掉了以前的东西了
student.name="lisi";
student.grade="3";


你只要引用了这个类static开辟的内存就存在了,但是不会反复开辟,
非static变量在new 的时候才分配内存,但是可以new多次,所以会反复分配内存
搂主的理解反了


0 0