关于this 的作用和规则

云和学院 学生:毛孬 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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值