环境变量配置:运行 cmd ,查看set path,set classpath=%path%;C:/...
我的电脑-属性-高级-环境变量 ,设置path的路径
类内部的变量叫成员变量,函数内的变量叫局部变量,局部变量会隐藏成员变量
(overloaded)函数的重载就是在同一个类中允许同时存在一个以上的同名函数,只要他们的参数个数或类型不同即可。
构造函数的特点:1他与类相同的名称
2他不含返回值
3他不能在方法中用return语句返回一个值
被自动调用,产生对象后就被调用,如果没有构造函数系统会自动产生一个什么都不做的。例如person(){}
new类名(参数)
this
实现存在的对象被调用
每个成员方法内部豆油一个this引用变量指想调用这个方法的对象
finalize()方法 垃圾回收 类似于c++中的析够方法
例子文档
class Person
{
private int age;//=-1;显示初始化//这是一个成员变量 这个变量是private私有的
private String name;
void shout()//成员函数
{
//int age=60;//局部变量会隐藏类的成员变量
System.out.println(name+ " 's my age is "+age);
}
public Person()//这个函数是构造函数,前面没有返回类型
{
//age = 10;//对所有person对象进行初始化
System.out.println("the constructor1 is calling!");
}
public Person(String name)
{
//this.name = name;
}
public Person(String name,int age)
{
//this.name = name;
this(name);//构造方法调用构造方法
this.age = age;//this关键字的使用
}
public void finalize()
{
System.out.println("Object is gonging!");
}
public void setAge(int x)
{
if(age < 0)
return;
age = x;
}
public int getAge()
{
return age;
}
public void getSomeOne(Person p)
{
p.shout();
}
public void fun1()
{
System.out.println(name);
}
public void fun2()
{
Person a2 = new Person("a2");
a2.fun1();
this.fun1();//this已经存在的还不知道名字的对象,访问同类成员this也可以省略
}
public static void main(String [] args)//main函数,启动运行类
{
/*Person a1 = new Person("a1");
a1.fun2();*/
new Person();
new Person();
new Person();
System.gc();//收垃圾
/*Person p1 = new Person();
Person p2 = new Person("zangsan",20);
Person p3 = new Person("wangwu");
p1.age = -30;//如果age是private lesson3.java:17: age has private access in Person p1.age = -30;
p1.shout();
p2.shout();//p2标示对象初始化为0
p3.shout();*/
//p1.getSomeOne(p2);
/*String str1 = new String("abc");//定义一个String的引用变量
String str2 = new String("abc");
String str3 = str1;
//if(str1 == str2)//不是同一个对象
//if(str1 == str3)//指向的对象是同一个
if(str1.equals(str2))//用equals方法进行比较(长相)
System.out.println(true);
else
System.out.println(false);
//new Person().shout();//没有指定 匿名对象*/
//getSomeOne(new Person());//实例对象
}
}
class TestPerson
{
public static void main(String [] args)//main函数,启动运行类
{
Person p1 = new Person();
Person p2 = new Person();
//p1.age = -30;//如果age是private lesson3.java:17: age has private access in Person p1.age = -30;
p1.shout();
p2.shout();//p2标示对象初始化为0
/*String str1 = new String("abc");//定义一个String的引用变量
String str2 = new String("abc");
String str3 = str1;
//if(str1 == str2)//不是同一个对象
//if(str1 == str3)//指向的对象是同一个
if(str1.equals(str2))//用equals方法进行比较(长相)
System.out.println(true);
else
System.out.println(false);
//new Person().shout();//没有指定 匿名对象*/
getSomeOne(new Person());//实例对象
}
public static void getSomeOne(Person p)//p是引用变量
{
p.shout();
}
}
class Container
{
Component comp;
public void addComponent()
{
comp = new Component(this);
}
}
class Component
{
Container c;
public Component(Container c)
{
this.c = c;
}
}