guyanqing преди 1 година
родител
ревизия
0bc1fe9e58

+ 123 - 0
src/main/java/com/sf/day07/MyDate.java

@@ -0,0 +1,123 @@
+package com.sf.day07;
+
+import com.sun.org.apache.bcel.internal.generic.NEW;
+
+import java.time.Year;
+import java.util.Date;
+import java.util.Scanner;
+
+public class MyDate {
+    private Integer year;
+    private Integer month;
+    private Integer day;
+
+    public MyDate() {
+    }
+
+    public MyDate(Integer year, Integer month, Integer day) {
+        this.year = year;
+        this.month = month;
+        this.day = day;
+    }
+
+    public Integer getYear() {
+        return year;
+    }
+
+    public void setYear(Integer year) {
+        this.year = year;
+    }
+
+    public Integer getMonth() {
+        return month;
+    }
+
+    public void setMonth(Integer month) {
+        this.month = month;
+    }
+
+    public Integer getDay() {
+        return day;
+    }
+
+    public void setDay(Integer day) {
+        this.day = day;
+    }
+
+    @Override
+    public String toString() {
+        return "MyDate{" +
+                "year=" + year +
+                ", month=" + month +
+                ", day=" + day +
+                '}';
+    }
+
+    //判断是否为闰年
+    public boolean isLeapYear(Integer year){
+        if(year % 4 == 0 && year % 400 !=0 || year % 100 ==0){
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 修改年月日
+
+     */
+    public void set(){
+        System.out.println("请输入新日期 --年:");
+        Scanner scanner = new Scanner(System.in);
+        this.year = scanner.nextInt();
+        System.out.println("请输入新日期 --月:");
+        this.month = scanner.nextInt();
+        System.out.println("请输入新日期 --日:");
+        this.day = scanner.nextInt();
+    }
+
+    /**
+     * 加日期
+     */
+    public static MyDate puls(MyDate myDate){
+        Scanner scanner = new Scanner(System.in);
+        Integer year1 = myDate.getYear();
+        Integer month1 = myDate.getMonth();
+        Integer day1 = myDate.getDay();
+
+        System.out.println("年");
+        int year2 = scanner.nextInt();
+        System.out.println("月");
+        int month2 = scanner.nextInt();
+        System.out.println("日");
+        int day2 = scanner.nextInt();
+
+        Integer newYear = year1+year2;
+
+        Integer newMonth = month1+month2;
+
+       Integer newDay = day1+day2;
+
+       if(newDay > 30){
+           newMonth = newMonth+1;
+           newDay-=30;
+           if(newMonth>12){
+              newYear = newYear + 1;
+              newMonth  -= 12;
+           }
+       }
+
+        MyDate myDate1 = new MyDate();
+        myDate1.setYear(newYear);
+        myDate1.setMonth(newMonth);
+        myDate1.setDay(newDay);
+        return myDate1;
+    }
+
+    public static void main(String[] args) {
+        MyDate myDate = new MyDate();
+        myDate.set();
+        System.out.println(myDate);  //2022   12  01
+        MyDate date = puls(myDate);
+        System.out.println(date);
+    }
+}

+ 74 - 0
src/main/java/com/sf/day07/Person.java

@@ -0,0 +1,74 @@
+package com.sf.day07;
+
+/**
+ * 1、创建类
+ * 2、编写属性
+ * 3、添加构造器
+ * 4、getset方法
+ * 5、toString
+ */
+public class Person {
+    private Integer  No;
+    private String name;
+    private  Integer age;
+
+    public Person() {
+    }
+
+    public Person(Integer no, String name, Integer age) {
+        No = no;
+        this.name = name;
+        this.age = age;
+    }
+
+    /**
+     * this   当前对象
+     * @param no
+     *
+     * 50   46有参构造器
+     * 47  this(参数);
+     */
+
+    public Person(Integer no) {
+        this.No = no;
+    }
+
+    public Person(Integer no,String name) {
+       this(no);       //this.No = no;
+       this.name = name;
+
+    }
+
+    public Integer getNo() {
+        return No;
+    }
+
+    public void setNo(Integer no) {
+        No = no;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    @Override
+    public String toString() {
+        return "Person{" +
+                "No=" + No +
+                ", name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+}

+ 35 - 0
src/main/java/com/sf/day07/PersonTest.java

@@ -0,0 +1,35 @@
+package com.sf.day07;
+
+/**
+ * person的测试类
+ */
+public class PersonTest {
+    public static void main(String[] args) {
+
+        /**
+         * 构造器的作作用:
+         * 1、对象的创建
+         * 2、属性的赋值
+         */
+        Person person = new Person();   //创建对象     --无参构造器
+        person.setAge(20);
+        person.setName("zhangzhang");
+        person.setNo(12345);
+        System.out.println(person);
+
+
+        //方式二
+        /**
+         * 通过构造器进行属性赋值注意事项:
+         *  有参构造器进行赋值
+         *  通过有参构造器进行赋值的时候   属性的顺序不能变
+         */
+        Person person2 = new Person(53646,"小李",21);
+        System.out.println(person2);
+
+
+        Person person3 = new Person(53646);
+        System.out.println(person3);
+
+    }
+}

+ 54 - 0
src/main/java/com/sf/day07/Student.java

@@ -0,0 +1,54 @@
+package com.sf.day07;
+
+/**
+ * 学生类
+ */
+public class Student {
+
+    /**
+     * 类中:
+     *      属性
+     *      方法
+     *      构造器:
+     *          1、对象的创建     new   xxx();
+     *          2、属性的赋值
+     */
+
+    public int number;//学号
+   public int state;//年级
+   public int score;//成绩
+
+
+    /**
+     * public Student(){
+     *
+     * }
+     */
+
+
+
+
+    public Student(){  //无参构造器
+
+    }
+
+    public Student(int number, int state, int score) {
+        this.number = number;
+        this.state = state;
+        this.score = score;
+    }
+
+
+    public Student(int number){
+        this.number = number;
+    }
+
+    @Override
+    public String toString() {
+        return "Student{" +
+                "number=" + number +
+                ", state=" + state +
+                ", score=" + score +
+                '}';
+    }
+}

+ 31 - 0
src/main/java/com/sf/day07/StudentTest.java

@@ -0,0 +1,31 @@
+package com.sf.day07;
+
+public class StudentTest {
+    public static void main(String[] args) {
+        Student student1 = new Student();
+        Student[] students = new Student[5];   //动态定义数组
+        for (int i = 1;i<=5;i++){
+            students[i-1] = new Student();
+            students[i-1].number = i;
+            students[i-1].score = (int)((Math.random()*100)+1);
+            students[i-1].state = (int)((Math.random()*5)+1);
+        }
+        //打印所有学生的信息
+        
+//        for (int i =0 ;i<5;i++){
+//            System.out.println(students[i]);
+//        }
+// for增强  元素的类型  元素的变量名                  要被遍历的数组或集合     快捷键  iter+回车
+        for (Student student : students) {
+            System.out.println(student);
+        }
+
+        //打印出3年级(state值为3)的学生信息。
+        System.out.println("打印出3年级(state值为3)的学生信息。");
+        for (int i = 0;i<5;i++){
+            if(students[i].state ==3){
+                System.out.println(students[i]);
+            }
+        }
+    }
+}

+ 39 - 0
src/main/java/com/sf/day07/User.java

@@ -0,0 +1,39 @@
+package com.sf.day07;
+
+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;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "userName='" + userName + '\'' +
+                ", passWord='" + passWord + '\'' +
+                '}';
+    }
+}

+ 31 - 0
src/main/java/com/sf/day07/UserManager.java

@@ -0,0 +1,31 @@
+package com.sf.day07;
+
+import java.util.Scanner;
+
+public class UserManager {
+
+    public static void main(String[] args) {
+        User user = new User();
+        user.setUserName("admin");
+        user.setPassWord("123456");
+        boolean login = login(user.getUserName(), user.getPassWord());
+        if(login){
+            System.out.println("登录成功");
+        }else {
+            System.out.println("登录失败");
+        }
+    }
+
+    public static boolean login(String userName, String passWord){
+        Scanner scanner = new Scanner(System.in);
+        System.out.println("请输入用户名:");
+        String username = scanner.next();
+        System.out.println("请输入密码:");
+        String password = scanner.next();
+        if(username.equals(userName)  && password.equals(passWord)){
+            return true;
+        }
+        return false;
+    }
+
+}

+ 57 - 0
src/main/java/com/sf/day07/getset/Test01.java

@@ -0,0 +1,57 @@
+package com.sf.day07.getset;
+
+/**
+ * getter和setter方法
+ */
+public class Test01 {
+
+    /**
+     * 定义三个属性   name   id   age
+     */
+
+    private String name;   //张三
+    private Integer id;
+    private Integer age;
+
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        //this  代表是当前对象    属性值    =   张三
+        this.name = name;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    /**
+     *  用private修饰的属性  在其他类不能直接进行赋值    但是可以通过  getter  和  setter 方法进行赋值
+     * @return
+     */
+
+
+
+    @Override
+    public String toString() {
+        return "Test01{" +
+                "name='" + name + '\'' +
+                ", id=" + id +
+                ", age=" + age +
+                '}';
+    }
+}

+ 17 - 0
src/main/java/com/sf/day07/getset/Test02.java

@@ -0,0 +1,17 @@
+package com.sf.day07.getset;
+
+/**
+ * 对test01的测试类
+ */
+public class Test02 {
+    public static void main(String[] args) {
+        Test01 test01 = new Test01();
+        //通过setter方法进行赋值
+        test01.setName("zhangsan");
+        System.out.println(test01.getName());
+        test01.setId(1001);
+        test01.setAge(20);
+        System.out.println(test01);
+        System.out.println(test01.getId());
+    }
+}

BIN
target/classes/com/sf/day07/MyDate.class


BIN
target/classes/com/sf/day07/Person.class


BIN
target/classes/com/sf/day07/PersonTest.class


BIN
target/classes/com/sf/day07/Student.class


BIN
target/classes/com/sf/day07/StudentTest.class


BIN
target/classes/com/sf/day07/User.class


BIN
target/classes/com/sf/day07/UserManager.class


BIN
target/classes/com/sf/day07/getset/Test01.class


BIN
target/classes/com/sf/day07/getset/Test02.class