14. java面向对象 - 基础

一、面向对象主线

1. Java类及类的成员:属性、方法、构造器、代码块、内部类
2. 面向对象三大特征:封装、继承、多态、(抽象性)
3. 其他关键字:this、super、static、final、abstract、interface、package、import
class Person{
    //属性,或成员变量
    String name;
    boolean isMarried;
    //构造器
    public Person(){}
    public Person(String n, boolean im){
        name = n;
        isMarried = im;
    }
    //方法,或函数
    public void walk(){
        System.out.println("running");
    }
    public String display(){
        return "name is" + name + "Marry is" + isMarried;
    }
    //代码块
    {
        name = "Mahuateng";
        age = 10;
        isMarried = true;
    }
    //内部类
    class pet{
        String name;
        float weight;
    }
}

二、类和对象的创建

1. 创建一个文件名为PersonTest的.java文件

package china.java.demo;

public class PersonTest {
    public static void main(String[] args) {
        //创建Person类对象;
        Person p = new Person();
        p.talk("English");
    }
}


class Person {
    //属性
    String name;
    int age = 1;
    boolean isMale;
    //方法
    public void eat() {
        System.out.println("吃饭");
    }
    public void talk(String language) {
        System.out.println("此人说:" + language);
    }
}

三、类中方法的声明和使用

1. 方法定义

1. 方法声明:权限修饰符 返回值类型 方法名(形参列表){方法体}
2. 权限修饰符:private、public、缺省、protected
3. 返回值类型:如果有返回值,则必须在方法声明时,指定返回值的类型。同时,方法中需要使用return关键字来返回指定类型的变量或常量。如果没有返回值方法声明使用void,不用写return,若使用return;
4. 方法名:小驼峰命名法,类名大驼峰、常量ROUND_PIE
5. 形参列表:(数据类型1 形参1,数据类型2 形参2)

2. 返回值

1. 使用范围:方法体中
2. 作用:① 结束方法 ② 针对有返回值类型的方法,使用'return 数据'方法返回所要的数据 
3. 注意点:return 关键字后面不可以声明执行语句
1. 无返回值的 - void(就是没返回值的意思)
class Customer{
	String name;
	int age;
    public void eat(){
    	System.out.println("吃饭");
    }
    public void getAge(int age){
    	System.out.println(age);
    }
}

2. 有返回值的
class Customer{
	String name;
	int age;
    public String getName(){
    	return name;
    }
    public String getNation(String nation){
    	return nation;
    }
}

四、对象数组

public class StudentTest {
    public static void main(String[] args) {
        //声明一个Student类型的数组
        Student[] stu = new Student[20];
        for (int i = 0; i < stu.length; i++) {
            //给数组元素赋值
            stu[i] = new Student();
            //给stu的属性赋值
            stu[i].number = (i + 1);
            stu[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);
            stu[i].score = (int) (Math.random() * (100 + 1));
        }

        StudentTest myStu = new StudentTest();
        myStu.print(stu);
        myStu.searchState(stu, 3);
        myStu.sort(stu);
    }

    //遍历Student数组的操作
    public void print(Student[] stu) {
        for (int i = 0; i < stu.length; i++) {
            System.out.println(stu[i].info());
        }
    }

    //查找班级方法
    public void searchState(Student[] stu, int state) {
        for (int i = 0; i < stu.length; i++) {
            if (stu[i].state == state) {
                System.out.println(stu[i].info());
            }
        }
    }

    //根据成绩排序
    public void sort(Student[] stu) {
        for (int i = 0; i < stu.length - 1; i++) {
            for (int j = 0; j < stu.length - 1 - i; j++) {
                if (stu[j].score > stu[j + 1].score) {
                    Student temp = stu[j];
                    stu[j] = stu[j + 1];
                    stu[j + 1] = temp;
                }
            }
            System.out.println(stu[i].info());
        }

    }
}

class Student {
    int number;
    int state;
    int score;

    public String info() {
        return "学号" + number + "  " + "年级" + state + "成绩" + score;
    }
}
posted @ 2019-12-18 10:28  qi.hu  阅读(148)  评论(0编辑  收藏  举报