关于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);
}
}
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);
}
}
相关文章推荐
- 如何有效预防宕机?你需要掌握这4个方法
- ecshop首页调用指定商品分类下的商品品牌列表
- Sublime Text2安装Package Control
- linux安装apache的纠结过程 http://www.cnblogs.com/fnng/archive/2012/08/30/2662984.html
- 抓取教程网页的小爬虫
- 安卓四大组件----活动
- hdu 1258 Sum It Up
- 风机桨叶故障诊断(二) 获取图像几何主方向
- 快速排序
- eclipse的快捷键
- 不要忽略小题和细节
- 深入理解7816协议(1)---- 关于F/D和etu
- [翻译] AYVibrantButton
- 常用服务器日志分析命令大全
- jar反编译神器
- 标准的make file 模板
- 进程中内存区域的划分
- 【UVA1146】NOW OR LATER 2-SAT问题
- XAMPP+Mantis安装步骤
- 讲解actionbar 比较好的文章