定义
类(Class)和对象(Object)是面向对象的核心概念
- 类是对一类事物的描述,是抽象的、概念上的定义
- 对象是实际存在的该类事物的每个个体,因而也称为实例(instance)
类及类的成员
- 属性
- 方法
例,构建一个人的类:
// 创建类
class Person{
// 属性
String name;
int age;
boolean isMarried;
// 方法
public void walk(){
System.out.println("walking");
}
public void display(){
return "Name : " + name + " , Age : " + age + " , Married : " + isMarried;
}
}
// 测试类
public class Test{
public static void main(String[] args){
// 创建对象
Person P = new Person();
// 调用对象的属性
P.name = "nanase";
P.age = 25;
P.isMarried = false;
// 调用对象的方法
P.walk();
P.display();
}
}
对象的内存解析
属性与局部变量
- 属性
- 具有权限修饰符
- 加载到堆
局部变量
描述类具有的功能
例,构建一个客户的类:
class Customer{
String name;
int age;
boolean isMarried;
public void eat(){
System.out.println("eating");
}
public void sleep(int hour){
System.out.println("hour + " + hour);
}
public String getName(){
return name;
}
public String getNation(String nation){
String info = "My Nation is " + nation;
return info;
}
}
方法的声明
- 权限修饰符
- private、public、protected、缺省
- 返回值类型
- 方法名
- 形参列表
练习:
定义类Student,包含三个属性:
学号number(int),年级state(int),成绩score(int)。
创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
(对象数组)
public class StudentTest {
public static void main(String[] args) {
Student[] stu = new Student[20];
for(int i = 0 ; i < 20 ; i ++ ){
stu[i] = new Student();
stu[i].id = i + 1;
stu[i].grade = (int)(Math.random() * (6 - 1 + 1) + 1);
stu[i].score = (int)(Math.random() * (100 - 0 + 1));
}
System.out.println("Q1:");
// 问题1
for(int i = 0 ; i < 20 ; i ++ ){
if(stu[i].grade == 3){
System.out.println("学号 : " + stu[i].id + " 年级 : " + stu[i].grade + " 成绩 : " + stu[i].score);
}
}
System.out.println("Q2:");
// 问题2
for(int i = 0 ; i < 19 ; i ++ ){
for(int j = 0 ; j < 20 - i - 1 ; j ++ ){
if(stu[j].score > stu[j + 1].score){
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
// 遍历学生信息
for(int i = 0 ; i < 20 ; i ++ ){
System.out.println("学号 : " + stu[i].id + " 年级 : " + stu[i].grade + " 成绩 : " + stu[i].score);
}
}
}
class Student {
int id; // 定义学号
int grade; // 定义年级
int score; // 定义成绩
}
方法的重载
- 两同一不同:同类同方法名,不同参数列表
例如:
public void println(byte x)
public void println(short x)
public void println(int x)
public void println(long x)
public void println(float x)
public void println(double x)
public void println(char x)
public void println(double x)
public void println()
可变个数的形参
- JavaSE 5.0 中提供了Varargs(variable number of arguments)机制,允许直接定义能和多个实参相匹配的形参。从而,可以用一种更简单的方式,来传递个数可变的实参。
- 只能有一个。
例如:
public class Demo{
public void show(int i){
System.out.println("int : " + i);
}
public void show(String s){
System.out.println("String : " + s);
}
public void show(String ... strs){
System.out.println("String ... strs ");
}
}
方法参数的传递机制
值传递机制
- 形参是基本数据类型:将实参基本数据类型变量的“数据值”传递给形参
- 形参是引用数据类型:将实参引用数据类型变量的“地址值”传递给形参
递归方法
- 方法递归包含了一种隐式的循环,它会重复执行某段代码,但这种重复执行无须循环控制。
- 递归一定要向已知方向递归,否则这种递归就变成了无穷递归,类似于死循环。
例如,计算1-100之间所有自然数的和:
public int sum(int n){
if(n == 1){
return 1;
}
return sum(n - 1) + n;
}
斐波那契:
例如,1 1 2 3 5 8 13 21 34 55…
当 时:
当 时:
```java import java.util.Scanner;
public class Fib { public static void main(String[] args) { Scanner input = new Scanner(System.in); Fib method = new Fib(); int n = input.nextInt(); System.out.println(“fib(“ + n + “) = “ + method.fib(n)); } public int fib(int n){ if(n <= 2){ return 1; } return fib(n - 1) + fib(n - 2); } } ```