标签:结构 产生 互相调用 构造 允许 turn 工资 person this
使用this调用本类方法
除了调用属性之外,this也可以实现方法的调用,但是对于方法的调用就必须考虑构造与普通方法
普通的方法调用:
package study;
class Person{
private int age;
private String name;
public Person(int age ,String name) {
//this.age = age;
//this.name = name;
this.setAge(age);
this.setName(name);//加this与不加 都表示本类方法
}
public void intorduce() {
System.out.println("age= "+this.age+" name= "+this.name);
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
}
public class xxx {
public static void main(String[] args) {
new Person(13,"sss").intorduce();
}
}
构造方法的调用
对于构造方法的调用,肯定是要放在构造函数中执行,现在假设类中一共定义有三个构造方法,但是要求不管调用哪个构造方法,都执行一行输出语句
传统实现:
package study;
class Person{
private int age;
private String name;
public Person() {
System.out.println("一个新的Person类对象实例化。");
}
public Person(int age){
System.out.println("一个新的Person类对象实例化。");
this.age = age;
}
public Person(int age ,String name) {
System.out.println("一个新的Person类对象实例化。");
this.age = age;
this.name = name;
}
public void intorduce() {
System.out.println("age= "+this.age+" name= "+this.name);
}
}
public class xxx {
public static void main(String[] args) {
new Person(13,"sss").intorduce();
}
}
>>>一个新的Person类对象实例化。
>>>age= 13 name= sss
如果要想评价一个代码的好坏:
利用this()构造调用优化
class Person{
private int age;
private String name;
public Person() {
System.out.println("一个新的Person类对象实例化。");
}
public Person(int age){
this();//调用无参构造方法
this.age = age;
}
public Person(int age ,String name) {
this();//调用单参构造方法
this.name = name;
}
public void intorduce() {
System.out.println("age= "+this.age+" name= "+this.name);
}
}
public class xxx {
public static void main(String[] args) {
new Person(13,"sss").intorduce();
}
}
>>>一个新的Person类对象实例化。
>>>age= 0 name= sss
对于本类构造方法的互相调用需要注意以下几点重要问题:
四参、三参 两参 无参构造函数
public class Emp {
private long empo;//员工编号
private String ename;//员工名字
private String dept;//部门名称
private Double salary;//基本工资
public Emp() {
this.empo =1000l;
this.ename = "无名氏";
}
public Emp(long empo) {
this.empo = empo;
this.ename = "新员工";
this.dept = "未定";
this.salary = 0.0d;
}
public Emp(long empo,String ename,String dept){
this.empo = empo;
this.ename = ename;
this.dept = dept;
salary = 2500.0d;
}
public Emp(long empo,String ename,String dept,double salary) {
this.empo = empo;
this.ename =ename;
this.dept = dept;
this.salary = salary;
}
public String tell() {
return "empo:"+this.empo+" ename:"+this.ename+" dept:"+this.dept+" salary:"+this.salary;
}
public static void main(String[] args) {
Emp emp = new Emp(130l);
System.out.println(emp.tell());
}
}
优化 得带赋值 四参构造足以撑起所有赋值
public class Emp {
private long empo;//员工编号
private String ename;//员工名字
private String dept;//部门名称
private Double salary;//基本工资
public Emp() {
this(1000l,"无名氏",null,0.0d);
// this.empo =1000l;
// this.ename = "无名氏";
}
public Emp(long empo) {
this(empo,"新员工","未定",0.0d);
// this.empo = empo;
// this.ename = "新员工";
// this.dept = "未定";
// this.salary = 0.0d;
}
public Emp(long empo,String ename,String dept){
this(empo,ename,dept,2500.0d);
// this.empo = empo;
// this.ename = ename;
// this.dept = dept;
// salary = 2500.0d;
}
public Emp(long empo,String ename,String dept,double salary) {
this.empo = empo;
this.ename =ename;
this.dept = dept;
this.salary = salary;
}
public String tell() {
return "empo:"+this.empo+" ename:"+this.ename+" dept:"+this.dept+" salary:"+this.salary;
}
public static void main(String[] args) {
Emp emp = new Emp(130l);
System.out.println(emp.tell());
}
}
标签:结构 产生 互相调用 构造 允许 turn 工资 person this
原文地址:https://www.cnblogs.com/sakura579/p/12324139.html