“equals”和“==”不一样,前者比较内容是否一致,后者比较对象是否一样(即使两个对象名称完全一样,也不符合==的逻辑)
******************************************
构造方法作业:
class Person
{
private int age=-1;
private String name="unknown";
void shout()
{
//int age=60;
System.out.println(name+"my age is"+age);
}
/*
public Person () {
age= 10;
System.out.println("the constructor1 is calling!");
}
*/
public Person(String x)
{
name = x;
}
public Person(String x,int y)
{
name =x;
age = y;
}
public void setAge(int x)
{
if(age<0)
return;
age = x;
}
public int getAge()
{
return age;
}
public static void getSomeOne(Person p)
{
p.shout();
}
public static void main(String [] args)
{
Person p1 = new Person();
Person p2 = new Person("zhangsan,20");
Person p3 = new Person("wangwu");
p1.age= -30;
p1.shout();
p2.shout();
p3.shout();
//p1.getSomeOne(p2);
/*String str1 = new String("abc");
String str2 = new String("abc");
String str3 = str1;
if(str1.equals(str2))
System.out.println("true");
else {
System.out.println("false");
}*/
//getSomeOne(new Person());
}
}
class TextPerson
{
public static void main(String [] args)
{
/*Person p1 = new Person();
Person p2 = new Person();
p1.age= -30;
p1.shout();
p2.shout();
*/
/*String str1 = new String("abc");
String str2 = new String("abc");
String str3 = str1;
if(str1.equals(str2))
System.out.println("true");
else {
System.out.println("false");
}*/
//getSomeOne(new Person());
}
public static void getSomeOne(Person p)
{
p.shout();
}
}
***************************************************
this的用法
<span style="font-size:14px;"> public Person(String name)
{
this.name = name;
}
public Person(String name,int age)
{
this.name = name;
this.age = age;
}</span>
********************************************************
垃圾回收:finalize方法
import java.awt.Component;
class Person
{
private int age=-1;
private String name="unknown";
void shout()
{
//int age=60;
System.out.println(name+"my age is"+age);
}
public Person () {
age= 10;
System.out.println("the constructor1 is calling!");
}
public Person(String name)
{
this.name = name;
}
public Person(<span style="color:#FF0000;">String name,int age</span>)
{
/*this.name = name;*/
<span style="color:#FF0000;">this(name);</span>
<span style="color:#FF0000;">this.age = age;</span>
}
public void <span style="color:#FF0000;">finalize</span>()
{
System.out.println("object is going");
}
public void setAge(int x)
{
if(age < 0)
return;
age = x;
}
public int getAge()
{
return age;
}
public static 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();
}
public static void main(String [] args)
{
/*Person a1 = new Person("a1");
a1.fun2();
*/
new Person();
new Person();
new Person();
System.gc();
/*Person p1 = new Person("ni");
Person p2 = new Person("zhangsan,20");
Person p3 = new Person("wangwu");
p1.age= -30;
p1.shout();
p2.shout();
p3.shout();
*/
//p1.getSomeOne(p2);
/*String str1 = new String("abc");
String str2 = new String("abc");
String str3 = str1;
if(str1.equals(str2))
System.out.println("true");
else {
System.out.println("false");
}*/
//getSomeOne(new Person());
}
}
class TextPerson
{
public static void main(String [] args)
{
/*Person p1 = new Person();
Person p2 = new Person();
p1.age= -30;
p1.shout();
p2.shout();
*/
/*String str1 = new String("abc");
String str2 = new String("abc");
String str3 = str1;
if(str1.equals(str2))
System.out.println("true");
else {
System.out.println("false");
}*/
//getSomeOne(new Person());
}
public static void getSomeOne(Person p)
{
p.shout();
}
}
/*
class Container
{
Component comp;
public void addCompenent(Component comp)
{
comp = new Component(new Container());
}
}
*/
/*
class Component
{
Container c;
public Component(Container c)
{
this.c=this.c;
}
}
*/