day08-静态代码块的练习
This commit is contained in:
@@ -22,6 +22,7 @@ public class Demo11 {
|
|||||||
Student s1 = new Student();
|
Student s1 = new Student();
|
||||||
Student s2 = new Student();
|
Student s2 = new Student();
|
||||||
Student s3 = new Student();
|
Student s3 = new Student();
|
||||||
|
Student.clazz = "1903室";
|
||||||
//询问3个同学的学号分别是什么
|
//询问3个同学的学号分别是什么
|
||||||
System.out.println(s1.id);
|
System.out.println(s1.id);
|
||||||
System.out.println(s2.id);
|
System.out.println(s2.id);
|
||||||
|
|||||||
30
day08/src/com/inmind/static02/Demo12.java
Normal file
30
day08/src/com/inmind/static02/Demo12.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.static02;
|
||||||
|
/*
|
||||||
|
静态代码块:
|
||||||
|
定义在成员位置,使用static修饰的代码块{ }。
|
||||||
|
位置:类中方法外。
|
||||||
|
执行:随着类的加载而执行且执行一次,优先于main方法和构造方法的执行。
|
||||||
|
|
||||||
|
格式:
|
||||||
|
static{
|
||||||
|
java语句;
|
||||||
|
}
|
||||||
|
|
||||||
|
静态代码块的作用:在程序运行之前,就进行一些操作,常用于对静态变量的初始化操作,后期工作中
|
||||||
|
常用于对配置文件的值赋值操作
|
||||||
|
*/
|
||||||
|
public class Demo12 {
|
||||||
|
|
||||||
|
//静态代码块
|
||||||
|
static {
|
||||||
|
System.out.println("静态代码块执行了");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("main方法开始了");
|
||||||
|
Student s = new Student();
|
||||||
|
s.show2();
|
||||||
|
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,15 @@ public class Student {
|
|||||||
//静态变量
|
//静态变量
|
||||||
static int studentCount;
|
static int studentCount;
|
||||||
|
|
||||||
|
public static void show(){
|
||||||
|
System.out.println(studentCount);
|
||||||
|
System.out.println(clazz);
|
||||||
|
show1();
|
||||||
|
// System.out.println(this);//静态方法中,不能使用this关键字。
|
||||||
|
// System.out.println(name); 成员变量和成员方法在静态方法中都不能访问
|
||||||
|
// show2();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Student(String name, int age, int id) {
|
public Student(String name, int age, int id) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@@ -19,6 +28,7 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Student() {
|
public Student() {
|
||||||
|
System.out.println("学生类的无参构造方法执行了");
|
||||||
//每次创建一个新的学生,都让共享的学生总数静态变量自增1
|
//每次创建一个新的学生,都让共享的学生总数静态变量自增1
|
||||||
studentCount++;
|
studentCount++;
|
||||||
this.id = studentCount;
|
this.id = studentCount;
|
||||||
@@ -26,21 +36,12 @@ public class Student {
|
|||||||
|
|
||||||
|
|
||||||
//静态方法 类方法
|
//静态方法 类方法
|
||||||
|
|
||||||
/*
|
/*
|
||||||
静态方法调用的注意事项:
|
静态方法调用的注意事项:
|
||||||
1.静态方法可以直接访问类变量(静态变量)和静态方法。
|
1.静态方法可以直接访问类变量(静态变量)和静态方法。
|
||||||
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,反之,成员方法可以直接访问静态变量或静态方法。
|
2.静态方法不能直接访问普通成员变量或成员方法。静态方法只能访问静态内容,反之,成员方法可以直接访问静态变量或静态方法。
|
||||||
3.静态方法中,不能使用this关键字。
|
3.静态方法中,不能使用this关键字。
|
||||||
*/
|
*/
|
||||||
public static void show(){
|
|
||||||
System.out.println(studentCount);
|
|
||||||
System.out.println(clazz);
|
|
||||||
show1();
|
|
||||||
// System.out.println(this);//静态方法中,不能使用this关键字。
|
|
||||||
// System.out.println(name); 成员变量和成员方法在静态方法中都不能访问
|
|
||||||
// show2();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void show1(){
|
public static void show1(){
|
||||||
}
|
}
|
||||||
|
|||||||
67
day08/src/com/inmind/static02/Test13.java
Normal file
67
day08/src/com/inmind/static02/Test13.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package com.inmind.static02;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/*
|
||||||
|
模拟用户登录。
|
||||||
|
* 定义用户类,属性为用户名和密码。
|
||||||
|
* 使用集合存储多个用户对象。
|
||||||
|
* 录入用户和密码,对比用户信息,匹配成功登录成功,否则登录失败。
|
||||||
|
* 登录失败时,当用户名错误,提示没有该用户。
|
||||||
|
* 登录失败时,当密码错误时,提示密码有误。
|
||||||
|
*/
|
||||||
|
public class Test13 {
|
||||||
|
static ArrayList<User> users = new ArrayList<>();//静态变量
|
||||||
|
|
||||||
|
//我要求,在主程序开始运行之前,就准备好一些可以登录的用户对象
|
||||||
|
// 静态代码块的作用:在程序运行之前,就进行一些操作,常用于对静态变量的初始化操作
|
||||||
|
static {
|
||||||
|
users.add(new User("张三", "12346"));
|
||||||
|
users.add(new User("李四", "12346"));
|
||||||
|
users.add(new User("王五", "12346"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//录入用户和密码,对比用户信息,匹配成功登录成功,否则登录失败。
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("请输入用户名:");
|
||||||
|
String username = sc.nextLine();
|
||||||
|
System.out.println("请输入密码:");
|
||||||
|
String password = sc.nextLine();
|
||||||
|
User user = new User(username, password);
|
||||||
|
//定义一个接收用户的方法,判断是否登录成功
|
||||||
|
boolean result = login(user);
|
||||||
|
System.out.println("是否登录成功:"+result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean login(User user) {
|
||||||
|
//判断用户名和密码
|
||||||
|
String password = user.getPassword();
|
||||||
|
String username = user.getUsername();
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
//遍历已经定义好的用户信息,如果一致则成功,否则失败
|
||||||
|
for (int i = 0; i < users.size(); i++) {
|
||||||
|
//获取每个用户,一一比对
|
||||||
|
User u = users.get(i);
|
||||||
|
if (u.getUsername().equals(username)) {
|
||||||
|
//判断密码
|
||||||
|
if (u.getPassword().equals(password)) {
|
||||||
|
result = true;
|
||||||
|
// break;
|
||||||
|
} else {
|
||||||
|
System.out.println("密码不正确");
|
||||||
|
// break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*if (!result) {
|
||||||
|
System.out.println("用户名不正确");
|
||||||
|
}*/
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
30
day08/src/com/inmind/static02/User.java
Normal file
30
day08/src/com/inmind/static02/User.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.inmind.static02;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(String username, String password) {
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user