类和对象
方法的使用
1.方法
方法本身就是一个代码块,只是增加了一些代码块的描述(参数列表、返回值类型…)
main方法是程序的入口,现阶段程序想要运行必须执行main方法、可以在main方法中调用其他方法
public static void main(String[] args) {
String str = "world";
hello(str, 11);
hello("java", 12);
int sum = plus(12, 30);
System.out.println(sum);
}
2.参数列表
声明需要传入代码块中的变量参数
/**
* 演示方法的参数列表
* @param str
* @param a
*/
public static void hello(String str, int a) {
System.out.println("hello " + str + " " + a);
}
3.返回值
方法被调用之后,返回的结果
void表示空返回值,该方法没有返回值,调用之后不生成结果
// 编写一个方法,传入两个参数,实现两个数的求和
/**
* 演示方法的返回值
* @param a
* @param b
* @return
*/
public static int plus(int a, int b) {
return a + b;
}
4.注意
1.static修饰符修饰的方法,才可以直接被main方法调用。
2.方法名和变量名命名规则一致
对象的创建和使用
创建
1.使用关键字new。
2.使用构造方法,默认每一个类都会有一个无参的构造方法。
// 1.创建对象
Student stu1 = new Student();
System.out.println(stu1);
构造方法
1.用来创建对象
2.方法名和类名一致
3.使用无参构造创建出来的对象,所有的属性都是默认值,未做初始化赋值。
// 2.使用方法
stu1.showInfo();
stu1.study("java");
stu1.eat("薯片");
使用
1.使用属性
1.直接使用对象名加"."调用属性
// 3.使用属性
stu1.name = "张文杰";
stu1.sex = '女';
stu1.age = 18;
stu1.number = "00038";
stu1.showInfo();
2.使用方法
直接使用对象名加"."调用方法
实体类
1.属性
成员变量,描述实体的属性
2.方法
成员方法,描述实体的行为
public class Student {
// 1.公开的成员变量
// 姓名
public String name;
// 年龄
public int age;
// 性别
public char sex;
// 学号
public String number;
// 2.公开的成员方法
// 学习
public void study(String coursor) {
System.out.println("我爱学" + coursor);
}
// 吃
public void eat(String food) {
System.out.println("我爱吃" + food);
}
// 自我介绍方法
public void showInfo() {
System.out.println("我叫" + name + ",我是一个" + sex + "生,我今年" + age + "岁,我的学号是" + number);
}
}
例1:写一个用户实体类
public class User {
public String username;
public String password;
}
例2:定义方法,返回输入区间内的随机数
定义一个方法,传入区间[a,b],调用这个方法返回该区间内的随机数。
public class Work01 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
System.out.println(getRandomNum(3, 33));
}
}
/**
* 传入区间,生成随机数
* @param a
* @param b
* @return
*/
private static int getRandomNum(int a, int b) {
// Math.random() => [0, 1) 浮点数
// a = 0; b = 9
// Math.random() * 10 => [0, 10) 浮点数
// (int)(Math.random() * 10) => [0, 9] 整数
// a = 1; b = 10
// (int)(Math.random() * 10 + 1) => [1, 10] 整数
// a = 0; b = 10
// (int)(Math.random() * 11) => [0, 10] 整数
// a = 3; b = 12
// (int)(Math.random() * (12 - 3 + 1) + 3) => [3, 12] 整数
// (int)(Math.random() * (b - a + 1) + a) => [a, b] 整数
return (int)(Math.random() * (b - a + 1) + a);
}
}
例3:用户注册和登录业务
public class Work02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 创建一个数组用于存放用户对象
User[] users = new User[10];
while (true) {
System.out.println("请选择(1.登录 2.注册 0.退出):");
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String password = sc.next();
boolean result = false;
for (User user : users) {
if (user != null && username.equals(user.username) && password.equals(user.password) ) {
result = true;
break;
}
}
if (result) {
System.out.println("登录成功!");
} else {
System.out.println("登录失败!");
}
break;
case 2:
User user = new User();
System.out.println("请输入用户名:");
user.username = sc.next();
System.out.println("请输入密码:");
user.password = sc.next();
System.out.println("注册成功!");
for (int i = 0; i < users.length; i++) {
if (users[i] == null) {
users[i] = user;
break;
}
}
break;
case 0:
sc.close();
System.out.println("退出系统!");
return;
default:
System.out.println("选择错误!");
break;
}
}
}
}