1 面向对象
1.1 面向过程&面向对象
- 属性➕方法就是一个类
1.2 什么是面向对象
- 以类的方式组织代码,以对象的方式封装数据
- 抽象(抽像):把像的部分抽取出来
- 继承:子类继承父类的全部
- 多态:不同的人学习能力不同(执行同一种方法的结果不同)
- 对象:人;类:教师(一部分人的共同特征是老师)
2 回顾方法及加深
2.1 方法的定义
- break:跳出switch语句;结束循环
- 写完return该方法已经结束,不能继续写
- 方法名:注意规范,见名知意
- 参数列表:(参数类型,参数名)或(…) 可变长参数
package com.oop.demo01;
import java.io.IOException;
//Demo01 类
public class Demo01 {
//main 方法
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(...){
//方法体
return 返回值;
}
*/
public String sayHello(){
return "hello, world";
}
//return 结束方法,返回一个结果
public void hello(){
return; //返回一个空值 return也可以不写
}
public void print(){
return;
}
public int max(int a,int b){
return a>b ? a : b; //三元运算符
}
//数组下标越界:Arrayindexoutofbounds
public void readFile(String file) throws IOException{
}
}
2.2 方法的调用
2.2.1 静态方法和非静态方法
package com.oop.demo01;
public class Demo02 {
public static void main(String[] args) {
//静态方法 static
//Student类定义say方法加static可调用
//不加static,这边报错,无法调用
Student.say();
//非静态方法
//实例化这个类 new
//对象类型 对象名 = 对象值
Student student = new Student();
student.say(); //实例化后即可调用
}
//static是和类(Demo02)一起加载的,a已经存在
public static void a(){
b(); //b()报错 存在的无法调不存在的
}
//b在类实例化(new)之后才存在
public void b(){
}
}
package com.oop.demo01;
//学生类
public class Student {
//非静态方法
public void say(){
System.out.println("学生说话了");
}
}
2.2.2 形参和实参
package com.oop.demo01;
public class Demo03 {
public static void main(String[] args) {
//实际参数和形式参数的类型要一致
//调用add
//方法1
new Demo03().add(1,2); //实参 //alt+enter 填充完整
//方法2
Demo03.add(1,2); //变为静态方法 public static int add(int a,int b){}
}
//形参
public int add(int a,int b){
return a+b;
}
}
2.2.3 值传递和引用传递
- 值传递
package com.oop.demo01;
//值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a); //1
Demo04.change(a);
//如果void改为int,return a,打印Demo04.change(a)就是10
System.out.println(Demo04.change(a)); //10
System.out.println(a); //1 改的是形参的值不是实参的
}
//返回值为空
public static void change(int a){
a = 10;
}
}
- 引用传递
package com.oop.demo01;
//引用传递:传递一个对象,本质还是值传递
//对象,内存!
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name); //null
Demo05.change(person);
System.out.println(person.name); //Ximena
}
public static void change(Person person){
//person是一个对象,指向的是类Person person = new Person();这是一个具体的人,可以改变属性
person.name = "Ximena";
}
}
//1个类里可以有多个class,但只能有一个public class
//定义了一个person类,有一个属性:name
class Person{
String name; //默认值为null
}
3 类与对象的创建
3.1 类与对象的关系
3.2 创建与初始化对象
package com.oop.demo02;
//学生类
public class Student {
//属性
String name; //null
int age; //0
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
package com.oop.demo02;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
//类:抽象的,需要实例化
//类实例化后会返回一个自己的对象
//student对象就是Student类的一个具体实例
Student xiaoming = new Student();
Student xh = new Student();
xiaoming.name = "小明";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
xh.name = "小红";
xh.age = 3;
System.out.println(xh.name);
System.out.println(xh.age);
}
}
3.3 构造器详解
package com.oop.demo02;
//java ---> class文件
public class Person {
//一个类即使什么都不写,它也会存在一个方法
//显示的定义构造器
String name;
int age;
//1.使用new关键字,本质是在调用构造器
//2.用来初始化值
//无参构造器 (默认不显示)
public Person(){
}
//有参构造:一旦定义了有参构造,无参就必须显示定义 (重载)
public Person(String name){
//this.name代表上面的String name(这个类的name) 后面的 =name 是构造器的String name传入的name
this.name = name;
}
//command+n 选择constructor ---> OK ---> OK 默认生成有参
//command+n 选择constructor ---> OK ---> Select None 生成无参
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
/*
public static void main(String[] args) {
//new 实例化一个对象
Person person = new Person("Ximena",22);
System.out.println(person.name); //Ximena
}
构造器:
1.和类名相同
2.没有返回值
作用:
1.new本质在调用构造器
2.初始化对象的值
注意点:
1.定义了有参构造后,如果想使用无参构造,显示的定义一个无参的构造
2.this.代表当前类的,后面=的值是参数传进来的值
*/
3.4 创建对象内存分析
- 方法区是堆的一部分,随着代码运行类被分别加载出来
- 开始运行,栈层层叠加,存放方法和引用变量名
- 不同的引用变量名指向堆中具体的对象,不同对象存放在不同的地址,赋不同的值
- static在一开始和类一起加载,因此所有对象都可以调静态方法区中的方法
package com.oop.demo02;
import com.oop.demo03.Pet;
//一个项目应该只存在一个main方法
public class Application {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "旺财";
dog.age = 3;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
Pet cat = new Pet();
}
}
package com.oop.demo03;
public class Pet {
public String name;
public int age;
//无参构造,默认有,不显示
public void shout(){
System.out.println("叫了一声");
}
}
3.5 简单小结类与对象
/*
1.类与对象
类是一个模版(抽象),对象是一个具体的实例
2.方法
定义、调用
3.对象的引用
引用类型:除了8个基本数据类型都是引用类型
对象是通过引用来操作的:栈 ---> 堆(地址)
4.对象的属性:字段field or 成员变量
默认初始化:
数字:0 0.0
char: u0000
boolean:false
引用:null
修饰符 属性类型 属性名 = 属性值
5.方法(类只包含属性和方法)
6.对象的创建和使用
- 必须使用new关键字创造对象,必须有构造器 Person ximena = new Person();
- 对象的属性 ximena.name
- 对象的方法 ximena.sleep()
7.类:
静态的属性 属性
动态的行为 方法
*/
4 封装
package com.oop.demo04;
//类 private私有
public class Student {
//属性私有 在别的类下无法赋值
private String name;//名字
private int id;//学号
private char sex;//性别
private int age;//年龄
//提供一些可以操作这个属性的方法
//提供一些public的get、set方法
//get 获得这个数据
public String getName(){
return this.name;
}
//set 给这个数据设置值
public void setName(String name){
this.name = name;
}
//快捷键自动生成get、set
// command+n ---> getter and setter ---> 选中属性 ---> OK
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;
import com.oop.demo04.Student;
/*
封装的意义
1. 提高程序的安全性,保护数据
2. 隐藏代码的实现细节
3. 统一接口
4. 系统的可维护性增加
*/
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
String name = s1.getName();
s1.setName("Ximena");
//println就存在重载:相同的方法名,可以输出各种类型的数据
System.out.println(s1.getName());//s1.getName().sout回车 快捷方式
s1.setAge(999); //不合法的
System.out.println(s1.getAge());
}
}
5 继承
- 一个子类只能有一个父类,一个父类可以有多个子类
5.1 object类
package com.oop.demo05;
//在Java中,所有的类都默认直接或者间接继承object类
//Person 人:父类
public class Person /*extends object*/ {
//优先级
//public 子类才能继承父类
//protected
//default 什么都不写,默认
//private
private int money = 10_0000_0000;
public void say(){
System.out.println("说了一句话");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
package com.oop.demo05;
//学生 is 人:派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person{
//英文状态下control+H 快捷键 打开继承树
}
/*
public static void main(String[] args) {
Student student = new Student();
student.say(); //说了一句话
System.out.println(student.money); //1000000000 //public int money可输出,private报错
}
*/
- 英文状态下control+H 快捷键 打开继承树
5.2 Super
- super和this一个代表父,一个代表当前的
- command+F 在源码中搜索关键字 e.g. 在String中搜索this
package com.oop.demo05;
//在Java中,所有的类都默认直接或者间接继承object类
//Person 人:父类
public class Person /*extends object*/ {
//定义一个无参构造器 command+n
//定义了有参,无参就没了,子类默认调用父类无参会报错,因此必须有无参,因此只要写了有参构造,立马把无参加上(除非子类调用父类的有参,要写出来,不写默认调用无参)
public Person() {
System.out.println("Person无参构造执行了");
}
protected String name = "Ximena";
//private无法在子类被继承
public void print(){
System.out.println("Person");
}
}
package com.oop.demo05;
//学生 is 人:派生类,子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person{
//定义一个无参构造器 command+n
public Student() {
//隐藏代码:调用了父类的无参构造
super();//调用父类的构造器,必须在子类构造器的第一行
//this("hello"); //调用子类构造器 报错 也要求放在第一行 因此子类和父类构造器只能调用一个;若删除super(),使super()变回隐藏形式,则不报错,super()、子类有参和子类无参都运行
System.out.println("Student无参构造执行了");
}
//定义一个有参构造器
public Student(String name) {
this.name = name;
}
//属性
private String name = "Joy";
//方法
public void print(){
System.out.println("Student");
}
//方法
public void test1(){
print(); //Student
this.print(); //Student
super.print(); //Person
}
//方法
public void test(String name){
System.out.println(name); //Xiang
System.out.println(this.name); //Joy
System.out.println(super.name); //Ximena
}
}
package com.oop;
import com.oop.demo02.Person;
import com.oop.demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student(); //Person无参构造执行了 Student无参构造执行了
student.test("Xiang"); //Xiang Joy Ximena
student.test1(); //Student Student Person
}
}
super注意点:
1.super调用父类的构造方法(构造器),必须在构造方法的第一个
2.super必须只能出现在子类的方法或构造方法中
3.super和this不能同时调用构造方法
super VS this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的引用
前提:
this:没有继承也可以使用
super:只有在继承条件下才可以使用
构造方法:
this();本类的构造
super();父类的构造
5.3 方法重写
package com.oop.demo05;
//重写都是方法的重写,和属性无关
public class B {
public void test(){
System.out.println("B=>test()");
}
}
package com.oop.demo05;
//A继承B
public class A extends B{
//重写方法快捷键 command+n --> Override Methods --> 选择要重写的方法 --> OK
@Override //注解:有功能的注释
public void test() {
System.out.println("A=>test()");
}
}
package com.oop;
import com.oop.demo05.A;
import com.oop.demo05.B;
public class Application {
//静态方法和非静态方法区别很大
//静态方法:方法的调用只和左边有关,即定义的数据类型有关
//非静态方法:重写
//静态方法static结果: A=>test() B=>test()
//方法重写结果: A=>test() A=>test()
public static void main(String[] args) {
A a = new A();//子类重写了父类的方法
a.test(); //A
//父类的引用指向了子类
B b = new A();
b.test(); //B
}
}
6 多态
6.1 什么是多态
- 多态为了提供统一的接口
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.必须是父类和子类,有联系 否则类型转换异常 ClassCastException
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son();
哪些方法不能被重写?
1. static 静态方法,属于类,它不属于任何一个实例
2. final 常量
3. private 无法被继承,更谈不上重写
*/
package com.oop.demo06;
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
package com.oop;
import com.oop.demo06.Person;
import com.oop.demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型不确定:父类的引用指向子类 把子类对象赋值给父类类型的对象
//Student 能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person 父类型 可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student(); //Object是所有类的祖宗类
s2.run();//son 子类重写了父类的方法,执行子类的方法
s1.run();//son
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
//s2.eat; 报错
((Student) s2).eat(); //强制转换成Student引用类型才能运行
s1.eat();
}
}
6.2 instanceof和类型转换
- instanceof关键词可以判断两个类之间是否存在父子关系,即x指向的实际类型是不是y的子类型
package com.oop.demo06;
public class Teacher extends Person{}
package com.oop.demo06;
public class Student extends Person{
public void go(){
}
}
/*
public static void main(String[] args) {
// Object > String
// Object > Person > Teacher
// Object > Person > Student
//关键看new什么
Object object = new Student();
System.out.println(object instanceof Student); //true
System.out.println(object instanceof Person); //true
System.out.println(object instanceof Object); //true
System.out.println(object instanceof Teacher); //false
System.out.println(object instanceof String); //false object和String有点关系
System.out.println("=============================");
Person person = new Student();
System.out.println(person instanceof Student); //true
System.out.println(person instanceof Person); //true
System.out.println(person instanceof Object); //true
System.out.println(person instanceof Teacher); //false
//System.out.println(person instanceof String); //编译报错! person和String完全没有关系
System.out.println("=============================");
Student student = new Student();
System.out.println(student instanceof Student); //true
System.out.println(student instanceof Person); //true 子类与父类有关,父类与子类无关
System.out.println(student instanceof Object); //true 子类与父类有关,父类与子类无关
//System.out.println(student instanceof Teacher); //编译报错! student和Teacher完全没有关系
//System.out.println(student instanceof String); //编译报错! student和String完全没有关系
}
*/
package com.oop.demo06;
public class Person {
public void run(){
System.out.println("run");
}
}
/*
public static void main(String[] args) {
//类型之间的转化: 父 子 //基本类型转换——高低 64 32 16 8 高转低强转,低转高不需要
//子类转换为父类,可能丢失自己本来的一些方法!
Student student = new Student();
student.go();
//低转高
Person person = student;
//person.go(); //编译报错
//高转低
//高 低
Person obj = new Student();
//student.go(); //编译错误
//将student这个对象转换成Student类型,就可以使用Student类的方法了
//高转低要强制转换
//Student student = (Student) obj; 方法一
//student.go();
((Student) obj).go(); //方法二
}
*/
/*
1. 父类引用指向子类的对象
2. 把子类转换为父类,向上转换
3. 把父类转换为子类,向下转换:强制转换
4. 方便方法的调用,减少重复代码,简洁!
抽象思想:封装,继承,多态!(面向对象三大基本特征)
*/
7 static关键字
package com.oop.demo07;
//static
public class Student {
private static int age;//静态的变量 多线程 希望很多类操作,用static
private double score;//非静态的变量
public void run(){
go(); //static和类一起加载,可以直接调用
}
public static void go(){}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age); //Student.age.sout回车 如果是静态变量,推荐直接使用类名访问
//System.out.println(Student.score); //编译错误 未定义static score
System.out.println(s1.age);
System.out.println(s1.score);
go(); //在当前类,可以不加Student
Student.go();
new Student().run(); //如果不new Student,编译报错
}
}
package com.oop.demo07;
public class Person {
//1
{
//代码块(匿名代码块):赋初始值 程序执行时不能主动调用这个模块 创建对象时自动创建,在构造器之前就加载好了
//代码块(匿名代码块):赋初始值 程序执行时不能主动调用这个模块 创建对象时自动创建,在构造器之前就加载好了
System.out.println("匿名代码块");
}
//2
static{
//静态代码块 类加载时执行,且只执行一次
System.out.println("静态代码块");
}
//3
// 构造器/构造方法
public Person() {
System.out.println("构造方法"); //new一个新对象的时候执行
}
public static void main(String[] args) {
Person person1 = new Person();//静态代码块 匿名代码块 构造方法
System.out.println("=====================================");
Person person2 = new Person();//匿名代码块 构造方法
}
}
package com.oop.demo07;
//静态导入包
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);
}
}
//final 常量修饰符 通过final修饰的类无法被继承,没有子类
8 抽象类
package com.oop.demo08;
//abstract抽象类:extends 单继承 (接口可以多继承)
public abstract class Action {
//约束~有人帮我们实现~
//abstract 抽象方法,只有方法名字,没有方法的实现
public abstract void doSomething();
//1. 不能new这个抽象类,只能靠子类去实现它:约束!
//2. 抽象类中可以写普通方法
//3. 抽象方法必须在抽象类中
//抽象的抽象:约束~
//存在的意义:提高开发效率
//new 存在构造器(class文件查看)
}
package com.oop.demo08;
//抽象类的所有方法,继承它的子类,都必须要实现(父类没写,子类需要重写)它的方法,除非子类也是abstract(子子类实现)
public class A extends Action{
@Override
public void doSomething() {
}
}
9 接口的定义与实现
- 自己无法写方法,专业的约束!
- 约束和实现分离:面向接口编程
package com.oop.demo09;
//interface 定义接口的关键字,接口都需要实现类
public interface UserService {
//属性 默认常量 默认public static final(写出来是灰色)
//一般不在接口定义常量
int age = 99;
//接口中的所有定义其实都是抽象的 默认public abstract(写出来是灰色) 不需要写
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
package com.oop.demo09;
public interface TimeService {
void timer();
}
package com.oop.demo09;
//抽象类:extends
//(实现)类 可以实现接口 implements 接口
//实现了接口的类,就需要重写接口中的方法 command+回车/alt回车 重写接口中的方法,否则编译报错
//利用接口实现多继承
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 query(String name) {
}
@Override
public void timer() {
}
}
接口的作用
1. 约束
2. 定义一些方法,让不同的人实现
3. 所有方法都是 public abstract
4. 所有常量都是 public static final
5. 接口不能被实例化,接口没有构造方法(构造器)--->没有类,更没有构造器
6. implements可以实现多个接口
7. 必须重写接口中的方法
10 内部类
- 内部类可以获得外部类的私有属性、私有方法
10.1 成员内部类
package com.oop.demo10;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性和方法
public void getID(){
System.out.println(id);
out();
}
}
}
package com.oop;
import com.oop.demo10.Outer;
public class Application {
public static void main(String[] args) {
//new
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();// outer.new Inner(); alt回车
inner.in(); //这是内部类的方法
inner.getID(); //10 这是外部类的方法
}
}
10.2 静态内部类
- 静态内部类不能访问非静态外部类的属性,因为内部类已加载,外部类还没出生
- 除非都加static
package com.oop.demo10;
public class Outer {
private static int id = 10;
public static void out(){
System.out.println("这是外部类的方法");
}
public static class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性和方法
public void getID(){
System.out.println(id);
out();
}
}
}
10.3 局部内部类
- 一个java文件可以有多个class类,但只能有一个public class
public class Outer {
//局部内部类 在方法里写一个类
public void method(){
class Inner{
public void in(){
}
}
}
}
10.4 匿名内部类
package com.oop.demo10;
//匿名内部类
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();
}