上节回顾:
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();
}
}