/*面向对象上
函数的参数传递
基本数据类型的参数传递
数组的传递和对象引用变量的情况是一样的.
*/
1.理解面向对象的概念:
面向过程MoveWindow
面向对象Move 类与对象,类是对某一类事物的描述,是抽象的,概念上的定义:对象是实际存在的该事物的每个个体,因而也称实例.
类可以将数据和函数封装在一起.
创建对象要用new关键字.并且对象是具有生命周期的
class PassParam
{
int x;
public static void main(String[] args)
{
System.out.println(args[0]);//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
System.out.println(args[1]);//aaa bbb 运行时传递的一个参数.运行时参数用空格格开
/*int x=5;
change(x);
System.out.println(x);*/
PassParam obj=new PassParam();
obj.x=5;
change(obj);
System.out.println(obj.x);
int[]x=new int[1];
x[0]=5;
change(x);
System.out.println(x[0]);
}
public static void change(PassParam obj)
{
obj=new PassParam();//新创建的一个对象(参数传递)
obj.x=3;
}
public static void change(int[] x)
{
x[0]=3;
}
}
/*
static静态变量
静态方法里只能直接调用同类中的其它的静态成员,而不能直接访问同类中的非静态成员.这是因为,对于非静态的方法和变量,需要先创建类的实例
对象后才可使用,而静态方法使用前不用创建任何对象
静态方法不能以任何方式引用this和super关键字
main()方法是静态的
static静态变量巧妙应用:
1.单态设计模式(设计模式就是前人成功经验)
静态代码块:
*/
class Chinese
{
static Chinese objRef=new Chinese();//只有一份内存空间,相当于全局变量
private static int count=0;
private static String country="中国";
static //静态代码块
{
count=2;
System.out.println("static code");
}
String name;
int age;
public static Chinese getInstance()
{
return objRef;
}
private Chinese()//Chinese() 可以在 Chinese 中访问 private
{
//count++;
System.out.println(++count);
}
static void sing()
{
System.out.println("啊");
//singOurCountry();无法从静态上下文中引用非静态 方法 singOurCountry()
}
void singOurCountry()
{
System.out.println(country);
//System.gc();
sing();
}
}
class TestChinese
{
public static void main(String[] args)
{
/*System.out.println(Chinese.country);
Chinese ch1=new Chinese();
ch1.singOurCountry();
System.out.println(ch1.country);*/
/*System.out.println("begin");
Chinese.sing();
System.out.println("end");
new Chinese().sing();
new Chinese().singOurCountry();*/
Chinese obj1=Chinese.getInstance();
Chinese obj2=Chinese.getInstance();
System.out.println(obj1==obj2);
}
}