方法重载、构造方法
注意
java源文件中,只能有一个被public修饰的类,并且该类名和文件名一致。
java源文件中,可以有多个类,类名不重复即可,但是不能使用public修饰。
一个java源文件中有多个类,那么编译后会生成多个独立的.class文件。
例如:
public class Demo01 {
public static void main(String[] args) {
System.out.println("hello");
}
}
class Demo01_2 {
public static void main(String[] args) {
System.out.println("hello");
}
}
class Demo01_3 {
public static void main(String[] args) {
System.out.println("hello");
}
}
static关键字
被static修饰的方法叫静态方法
静态方法
1.可以直接使用类名调用,无需创建对象
2.使用对象也可以调用类方法,但是所有对象公用一个方法
3.静态方法一般在工具类中使用
方法测试
1、创建一个静态(类)方法和非静态(成员)方法
// 在main方法中使用成员方法,需要先创建对象
Demo02 demo02 = new Demo02();
demo02.hello();
// main方法中使用类方法,可以直接调用
hi();
}
// 非静态方法、成员方法
public void hello() {
System.out.println("hello");
}
// 静态方法、类方法
public static void hi() {
System.out.println("hi");
}
2、创建一个随机数工具类
public class RandomUtil {
/**
* 传入区间,生成随机数
* @param start
* @param end
* @return
*/
public static int getRandomNum(int start, int end) {
return (int)(Math.random() * (end - start + 1) + start);
}
/**
* 传入指定随机数长度,生成随机数字符串
* @param length
* @return
*/
public static String getRandomNumByLength(int length) {
String result = "";
for (int i = 0; i < length; i++) {
result += (int)(Math.random() * 10);
}
return result;
}
}
3、测试调用其他类中的成员方法和类方法
public static void main(String[] args) {
// 先创建对象再调用成员方法
Demo02 demo02 = new Demo02();
demo02.hello();
// 可以直接使用类名调用类方法
Demo02.hi();
// 使用对象也可以调用类方法
demo02.hi();
// 使用工具类中的静态方法
int num1 = RandomUtil.getRandomNum(1, 1000);
System.out.println(num1);
String num2 = RandomUtil.getRandomNumByLength(24);
System.out.println(num2);
}
方法的重载
1.原因
在调用同名方法时,可以根据参数列表的不同来区分不同的方法。
2.条件
1.发生在同一个类中
2.方法名必须相同
3.参数列表必须不同(参数的个数、类型)
4.与返回值类型无关(只有返回值不一样不能构成重载)
5.与权限修饰符无关
// 两个整数的加法
public int plus(int a, int b) {
return a + b;
}
// private double plus(int a, int b) {
// return a + b;
// }
// 两个浮点数的加法
public double plus(double a, double b) {
return a + b;
}
// 三个浮点数的加法
public double plus(double a, double b, double c) {
return a + b + c;
}
public double plus(int a, double b) {
return a + b;
}
// public double plus(int b, double a) {
// return a + b;
// }
public double plus(double a, int b) {
return a + b;
}
构造方法
无参构造
1.系统默认提供,可以不写。也可以手动编写。
2.如果写了有参构造,那么系统将不默认提供无参构造。
有参构造
1.用于创建对象时直接初始化赋值。
2.可以使用this关键字,区分本对象的属性和方法的参数。
注意
1.构造方法没有返回值,不能写类型或者void。
2.构造方法方法名和类名相同。
// 使用无参构造创建对象,属性需要额外赋值
Computer c1 = new Computer();
c1.game();
c1.brand = "小米笔记本";
c1.type = "pro";
c1.price = 6700.7;
c1.cpu = "i7 8th";
c1.videoCard = "mx150";
c1.showInfo();
System.out.println("=====================");
// 使用有参构造创建对象,在创建对象的同时进行赋值
Computer c2 = new Computer("rog", "幻14", 15000.5, "i7 10th", "rtx2070");
c2.showInfo();
例题1:女朋友类和程序员类
1、主类
/**
* 编写一个女朋友类
* 属性:姓名、年龄、颜值
* 方法:购物、吃
*
* 编写一个程序员类
* 属性:姓名、年龄、女朋友
* 方法:敲代码、陪女朋友逛街
*
* 创建一个程序员对象,并使用有参构造方法初始化赋值。
* 测试调用程序员对象的方法。
*
* @author junki
* @date 2020年6月28日
*/
public class Work01 {
public static void main(String[] args) {
Girlfriend gf = new Girlfriend("张文杰", 18, 100);
Programmer programmer = new Programmer("张瑞", 19, gf);
programmer.codding();
programmer.shoppingWithGf();
}
}
2、女朋友类
public class Girlfriend {
public String name;
public int age;
public int faceScore;
public Girlfriend() {
}
public Girlfriend(String name, int age, int faceScore) {
this.name = name;
this.age = age;
this.faceScore = faceScore;
}
public void shopping() {
System.out.println("买买买");
}
public void eat() {
System.out.println("吃吃吃");
}
}
3、程序员类
/**
* 程序员类
* @author junki
* @date 2020年6月28日
*/
public class Programmer {
public String name;
public int age;
public Girlfriend girlfriend;
public Programmer() {
}
public Programmer(String name, int age, Girlfriend girlfriend) {
this.name = name;
this.age = age;
this.girlfriend = girlfriend;
}
public void codding() {
System.out.println("努力敲代码,挣钱!");
}
public void shoppingWithGf() {
System.out.println("陪" + girlfriend.name + "逛街买东西。");
}
}
例题2:武器类和英雄类
1、主类
/**
* 编写一个武器类:
* 属性:名称、等级、耐久度
*
* 编写一个英雄类:
* 属性:姓名、金币、武器
* 方法:强化武器、打怪赚钱
*
* 注意:
* 1.打怪可以增加金币、减少武器耐久度。
* 2.强化武器可以增加武器等级、需要消耗金币,
* 强化有一定概率会失败,失败会下降一个等级,强化成功则增加一个等级。
*
* 需求:
* 1.使用循环结构加选择结构,实现英雄打怪赚取金币并强化武器的游戏。
* 2.具体游戏规则自行定义,如:打怪增加金币数、强化消耗金币数、强化概率。
* 3.最终在武器耐久度为0时结束游戏。
*
*
*
*
* @author junki
* @date 2020年6月28日
*/
public class Work02 {
public static void main(String[] args) {
Weapon weapon = new Weapon("张文杰的头", 7, 100);
Hero hero = new Hero("张文杰", 50, weapon);
Scanner sc = new Scanner(System.in);
while (hero.weapon.durability > 0 || hero.money > 15) {
System.out.println("请选择:1.打怪赚钱 2.强化武器");
int choose = sc.nextInt();
switch (choose) {
case 1:
hero.killMonster();
break;
case 2:
hero.upWeapon();
break;
default:
System.out.println("选择错误!");
break;
}
}
sc.close();
}
}
2、武器类
/**
* 武器类
* @author junki
* @date 2020年6月29日
*/
public class Weapon {
public String name;
public int level;
public int durability;
public Weapon() {
}
public Weapon(String name, int level, int durability) {
this.name = name;
this.level = level;
this.durability = durability;
}
}
3、英雄类
/**
* 英雄类
* @author junki
* @date 2020年6月29日
*/
public class Hero {
public String name;
public int money;
public Weapon weapon;
public Hero() {
}
public Hero(String name, int money, Weapon weapon) {
this.name = name;
this.money = money;
this.weapon = weapon;
}
// 打怪挣钱
public void killMonster() {
if (this.weapon.durability < 0) {
System.out.println("武器耐久不足!");
return;
}
System.out.println("刷了10个怪!");
this.weapon.durability--;
this.money += 10;
System.out.println("武器耐久:" + this.weapon.durability + ";金币:" + this.money);
}
// 强化武器
public void upWeapon() {
if (this.money < 15) {
System.out.println("金币不够!");
return;
}
System.out.println("消耗15金币强化武器!");
this.money -= 15;
int randomNum = RandomUtil.getRandomNum(1, 100);
if (randomNum <= 66) {
this.weapon.level++;
System.out.println("强化成功,当前等级" + this.weapon.level);
} else {
this.weapon.level--;
System.out.println("强化失败,当前等级" + this.weapon.level);
}
System.out.println("剩余金币:" + this.money);
}
}