hooyantsing's Blog

第20次课程

字数统计: 1.1k阅读时长: 6 min
2019/05/28

源辰74班

第20次课程

05.28.2019

**内容    **

OOP

1.对象数组

Class Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.yc.bean;
public class Student {
    private int sID;
    private String sName;
    private int age;
    public Student(int sID, String sName,  int age) {
       super();
       this.sID = sID;
       this.sName = sName;
       this.age = age;
    }
    public int getsID() {
       return sID;
    }
    public void setsID(int sID) {
       this.sID = sID;
    }
    public String getsName() {
       return sName;
    }
    public void setsName(String sName) {
       this.sName = sName;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
    
    
    public String getInfo() {
       return ("学号:"+this.sID+"\t姓名:"+this.sName+"\t年龄:"+this.age);
    }
}

Main Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.yc.bean;
import java.util.Random;
import java.util.Scanner;
public class Test {
    private static final int MAX = 5;
    private static Random r = new Random();
    private static Scanner sc = new  Scanner(System.in);
    public static void main(String[] args) {
       Student[] students = new  Student[MAX];
       
       //Init
       for(int i=0;i<students.length;i++) {
           students[i] = new  Student(i+1,"Hooy"+i,r.nextInt(6)+18);
       }
       //Show
       show(students);
       //Sort
       sortAge(students);
       //Show
       System.out.println("年龄排序:");
       show(students);
       
       //Sort
       sortName(students);
       //Show
       System.out.println("名字排序:");
       show(students);
       
       //Find
       System.out.println("请输入要查询学生名:");
       String toFind = sc.nextLine();
       
       int index =  binarySearch(students,toFind);
       System.out.println(index==-1?"查无此人":"此人信息:"+students[index].getInfo());
    }
    //二分查找法
    private static int  binarySearch(Student[] students,String  toFind) {
       int low = 0;
       int high = students.length;
       int mid = 0;
       while(low<=high) {
           mid = (low+high)/2;
           if(students[mid].getsName().equals(toFind))  {
               return mid;
           }else  if(students[mid].getsName().compareTo(toFind)<0) {
               low = mid + 1;
           }else {
               high = mid - 1;
           }
       }
       return -1;
    }
    private static void sortName(Student[]  students) {
       if(students==null ||  students.length==0) {
           System.out.println("数组为空,无法排序");
           return;
       }
       for(int i=0;i<students.length-1;i++)  {
           for(int  j=0;j<students.length-i-1;j++) {
               //(名字j) - (名字j+1) < 0
               if(students[j].getsName().compareTo(students[j+1].getsName())<0) {
                  Student temp =  students[j];
                  students[j] =  students[j+1];
                  students[j+1] = temp;
               }
           }
       }   
    }
    
    private static void sortAge(Student[]  students) {
       if(students==null ||  students.length==0) {
           System.out.println("数组为空,无法排序");
           return;
       }
       for(int i=0;i<students.length-1;i++)  {
           for(int  j=0;j<students.length-i-1;j++) {
               if(students[j].getAge()>students[j+1].getAge()) {
                  Student temp =  students[j];
                  students[j] =  students[j+1];
                  students[j+1] = temp;
               }
           }
       }   
    }
    private static void show(Student[]  students) {
       if(students==null || students.length==0) {
           System.out.println("数组为空,无法输出");
           return;
       }
       for(Student arr:students) {
           System.out.println(arr.getInfo());
       }
    }
}

2.继承 extends

通俗讲:子类是父类功能的扩展。子类继承父类非私有成员和类。

注意关键字:extends(继承) protected(受保护的)

Class Person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.yc.bean;
public class Person {
    //在同包内都可访问,父子类可访问
    protected String sName;
    protected int sAge;
    public Person() {
       super();
    }
    public String getsName() {
       return sName;
    }
    public void setsName(String sName) {
       this.sName = sName;
    }
    public int getsAge() {
       return sAge;
    }
    public void setsAge(int sAge) {
       this.sAge = sAge;
    }
}

Class Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.yc.bean;
public class Student extends Person{
    private int sID;
    //private String sName;
    //private int sAge;
    public Student(int sID, String sName,  int sAge) {
       super();
       this.sID = sID;
       this.sName = sName;
       this.sAge = sAge;
    }
    public int getsID() {
       return sID;
    }
    public void setsID(int sID) {
       this.sID = sID;
    }
    /*public String getsName() {
       return sName;
    }
    public void setsName(String sName) {
       this.sName = sName;
    }
    public int getsAge() {
       return sAge;
    }
    public void setsAge(int sAge) {
       this.sAge = sAge;
    }*/
    
    
    public String getInfo() {
       //子类内寻找变量
       /*return ("学号:"+this.sID+"\t姓名:"+this.sName+"\t年龄:"+this.sAge);*/
       //父类内寻找变量
       /*return ("学号:"+this.sID+"\t姓名:"+super.sName+"\t年龄:"+super.sAge);*/
       //调用父类方法
       return ("学号:"+this.sID+"\t姓名:"+this.getsName()+"\t年龄:"+this.getsAge());
       //父类变量为:protected
       /*return ("学号:"+sID+"\t姓名:"+sName+"\t年龄:"+sAge);*/
    }
}

Class Teacher

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.yc.bean;
                  //继承Person
public class Teacher extends Person{
    private double salary;
    public Teacher() {
       super();
    }
    public Teacher(double salary) {
       super();
       this.salary = salary;
    }
    public double getSalary() {
       return salary;
    }
    public void setSalary(double salary) {
       this.salary = salary;
    }
}

3.四种权限

**6c71d0174954fcc2a8346550b4949b68.png

**

访问限制由高到低:private(私有)>缺省>protected(受保护)>public(公有)

4.上一级 super();

//也称父级

Class Father

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.yc.bean;
public class Father {
    private String name;
    public Father() {
       System.out.println("Father类无参构造器");
    }
    
    public Father(String name) {
       super();          //实例父级Object类
       this.name = name;
       System.out.println("Father类有参构造器");
    }
}

Class Son

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.yc.bean;
public class Son extends Father{
    private int age;
    public Son() {
       super("张三");
       System.out.println("Son类无参构造器");
    }
    public Son(String name,int age) {
       super(name);          //实例父级Father类
       this.age = age;
       System.out.println("Son类有参构造器");
    }
}

Main TestSuper

1
2
3
4
5
6
package com.yc.bean;
public class TestSuper {
    public static void main(String[] args) {
       new Son("smith",40);
    }
}

贴士:

**super()调用上一级构造器,super.**调用上一级方法。

CATALOG