使用this调用本类方法

除了调用属性之外,this也可以实现方法的调用,但是对于方法的调用就必须考虑构造与普通方法

  • 构造方法调用(this()):使用关键字new实例化对象的时候才会调用构造方法;
  • 普通方法调用(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

对于本类构造方法的互相调用需要注意以下几点重要问题:

  • 构造方法必须在实例化新对象的时候调用,所以“this()”的语句只允许放在构造方法的首行;
  • 构造方法互相调用时请保留有程序的出口,别形成死循环;

四参、三参 两参 无参构造函数


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());
}
}

最新文章

  1. 应用OpenMP的一个简单的设计模式
  2. coding.net就这么横空出世
  3. XObject.java 对象还没写完,希望电脑不会丢失。坏笑,早点见。
  4. Remove openjdk in Ubuntu/Configure jdk and running adb in 64-bit Ubuntu
  5. vtk renderer / rendering 绘制
  6. Object C学习笔记25-文件管理(一)
  7. Objective-C与C++的区别
  8. Python实现PLA(感知机)
  9. 4种方法生成二维码 (js 控制canvas 画出 二维码)
  10. codevs1506传话(kosaraju算法)
  11. c#中,DataTable 过滤重复行
  12. IDFA的值什么时候会发生改变
  13. ios说说自己的计划是什么样的发展论坛
  14. C#多线程--仓库问题引发的故事
  15. Python 装饰器装饰类中的方法
  16. POJ 3182 The Grove [DP(spfa) 射线法]
  17. jQuery兼容浏览器IE8方法
  18. Delphi备忘录——基本语句
  19. install ubuntu env
  20. HTML标签-----article、aside、figure、nav和section

热门文章

  1. HTML学习(5)标题、水平线、注释
  2. 扩展欧几里得求解同余方程(poj 1061)
  3. Python开发中国象棋实战(附源码)
  4. 情人节用Python智能聊天机器人的实现|制作一个虚拟恋人
  5. WIN10 设置WEB
  6. Bugku-CTF之web8(txt????)
  7. Caffe 笔记 (一)caffe的层与数据结构
  8. nginx配置 yii2 URL重写规则 SSI配置使shtml
  9. 前端之HTML基础篇
  10. P&R 5