this关键字.5

关键字this 使用访问问题:表示出当前对象
使用this访问本类属性:this.(属性)。

使用this调用方法

package thisa;
class Person
{
private String name;
private int age;
public Person() {}
public  Person(String n,int a)//n a这样的取名没意义,最好参数的名称和属性的名称一致
{
name=n;
age=a;
}
public void setName(String n) 
{
name=n;
}
public void setAge(int a)
{
age=a;
}
public String getName() 
{
return name;
}
public int getAge() {
return age;
}
public String getInfo() 
{
return "姓名:"+name+"年龄:"+age;
}
}
public class thisa {


public static void main(String[] args) {
// TODO Auto-generated method stub
Person per=new Person("张三",20);
System.out.println(per.getInfo());
}

}


修改为:

package thisa;

class Persona
{
private String name;
private int age;
public Persona() {}
public  Persona(String name,int age)//参数的名称和属性的名称一致了
{
this.name=name;
this.age=age;
}
public void setName(String name) 
{
this.name=name;
}
public void setAge(int age)
{
this.age=age;
}
public String getName() 
{
return name;
}
public int getAge() {
return age;
}
public String getInfo() 
{
return "姓名:"+name+"年龄:"+age;
}

}
public class thisb {


public static void main(String[] args) {
// TODO Auto-generated method stub
Persona per=new Persona("张三",20);
System.out.println(per.getInfo());
}
}

方法有两类:普通和构造  

互调操作:(thisC)

package thisa;
//实现互调操作
class Personb
{
private String name;
private int age;

//setter,getter省略

扫描二维码关注公众号,回复: 2169428 查看本文章

        public Personb(){}

public Personb(){
System.out.println("1.无参构造");
}
public Personb(String name){
this();//调用无参的
System.out.println("2.有一个参数");
}
public Personb(String name,int age){
this(name);//调用一个无参的
System.out.println("3.有二个参数");
}
}
public class thisc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Personb per=new Personb("三",90);
}
}
注意也有要注意的地方
使用this调用构造方法时必须放在构造方法的首行

使用this()进行构造方法互相调用时,至少留下一个出口,即:不使用this调用其它构造


思考题:
定义一个雇员的数据类型
第一种
class Emp
{
private int empno;
private String name;
private String dept;
private double salary;
public Emp() {
this.empno=1000;
this.name="无名";
this.dept="未定";
this.salary=0.0;
}
public Emp(int empno) {
this.empno=empno;
this.name="无名";
this.dept="后勤";
this.salary=1000.0;
}
public Emp(int empno,String name,String dept) {
this.empno=empno;
this.name=name;
this.dept=dept;
this.salary=3000.0;
}
public Emp(int empno,String name,String dept,double salary) {
this.empno=empno;
this.name=name;
this.dept=dept;
this.salary=salary;
}
public String getInfo()
{
return "编号:"+this.empno+
"姓名:"+this.name+
"部门:"+this.dept+
"工资:"+this.salary;
}
}
public class thisd {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Emp().getInfo());
System.out.println(new Emp(1000).getInfo());
System.out.println(new Emp(1000,"sa","sd").getInfo());
System.out.println(new Emp(1000,"sa","sd",1000.0).getInfo());
}

}

这种简单,但有部分代码重复了

改修:

package thisa;
class Emp
{
private int empno;
private String name;
private String dept;
private double salary;
public Emp() {
this(1000,"无名","未定",0.0);
}
public Emp(int empno) {
this(empno,"无名","后勤",1000.0);
}
public Emp(int empno,String name,String dept) {
this(empno,name,dept,3000.0);
}
public Emp(int empno,String name,String dept,double salary) {
this.empno=empno;
this.name=name;
this.dept=dept;
this.salary=salary;
}
public String getInfo()
{
return "编号:"+this.empno+
"姓名:"+this.name+
"部门:"+this.dept+
"工资:"+this.salary;
}
}
public class thisd {


public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(new Emp().getInfo());
System.out.println(new Emp(1000).getInfo());
System.out.println(new Emp(1000,"sa","sd").getInfo());
System.out.println(new Emp(1000,"sa","sd",1000.0).getInfo());
}

}

this表示当前对象(开头)
观察当前对象:
class Demo{
public void print()
{
System.out.println("**Demo类中的输入"+this);
}
}
public class thise {


public static void main(String[] args) {
// TODO Auto-generated method stub
Demo da=new Demo();
System.out.println("主类中的输出"+da);
da.print();
System.out.println("==========");
Demo db=new Demo();
System.out.println("主类中的输出"+db);
db.print();
}

}

package thisa;
class Demo{
private MyUtil mu;
public Demo() //2.执行构造方法
{
this.mu=new MyUtil(this);//3.实例化MyUtil对象,this相当于demo
this.mu.fun();//6.调用MyUtil方法
}
public void print() //8.执行此方法
{
System.out.println("Hello world .");
}
}
class MyUtil
{
private Demo demo;
public MyUtil(Demo d)//4.调用构造,d=主类的demo
{
this.demo=d;//5.将主类的demo传递给MyUtil类的demo属性
}
public void fun()
{
this.demo.print();//调用Demo类的方法
}
}
public class thise {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo=new Demo();//1.实例化Demo类对象
//demo.print();相当于这个
}
}

猜你喜欢

转载自blog.csdn.net/a584898/article/details/81057351
今日推荐