您的位置:首页 > 其它

关于this 的作用和规则

2015-09-07 20:01 274 查看
云和学院 学生:毛孬 QQ:285673392

package com.yunhe.L97;

//this关键字

class Employee {
int empno;
String ename;
int deptno;// department
boolean gender;
float salary;
float comm;

// 构造方法用来在内存中创建对象
public Employee(){

}
public Employee(int empno, String ename, int deptno, boolean gender,float salary, float comm) {
super();//
System.out.println(this);
this.empno = empno;// this 指当前类的====当前对象
this.ename = ename;
this.deptno = deptno;
this.gender = gender;
this.salary = salary;
this.comm = comm;
}

public void fun() {
String res = this.toString();
System.out.println(res);
}

@Override
public String toString() {
return "Employee [empno=" + empno + ", ename=" + ename + ", deptno="
+ deptno + ", gender=" + gender + ", salary=" + salary
+ ", comm=" + comm + "]";
}

}

public class This {
public static void main(String[] args) {
// 开辟空间的同时赋值
Employee emp = new Employee(12, "lisi", 22, false, 88, 12);
System.out.println(emp.toString());
System.out.println("-----------");
emp.fun();
System.out.println("-----------");

// 先开辟空间,后给各属性赋值
Employee employee = new Employee();
employee.empno = 22;
employee.ename = "zhangsan";
employee.gender = true;
employee.deptno = 2;
employee.salary = 888;
employee.comm = 112;

System.out.println(employee);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: