this相当于隐藏形成在对应的堆中且与相应的引用对象指向同一个地址
用hashcode()实验,可以将hashcode看做地址(本质不是地址)
public class Der {
public static void main(String[] args) {
this_use p=new this_use(18,"xiao");
p.thus();
System.out.println("p的hashcode="+p.hashCode());
this_use p1=new this_use(20,"mei");
p1.thus();
System.out.println("p的hashcode="+p1.hashCode());
}
}
class this_use{
int age;
String name;
public this_use(int a,String b)
{
this.age=a;
this.name=b;
System.out.println("this的hashcode="+this.hashCode());
}
public void thus()
{
System.out.println(age+name);
}
}
哪个对象调用,this就指向哪个
加this则访问的是属性的信息,不加就近原则访问
public class Der {
public static void main(String[] args) {
Person p=new Person("meimei",18);//这个值传入到构造器中
//除了这种传参方式,还可以用构造器
// p.age=18;
// p.name="meimei";
Person p1=new Person("meimeiw",18);
boolean y=p.compare(p1);//传入到方法compare中
System.out.println(y);
}
}
class Person{
String name;
int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public boolean compare(Person p1){
// String name="meimei";
// int age=18;
// if (this.name==name&&this.age==age)
// return true;
// else
// return false;
return (this.name.equals(p1.name))&&(this.age==p1.age);
}
}
public class Der {
public static void main(String[] args) {
Circle o=new Circle();
PassObject v=new PassObject();
v.printAreas(o,5);
}
}
class Circle{
double redious;
public double findAer(double o){
return Math.PI*o*o;
}
public void turn(double redious)
{
this.redious=redious;
}
}
class PassObject{
public void printAreas(Circle o,int times)
{
System.out.println("redious\t areas");
for (int i=1;i<=times;i++)
{
o.turn(i);//随着for循环可改变半径的值
System.out.println(i+ "\t"+o.findAer(o.redious));
}
}
}