面向对象编程(OOP,Object-Oriented Programming)
首先思考解决问题需要那些分类,然后对这些分类进行单独思考,最后对分类下的细节进行面向过程思索。
以类的方式组织代码,以对象的组织(封装)数据。
三大特性:封装,继承,多态
方法的定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.oop;
public class Demo01 { public static void main(String[] args) {
}
public String sayHello(){ return "Hello, World"; } public int max(int a, int b){ return a > b ? a : b; } }
|
方法调用
1 2 3 4 5 6 7 8 9 10 11 12
| package com.oop;
public class Student { public void say(){ System.out.println("学生说话了"); } static public void sayHello(){ System.out.println("Hello"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.oop;
public class Demo03 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); Demo03.change(person); System.out.println(person.name); }
public static void change(Person person){ person.name = "HUI"; } }
class Person{ String name; }
|
引用传递
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.oop;
public class Demo03 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); Demo03.change(person); System.out.println(person.name); }
public static void change(Person person){ person.name = "HUI"; } }
class Person{ String name; }
|
类和对象的创建
1 2 3 4 5 6 7 8 9 10 11 12
| package com.oop.demo04;
public class Student { String name; int age;
public void studt(){ System.out.println("学习"); } }
|
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
| package com.oop.demo04; import com.oop.demo04.Student;
public class Application { public static void main(String[] args) { Student xiaoming = new Student(); Student xiaohong = new Student();
xiaoming.name = "小明"; xiaoming.age = 3;
System.out.println(xiaoming.name); System.out.println(xiaoming.age);
xiaohong.name = "小红"; xiaohong.age = 6;
System.out.println(xiaohong.name); System.out.println(xiaohong.age); } }
|
构造器详解(必须掌握)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.oop.demo04;
public class Person {
String name; public Person(){ this.name = "HUI"; }
public Person(String name){ this.name = name; }
}
|
封装
程序设计要求”高内聚,低耦合“。
- 高内聚:即使类的内部数据操作细节自己完成,不允许外部干涉。
- 低耦合:仅暴漏少量的方法给外部使用。
属性私有,get / set
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
| package com.oop.demo05;
public class Student { private String name; private int id; private char sex; private int age; public String getName(){ return this.name; } public void setName(String name){ this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { if (age <= 120 && age > 0){ this.age = age; } else { this.age = 3; }
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.oop.demo05;
public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.setName("HUI"); System.out.println(s1.getName()); s1.setAge(70); System.out.println(s1.getAge()); s1.setAge(-1); System.out.println(s1.getAge()); } }
|
继承
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
| package com.oop.demo06;
public class Person {
private int money = 100_000_000;
public void say(){ System.out.println("说话"); }
public int getMoney() { return money; }
public void setMoney(int money) { this.money = money; } }
|
1 2 3 4 5
| package com.oop.demo06;
public class Student extends Person{ }
|
1 2 3 4
| package com.oop.demo06;
public class Teacher extends Person{ }
|
1 2 3 4 5 6 7 8 9
| package com.oop.demo06;
public class Application { public static void main(String[] args) { Student student = new Student(); student.say(); System.out.println(student.getMoney()); } }
|
super - this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.oop.demo06;
public class Person { public Person() { super(); System.out.println("Person 无参执行了"); }
protected String name = "HUI";
public void print(){ System.out.println("Person"); } private void print1(){ System.out.println("private"); } }
|
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
| package com.oop.demo06;
public class Student extends Person{ public Student() { System.out.println("Student无参执行了"); }
private String name = "Tong";
public void test(String name){ System.out.println(name); System.out.println(this.name); System.out.println(super.name); }
public void print(){ System.out.println("Student"); }
public void test1(){ print(); this.print(); super.print(); } }
|
1 2 3 4 5 6 7 8 9
| package com.oop.demo06;
public class Application { public static void main(String[] args) { Student student = new Student(); student.test("FENG"); student.test1(); } }
|
方法重写(重点)–> 多态
1 2 3 4 5 6 7
| package com.oop.demo07;
public class B { public void test(){ System.out.println("B=>test()"); } }
|
1 2 3 4 5 6 7 8 9
| package com.oop.demo07;
public class A extends B{ @Override public void test() { System.out.println("A=>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
| package com.oop.demo07;
public class Application { public static void main(String[] args) { A a = new A(); a.test(); B b = new A(); b.test(); } }
|
多态
动态编译:类型:可扩展
1 2 3 4 5 6 7
| package com.oop.demo08;
public class Person { public void run(){ System.out.println("run"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.oop.demo08;
public class Student extends Person{ @Override public void run() { System.out.println("son"); }
public void eat(){ System.out.println("eat"); } }
|
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
| package com.oop.demo08;
public class Application { public static void main(String[] args) {
Student s1 = new Student(); Person s2 = new Student(); Object s3 = new Student();
s2.run(); s1.run();
s1.eat(); } }
|
instanceof 和 类型转换
instanceof 判断两个类之间是否是父子关系
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
| public class Application { public static void main(String[] args) {
Object object = new Student();
System.out.println(object instanceof Student); System.out.println(object instanceof Person); System.out.println(object instanceof Object); System.out.println(object instanceof Teacher); System.out.println(object instanceof String); System.out.println("===================="); Person person = new Student(); System.out.println(person instanceof Student); System.out.println(person instanceof Person); System.out.println(person instanceof Object); System.out.println(person instanceof Teacher); System.out.println("===================="); Student student = new Student(); System.out.println(student instanceof Student); System.out.println(student instanceof Person); System.out.println(student instanceof Object); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.oop.demo08;
public class Application { public static void main(String[] args) { Person obj = new Student();
Student student = (Student) obj; student.go(); } }
|
static关键字
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
| package com.oop.demo09;
public class Person {
{ System.out.println("匿名代码块"); } static { System.out.println("静态代码块"); } public Person(){ System.out.println("构造方法"); }
public static void main(String[] args) { Person person1 = new Person(); System.out.println("===================="); Person person2 = new Person(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.oop.demo09;
public class Student { private static int age; private double score;
public void run(){}
public static void go(){}
public static void main(String[] args) { Student s1 = new Student(); System.out.println(Student.age); System.out.println(s1.age); System.out.println(s1.score);
go(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package com.oop.demo09;
import static java.lang.Math.random; import static java.lang.Math.PI;
public class Test { public static void main(String[] args) { System.out.println(random()); System.out.println(PI); } }
|
抽象类
abstract 修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么该类就是抽象类。
1 2 3 4 5
| package com.oop.demo10;
public class A { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.oop.demo10;
public abstract class Action { public abstract void doSomething();
}
|
1 2 3 4 5 6 7
| package com.oop.demo10;
public class Application { public static void main(String[] args) { } }
|
接口的定义与实现
- 普通类:只有具体的实现
- 抽象类:具体实现和规范(抽象方法)都有!
- 接口:只有规划!自己无法写方法
专业的约束!约束的实现分离:面向接口编程
OO的精髓,是对对象的抽象,最能体现这一点的就是接口。
声明类的关键字是class,声明接口的关键字是interface
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
| package com.oop.demo11;
public interface UserService {
int AGE = 99;
void run(); void add(String name); void delete(String name); void update(String name); void query(String name); }
|
1 2 3 4 5
| package com.oop.demo11;
public interface TimeService { void timer(); }
|
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
| package com.oop.demo11;
public class UserServiceImpl implements UserService, TimeService{
@Override public void run() {}
@Override public void add(String name) {}
@Override public void delete(String name) {}
@Override public void update(String name) {}
@Override public void query(String name) {}
@Override public void timer() {} }
|
内部类
内部类就是在类的内部在定义一个类,比如,A类中定义一个B类,B类相对A类来说就是内部类,A类相对B类就是外部类。