1、OOP的三大特征:
封装:信息隐藏
将一个事物的特征、行为定义成为一个独立的结构,这个结构就是类(class)
你会做表吗?
类和表是一样滴:
public class Student {
private int sid;
private String name;
private char sex;
private String phone;
private String address;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public static void main(String[] args) {
Student stu1 = new Student();
//通过setter方法给属性赋值
stu1.setName("张三");
stu1.setSid(1);
stu1.setSex('男');
stu1.setPhone("18673363711");
stu1.setAddress("湖南");
//通过构造方法给属性赋值
Student stu2 =
new Student(2, "曾继墙", '男', "110", "广东");
}
}
构造方法的作用:初始化对象,给对象的属性赋值
继承:代码重用
关键字:extends
/**
* 班长类,班长首先得是个学生,然后才有自己独有的特性和行为
* @author Administrator
*/
public class Monitor extends Student {
private String type; //正的带是副的
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public Monitor() {
// TODO Auto-generated constructor stub
}
public Monitor(int sid, String name, char sex,
String phone,
String address, String type) {
//先调用父类构造方法完成前5个属性的初始化
super(sid, name, sex, phone, address);
//初始化自己的成员
this.type = type;
}
/**
* 管理班级
*/
public void manage(){
System.out.println(this.getName() + "正在管理班级");
}
public static void main(String[] args) {
Monitor m = new Monitor();
m.setSid(3);
m.setName(“孙悟空");
m.setSex('妖');
m.setPhone("120");
m.setAddress("火星");
m.setType("正班长");
m.study();
m.manage();
Monitor m2 = new Monitor(4, "猪八戒", '男',
"119", "株洲", "副班长");
m2.study();
m2.manage();
}
}
多态:增强扩展性
方法重写的语法规则
方法名、参数列表、返回值都必须相同
作用域不能变小(父类方法是protected,子类重写后可以是protected和public)
抛出的异常不能变大(父类方法抛出NullPointerException,子类不能抛出Exception异常,因为变大了)
protected void m(int a, int b) throws NullPointerException{
}
方法重写有什么作用?改变父类方法的行为
Student类中的study()方法
public void study(){
System.out.println(this.name + "正在学习");
}
Monitor类的重写的study()方法
@Override
public void study() {
System.out.println(this.getName() + "正在吃喝玩乐");
}
2、修饰符
final 不变的
修饰变量,变量值不能改变
修饰成员方法,方法不能被重写
修饰类,类不能被继承
public class A {
/**
* 矛盾1:a、b、c是局部变量,不能被其他类访问
* 矛盾2:B类定义在方法m中,应该可以访问方法中的变量
* 折中处理:在B类中可以访问方法m中的局部变量,但是不能改变他们的值
* @param a
* @param b
*/
public void m(final int a, final int b) {
final int c = 10;
class B{
public void x(){
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
}
}
static 静态的
修饰成员变量,变量值被所有对象共享
修饰方法,方法可以直接用类名调用
修饰内部类,内部类可以直接通过外部类访问
修饰块,静态块随类的加载而调用
public class Account {
private static double balance;// 余额
private String name;
static{
System.out.println("静态块");
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void display(){
System.out.println(this.getName() + "账户上的余额为:"
+ this.getBalance());
}
public static void main(String[] args) {
Account acc1 = new Account();
acc1.setName(“佳佳");
acc1.setBalance(10000);
Account acc2 = new Account();
acc2.setName("芬兰");
acc2.setBalance(1000000);
acc1.display();
acc2.display();
}
}
常量:
public static final double PI = 3.14159265358979323846;
final保证PI的值不能变化
static保证调用方便,Math.PI
枚举:列出所有可能的值 enum
public enum Week {
星期一,
星期二,
星期三,
星期四,
星期五,
星期六,
星期日
}
使用:Week w = Week.星期六;
3、抽象类和接口
抽象类(abstract):
抽象类中可以有方法不实现只定义(抽象方法)
public abstract class A{
public abstract void m();
}
案例:
public abstract class Shape {
public abstract double area();
public abstract double length();
public void show(){
System.out.println("周长:" + length());
System.out.println("面积:" + area());
}
}
public class Rectangle extends Shape {
private double a;
private double b;
public Rectangle(double a, double b) {
super();
this.a = a;
this.b = b;
}
@Override
public double area() {
return a * b;
}
@Override
public double length() {
return 2 * (a + b);
}
}
public class Square extends Rectangle {
public Square(double a) {
super(a, a);
}
public static void main(String[] args) {
Shape shape = new Square(10);
shape.show();
}
}
接口(interface):
项目中,接口主要用于异步处理
什么是异步处理?
一件事情结束后,才能做第二件事情,这就是异步处理
在开发手机App的时候,每一个操作都需要从网络服务器上读取数据,只有将数据读取到手机端,才能进行显示,这中间有一个等待过程,为了迎合这个流程,可以使用接口来解决。
案例:
public interface Inf {
void done(int a);
}
public class First {
public void x(Inf inf){
//从网络上获取数据,可能需要花费一定的时间
int k = 10;
inf.done(k);
}
}
public class Second {
public static void main(String[] args) {
First first = new First();
first.x(new Inf() {
@Override
public void done(int a) {
System.out.println("做完了");
System.out.println(a);
}
});
}
}