Java面向对象
Java编程的核心思想(OOP)
一、面向对象&面向过程
- 面向过程:线性过程、第一步做什么、第二步做什么
- 面向对象:物以类聚、分类的思维模式、处理复杂的问题,多人协作,其本质是:以类的方式组织代码,以对象的方式封装数据。
什么是面向对象
二、值传递和引用传递
package com.oop.demo01;
// 值传递
public class Demo01 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
change(10);
System.out.println(a);
}
public static void change(int a) {
a = 10;
}
}
package com.oop.demo01;
// 引用传递
public class Demo02 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
change(person);
System.out.println(person.name);
}
public static void change(Person person) { //person对象是引用类型
person.name = "Java";
}
}
class Person {
String name;
}
三、创建对象、构造器
类与对象的关系
创建与初始化对象
package com.oop.demo02;
public class Student {
// 属性
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//方法
public void study() {
System.out.println(this.age + "的学生" + this.name + "在学习。");
}
}
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
Student student = new Student("小明", 20);
student.study();
student.setName("小红");
student.age = 22;
student.study();
}
}
四、内存分析
package com.oop.demo03;
public class Pet {
String name;
int age;
public void shout() {
System.out.println(this.name + "今年" + this.age + "岁。");
}
}
package com.oop.demo03;
public class Application {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "旺财";
dog.age = 20;
dog.shout();
Pet cat = new Pet();
cat.name = "小花";
cat.age = 2;
cat.shout();
}
}
五、封装、继承、多态
5.1、封装
package com.oop.demo04;
public class Student {
// 属性私有
private String name;
private int id;
private char sex;
private int age;
//get / set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 120 || age < 0) {
this.age = 3;
} else {
this.age = age;
}
}
}
package com.oop.demo04;
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
// s1.name 私有变量,只能通过get/set方法进行访问
s1.setName("老板");
s1.setAge(23);
System.out.println(s1.getName());
System.out.println(s1.getAge());
}
}
作用
- 提高程序的安全性,保护数据。
- 隐藏代码的实现细节。
- 统一接口。
- 系统可维护性增加了。
5.2、继承
在IDEA中 Ctrl + H 查看继承关系
- object 类
所有类都默认直接或间接继承 object 类。 - super 和 this
总结:
- super调用父类的构造方法,必须在构造方法的第一个
- super必须只能出现在子类的方法或者构造方法中
- super和this不能同时调用构造方法
- super vs this
- this: 本身调用者对象
- super 代表父类对象的引用
- 前提
- this: 没有继承也能使用
- super只能在继承条件下才能使用
- 构造方法
- thist() 本类的构造
- super() 父类的构造
package com.oop.demo05;
public class Person {
public Person() {
System.out.println("Person 无参构造。");
}
private int gupiao = 20_0000_0000;
public int money = 10_0000_0000;
protected String name = "zhang";
public void say() {
System.out.println("说了一句话.");
}
public void print() {
System.out.println("Person");
}
private void print2() {
System.out.println("Person");
}
}
package com.oop.demo05;
public class Student extends Person {
public Student() {
super(); //默认调用父类无参构造
//this();
System.out.println("Student无参构造。。。");
}
private String name = "qingjiang";
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();
//super.print2(); 私有的属性和方法无法被继承。
}
}
package com.oop.demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.money);
//System.out.println(student.gupiao); 不能直接使用
//System.out.println(student.getGupiao());
student.test("Roc");
student.test1();
}
}
- 方法重写
package com.oop.demo06;
public class B {
public void test() {
System.out.println("B-- test");
}
}
package com.oop.demo06;
public class A extends B {
//重写
@Override //注解:有功能的注释
public void test() {
System.out.println("A-test");
}
}
package com.oop.demo06;
public class Application {
public static void main(String[] args) {
//静态方法方法的调用之和左边定义的数据类型有关
// 非静态的方法才能够重写
// private 方法不能重写
A a = new A();
a.test();
B b = new A();
b.test();
}
}
总结: 重写需要有继承关系,子类重写父类的方法
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大但不能缩小 public > protected > default > private
- 抛出异常: 范围可以被缩小但不能扩大
- 重写: 重写子类方法和父类方法 方法名相同、方法体不同
- 为什么需要重写:父类的功能,子类不一定需要或者不一定满足!
5.3、多态
package com.oop.demo07;
public class Person {
public void run() {
System.out.println("run");
}
}
package com.oop.demo07;
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
public void eat() {
System.out.println("eat");
}
}
package com.oop.demo07;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
// new Person();
// new Student();
// 可以指向的引用类型就不确定了,可以是他的父类型
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
s1.run();
s2.run(); // 子类重写父类方法后,执行子类的方法
s1.eat();
//对象能执行那边的方法,主要看对象左边的类型,和右边关系不大。
//s2.eat();
((Student) s2).eat(); // 强制类型转换,高转低
}
}
注意:
a. 多态是方法的多态,属性没有多态
b. 父类和子类的多态, 有联系, 不能是猫和狗的多态,只能是猫狗和动物的多态
c. 类型转换异常: ClassCastException
d. 多态存在的条件:继承的关系,方法需要重写, 父类引用指向子类对象 Father f1 = new Son();
e. 那些方法不能重写:
- static 方法, 属于类,不属于对象
- final 常量
- private方法
六、instanceof类型转换
七、代码块
- 匿名代码块:一般用于赋初始值
- 静态代码块:静态代码块(只执行一次,和类一起加载)
- 构造方法:初始化对象
- 执行顺序: 静态代码块 匿名代码块 构造方法
package com.oop.demo08;
public class Person {
{
// 匿名代码块: 一般用于赋初始值
System.out.println("匿名代码块");
}
static {
// 静态代码块(只执行一次,和类一起加载)
System.out.println("静态代码块");
}
public Person() {
System.out.println("构造方法");
}
}
package com.oop.demo08;
public class Application {
public static void main(String[] args) {
Person person = new Person();
System.out.println("================");
Person person2 = new Person();
}
}
八、抽象类
package com.oop.demo09;
// abstract 抽象类
// extends: 只能单继承(多继承通过接口实现)
public abstract class Action {
//约束 有人帮我们实现
// 抽象方法: 只有方法名字,没有方法的实现
public abstract void doSomething();
//1. 不能new抽象类,只能靠子类去实现它
//2. 抽象类里面可以写普通方法,但是抽象方法必须在抽象类中
}
package com.oop.demo09;
// 抽象类的所有方法,继承了它的子类,都必须要实现它的方法
// 除非:子类也是一个抽象类
public class A extends Action {
@Override
public void doSomething() {
}
}
package com.oop.demo09;
public class APP {
public static void main(String[] args) {
// Action action = new Action();
}
}
思考:
- 抽象类不能new,但是它存在构造方法吗?
java抽象类的构造方法和普通类的构造方法一样,都是用来初始化类。只是抽象类的构造方法不能直接调用,因为抽象类不能实现实例,但是一旦一个普通类继承了抽象类,便也可以在构造函数中调用其抽象类的构造函数,也可以使用其方法。 抽象类就是一个不能实例化的不同类,不过如果方法加了abstract那么就必须在子类里面重写了 - 存在的意义?
将公共模块抽象出来,提高开发的效率
九、接口
接口只有规范,自己无法写方法,~专业的约束!约束和实现分离:面向接口编程。接口的本质是契约,接口是OOP的精髓,是对象的抽象。作用:
- 约束
- 定义一些方法,让不同的人实现
- 方法都是public static
- 常量都是public static final
- 接口不能被实例化,没有构造方法
- 接口的实现,用implements,实现接口需要重写接口的方法
package com.oop.demo10;
public interface UserService {
//public abstract void run();
// void go();
// public abstract可以省略
// public static final int age = 30;
void add(String name);
void delete(String name);
void update(String name);
void select(String name);
}
package com.oop.demo10;
public interface TimeService {
void timer();
}
package com.oop.demo10;
// 实现了接口的类,必须重写接口中的方法
// 利用接口实现多继承
public class UserServiceImpl implements UserService, TimeService {
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void select(String name) {
}
@Override
public void timer() {
}
}
十、内部类
package com.oop.demo11;
public class Outer {
private int id = 10;
public void out() {
System.out.println("外部类方法");
}
// public static class Inner{} 静态内部类
public class Inner {
public void in() {
System.out.println("这是内部类的方法");
}
// 内部类可以获得内部类的私有属性
public void getID() {
System.out.println(id);
}
}
// 一个java只有一个public class, 但可以有多个class类
class A {
}
// 局部内部类,写在方法里面
public void method() {
class Inner {
public void in() {
}
}
}
}
class B{
}
package com.oop.demo11;
import com.oop.demo11.UserService;
public class Test {
public static void main(String[] args) {
new Apple().eat();//匿名内部类,不用将实例保存在变量中
new UserService() {
@Override
public void hello() {
}
};
}
}
class Apple {
public void eat() {
System.out.println("1");
}
}
interface UserService {
void hello();
}
十一、异常机制
什么是异常:
简单分类:
参考:https://www.cnblogs.com/achievement-active/p/9304293.html
- 检查性异常:用户错误或问题引起的问题,测试解决;
- 运行时异常:运行时异常实可能被程序员避免的异常;
- 错误:错误不是异常,而是脱离程序员控制的问题。
处理异常五个关键字:try、 catch、 finally、 throw、 throws
Error:
Exception:
package com.exception;
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 0;
// 要捕获多个异常要从小到大
// try-catch的快捷键 ctr + alt + t
try {
System.out.println(a / b);
new Test().a();
} catch (ArithmeticException e) {
System.out.println("程序出现异常,除数不能为0");
} catch (Error e) {
System.out.println("Err,栈溢出");
} catch (Throwable te) {
System.out.println("Throwable");
} finally {
System.out.println("finally");
}
try {
new Test().test(1, 0);
} catch (ArithmeticException e) {
e.printStackTrace();
} finally {
}
}
public void a() {
b();
}
public void b() {
a();
}
// 假设这个方法处理不了这个异常,可以在方法上抛出异常
public void test(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException(); //主动抛出异常
}
}
}
自定义异常:
实际应用中的总结: