7.构造方法、方法重载和this关键字

上节回顾:

public class Phone{
    String brand;
    int price;
    void show()
    {
        System.out.println("品牌:"+brand+",价格:"+price);
    }
    void setBrand(String s)
    {
        brand=s;
    }
    void setPrice(int p)
    {
        price = p;
    }
    String getBrand()
    {
        return brand;
    }
    int getPrice()
    {
        return price;
    }
    
    
    public static void main(String[] args)
    {
        Phone p=new Phone();
        p.show();
		p.setBrand("华为p30");
		p.setPrice(1999);
		p.show();
		String pp=p.getBrand();
		int jq=p.getPrice();
		System.out.println("获取到的品牌为:"+pp+",价格为:"+jq);
    }
    
}

今天内容:

  • 构造方法和方法重载
  • this关键字
  • 方法的传参和递归调用
1.构造方法和方法重载

Person p = new Person(); - 声明Person类型的引用指向Person类型的对象

p.show(); - 调用名字为show()的成员方法

1.1构造方法(重重之重)

构造方法进行成员变量的初始化工作

(1)语法格式

class Person
{
	Person()
	{
		
	}
}
//1.方法名称与类名完全相同,并且没有返回值类型,连void都不许有
//2.当使用new关键字创建/构造对象时,会自动调用构造方法进行成员变量的初始化工作
//3.默认构造方法
//当一个类中没有自定义任何形式的构造方法时,编译器会自动提供一个无参的空构造方法称为默认/缺省构造方法
//当类中出现自定义构造方法时,则编译器不再提供任何形式的构造方法


public class Person
{
    String name;
    int age;
    void show()
    {
        
        System.out.println("我是"+name+",今年"+age+"岁!");
    }
    //自定义构造方法
	Person()
	{
		name = "zhang";
        age = 0;
	}
	//带参数的构造方法
	Person(String s,int a)
	{
		name=s;
		age=a;
	}
    void grow()
    {
        age++;
    }
    void grow(int i)
    {
        age += i;
    }
    public static void main(String[] args){
        Person p = new Person("yaya",18);
        p.show();
		Person p1 = new Person();
        p1.show();
        System.out.println("-------");
        p.grow();
        p.show();
        p.grow(5);
        p.show();
    }
}

练习:

public class Point{
    int x;
    int y;
    Point()
    {
        x=0;
        y=0;
    }
    Point(int i,int j)
    {
        x=i;
        y=j;
    }
    void show()
    {
        System.out.println("该点横坐标:"+x+",纵坐标:"+y);
    }
    public static void main(String[] args)
    {
        Point p = new Point();
        p.show();
        Point p1= new Point(1,3);
        p1.show();
    }
}
//作业
public class Point{
    int x;
    int y;
    Point()
    {
        x=0;
        y=0;
    }
    Point(int i,int j)
    {
        x=i;
        y=j;
    }
    void show()
    {
        System.out.println("该点横坐标:"+x+",纵坐标:"+y);
    }
    void up()
    {
        y++;
    }
    void up(int i)
    {
        y += i;
    }
    public static void main(String[] args)
    {
        Point p = new Point();
        p.show();
        Point p1= new Point(1,3);
        p1.show();
        System.out.println("-----------");
        p.up();
        p.show();
        p.up(3);
        p.show();
    }
}

在这里插入图片描述

1.2方法的重载

(1)基本概念

​ 在Java语言中允许方法名相同参数列表不同,这样的方法之间构成重载关系。

(2)主要形式

方法重载的主要形式为:参数的个数不同、参数的类型不同、参数的顺序不同。

(3)实际意义

​ 重载的实际意义在于调用者只需记住一个方法名就能调用各种不同的版本实现不同的效果。

public class TestOverload{
    void show()
    {
        System.out.println("show()");
    }
	void show(int i){
		System.out.println("show(int)");
	}
	void show(int i,double j){
		System.out.println("show(int,double)");
	}
    public static void main(String[] args)
    {
        TestOverload to = new TestOverload();
        to.show();
		to.show(1);
		to.show(1,1.0);
    }

}
2.this关键字(原理、理解)
2.1基本概念

​ 对于构造方法来说,this关键字就表示当前正在构造的对象。

​ 对于成员方法来说,this关键字就表示当前正在调用的对象。

public class TestThis{
    TestThis()
    {
        System.out.println("构造方法this="+this);
    }
    void show()
    {
        System.out.println("成员方法this = "+this);
    }
    
    
    public static void main(String[] args)
    {
        TestThis tt = new TestThis();
        tt.show();
		System.out.println("main()方法中:tt="+tt);
    }
}
/*编程实现Boy类的定义*/
public class Boy{
    String name;
    Boy(){        
    }
    Boy(String s)
    {
        name=s;
    }
    void show()
    {
        System.out.println("我是"+this.name);
    }
    public static void main(String[] args)
    {
        Boy b1 = new Boy("zhang");
        b1.show();
        
        Boy b2 = new Boy("li");
        b2.show();
    }
}
2.2使用方式

(1)当形参变量名和成员变量名同名时,在方法体中会优先选择形参变量进行使用,若希望使用成员变量则需要在变量名的前面加上this.。

(2)在构造方法的第一行使用this(实参)的方式可以调用本类中的其他构造方法。

public class Person
{
    String name;
    int age;
    void show()
    {
        
        System.out.println("我是"+name+",今年"+age+"岁!");
    }
    //自定义构造方法
	Person()
	{
		name = "zhang";
        age = 0;
	}
	//带参数的构造方法
	Person(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
    void grow()
    {
        age++;
    }
    void grow(int i)
    {
        age += i;
    }
    public static void main(String[] args){
        Person p = new Person("yaya",18);
        p.show();
		Person p1 = new Person();
        p1.show();
        System.out.println("-------");
        p.grow();
        p.show();
        p.grow(5);
        p.show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MelodyYN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值