-------android培训、java培训、期待与您交流! ----------
构造函数:一旦建立对象就运行,给对象初始化
一般的函数是调用才被执行
静态方法被调用的形式
1.对象.方法名
2.类名.方法名
3.静态成员变量又叫类变量
4.类创建就存在静态变量,成员变量有对象才被加载
5.静态只能调用静态,不能调用非静。
2012 10 29
public static void main(String arguments[])
{
system.out.println(args);
}
for(int x=0;x<args.length;x++)
String[] args = new String[3];
String[] args = null;
什么时候使用static ?
1.static 修饰 成员变量和函数
2.对象中出现共享数据
什么时候定义静态函数?
class Person
{
String name;
pulbic static void show()
{
System.out.println(" ");
}
}
class T
{
public static void main(String args[])
{
// Person p = new Person();
//p.show();
Person.show();
}
}
/*
静态的应用
*/
class Demo
{
public static void main(String args[])
{
int [] arrys ={1,2,3};
int max = 0;
for(int x = 0;x<arrys.length;x++)
{
if(arrys[x]>arrys[max])
{
max=x;
}
System.out.p;rintln(max)
}
}
}
/*静态代码块*/
特点:
随着类的加载而执行,只执行一次,并优先于主函数
class StaticCode
{
static
{
System.out.println(" ");
}
}
《设计模式:单例设计模式》
1.先禁止其它程序建立该对象
2.在本类中自定义一个对象
3.提供对外访问方式
以上三点可以解释为:
1.将构造函数私有化
2.在类中创建本类对象
3.提供方法可以获取该对象
1.饿汉
class Sing
{
private Sing(){}
private static Sing s = new Sing();
public static Sing getIns()
{
return s;
}
}
class SingDemo
{
public static void main(String args[])
{
System.out.println("hello world");
Sing ss = Sing.getIns();
}
}
2.懒汉
class Sing
{
private static Sing s = null;
private Sing(){}
public static Sing getInt()
{
if(s==null)
{
s = new Sing();
return s ;
}
}
}
-------android培训、java培训、期待与您交流! ----------
详细请查看:http://edu.youkuaiyun.com/heima/