面向对象编程 OOP
-
初识面向对象
- 面向对象本质:以类的方式组织代码,以对象的形式组织数据。
- 三大特性:
- 封装
- 继承
- 多态
-
方法回顾和加深
-
//引用传递:传递一个对象 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);//杰 } public static void change(Person person){ person.name = "杰"; } } class Person{ String name; }
-
//值传递 public class Demo04 { public static void main(String[] args) { int a = 1; System.out.println(a);//1 Demo04.change(a); System.out.println(a);//1 } public static void change(int a){ a = 10; } }
-
public class Demo02 { public static void main(String[] args) { //实例化这个类 new Student student = new Student(); student.say(); } //和类一起加载的 public static void a(){ //b(); } //类实例化后才存在 public void b(){ } }
-
-
对象的创建分析
-
类是一种抽象的数据类型,是对某一类事物整体描述/定义,不代表某一具体事物。
-
对象是抽象概念的具体实例。
-
//一个项目应该只有一个main方法 public class Application { public static void main(String[] args) { //类:抽象的,要实例化 //类实例化后会返回一个自己的对象 Student xiaoming = new Student(); Student xiaohong = new Student(); xiaoming.name = "小明"; xiaoming.age = 15; xiaohong.name = "小红"; xiaohong.age = 14; System.out.println(xiaoming.name); System.out.println(xiaoming.age); System.out.println(xiaohong.name); System.out.println(xiaohong.age); } }
-
public class Student { //属性 String name;//null int age; //方法 public void study(){ System.out.println(this.name+"在学习"); } }
-
构造器:
-
public class Person { String name; int age; //实例化初始值 //1.使用new关键字,必须要有构造器 public Person(){ this.name = "李阳"; } //一旦定义有参构造,无参构造必须显示定义 public Person(String name){ this.name = name; } } /* public static void main(String[] args) { //实例化一个对象 Person person = new Person(); System.out.println(person.name); } */ /* 构造器: 1.和类名相同 2.没有返回值 */
-
-
alt + insert
-
-
面向对象的三大特性
-
封装
-
属性私有,get/set
-
主要对于属性
-
public class Student { //属性私有 private String name; private int id; private char sex; //提供操作属性方法 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 class Application { public static void main(String[] args) { Student s1 = new Student(); s1.setName("陈"); System.out.println(s1.getName());//陈 } } */
-
-
继承
-
extends
-
java中类只有单继承,没有多继承。一个儿子只能有一个爸爸,一个爸爸可以有多个儿子。
-
一个为子类,一个为父类。
-
子类继承父类,就会拥有父类的全部方法。
-
super,调用父类构造器必须放在子类构造器第一行。
-
/* 1.super调用父类构造方法 2.super只能出现在子类构造方法中。 3.super和this不能同时调用构造方法。 super只能在继承条件下才能使用。 this();本类的构造。 super();父类的构造。
-
-
方法重写:
- 重写都是方法的重写,和属性无关。
-
方法名必须相同 参数列表必须相同 修饰符:范围可以扩大。:public > producted > defualt > private 抛出的异常:范围,可以被缩小,但不能扩大;
-
public class Application {
//静态方法和非静态方法区别很大
//静态方法,//方法的调用只和左面,定义的类型有关
//非静态:重写
public static void main(String[] args) {
//方法的调用只和左面,定义的类型有关
A a = new A();
a.test();
//父类的引用指向了子类
B b = new A();//子类重写了父类的方法
b.test();
}
}
为什么要重写:
- 父类的功能子类不一定需要,或者不一定满足。
- ALT +Insert
-
多态
-
对象能执行哪些方法,主要看左面类型,和右边关系不大。
-
- 多态是方法的多态,属性没有多态。
- 父类和子类有联系 类型转换异常! ClassCastException!
- 多态存在条件:继承关系,方法需要重写,父类引用指向子类对象! father f1 = new Son();
不能重写:
- static 方法属于类,不属于实例。
- final 常量
- private方法:
-
instanceof (类型转换) 引用类型,判断一个对象是什么类型
-
public class Application { public static void main(String[] args) { //Object > Person > Student //Object > String //Object > Person > Teacher Object object = new Student(); //System.out.println(X instanceof Y); 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 Person person = new Student(); System.out.println("=========="); 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 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 } }
-
public class Application { public static void main(String[] args) { //类型之间转换:父 子 Person obj = new Student(); //obj将这个对象转换为Student类型,就可以使用Student类型的方法了 /*Student student = (Student) obj; student.go();*/ ((Student)obj).go(); //子类转化为父类,可能丢失自己本来的一些方法 Student student = new Student(); student.go(); Person person = student; } } /* 1.父类引用指向子类对象 2.把子类转换为父类,向上转型 3.把父类转化为子类,向下转型 ;强制转换 4.方便方法调用,减少重复代码 抽象:封装、继承、多态! */
package com.oop.demo07; public class Person { //2.赋初值 { //代码块(匿名代码块) System.out.println("代码块"); } //1. static { //静态代码块,只执行一次 System.out.println("静态代码块"); } //3. public Person() { System.out.println("构造方法"); } public static void main(String[] args) { Person person1 = new Person(); System.out.println("=========="); Person person2 = new Person(); } } package com.oop.demo07; //static public class Student { private static int age;//静态变量 多线程 private double score; //非静态可以调用静态,静态只可以调用静态 public void run(){ go(); } public static void go(){ } public static void main(String[] args) { Student s1 = new Student(); System.out.println(Student.age); System.out.println(s1.score); System.out.println(s1.age); Student s2 = new Student(); } } package com.oop.demo07; //静态导入包 import static java.lang.Math.random; public class Test { public static void main(String[] args) { System.out.println(random()); } }
-
5. 抽象类和接口
//abstract 抽象类:类 extends :单继承 (接口可以多继承)
public abstract class Action {
//约束 有人帮我们实现
//abstract 只有方法名字,没有实现
public abstract void deSomething();
//1.不能new这个抽象类,只能靠子类实现它;
//2.抽象类可以写普通方法;
//3.抽象方法必须在抽象类中;
//抽象的抽象:约束
public void hello(){
}
}
package com.oop.demo08;
//抽象类的方法,继承他的子类,都必须实现他的方法
public class A extends Action{
@Override
public void deSomething() {
}
}
-
接口
- 只有规范,自己无法写方法,约束和实现分离
//interface 定义关键字 public interface UserService { //接口中的所有定义其实都是抽象的 public abstract void run(String name); void add(String name); void delete(String name); void update(String name); void query(String name); } public interface TimeService { void timer(); } package com.oop.demo09; import java.util.Timer; //类 可以实现接口 implements //实现了接口的类,就需要重写接口中的方法 //多继承 利用接口实现 public class UserServiceImpl implements UserService, TimeService { @Override public void run(String name) { } @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.必须重写接口的方法 */
6.内部类及OOP实战
-
就是在一个类的内部再定义一个类。
-
- 成员内部类
- 静态内部类
- 局部内部类
- 匿名内部类
-
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); } } } public class Application { public static void main(String[] args) { //new Outer outer = new Outer(); //通过外部类实例化内部类 Outer.Inner inner = outer.new Inner(); inner.in(); inner.getId(); } }
-
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(); }