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);
}
});
}
}