一 什么是面向对象
面向对象编程(Object-Oriented Programming,OOP)
面向对象编程的本质就是:以类的方式组织代码,以对象组织(封装)数据。
三大特性:
从认识论角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
从代码运行角度考虑是先有类后有对象。类是对象的模板。
二,回顾方法
1,方法的定义:
- 修饰符
- 返回类型
- break和return的区别。(break跳出循环switch;return结束方法,返回结果)
- 方法名:驼峰命名法
- 参数列表:(参数类型 参数名)
- 异常抛出
| 修饰符 返回值类型 方法名(...){ |
| 方法体 |
| return 返回值; |
| } |
| 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; |
| } |
| public void print(){ |
| return; |
| } |
| } |
2,方法的调用:
| package oop; |
| |
| public class demo02 { |
| public static void main(String[] args) { |
| Student.say(); |
| |
| Student stu = new Student(); |
| stu.sayHello(); |
| } |
| } |
| ---------------------------------------------- |
| package oop; |
| |
| public class Student { |
| |
| public static void say(){ |
| System.out.println("学生说话"); |
| } |
| |
| public void sayHello(){ |
| System.out.println("HelloWorld!"); |
| } |
| } |
| public class Demo03 { |
| public static void main(String[] args) { |
| |
| int sum = Demo03.add(1,2); |
| System.out.println(sum); |
| } |
| public static int add(int a,int b){ |
| return a+b; |
| } |
| } |
| |
| |
| public class Demo04 { |
| public static void main(String[] args) { |
| int a = 1; |
| System.out.println(a); |
| Demo04.change(a); |
| System.out.println(a); |
| |
| } |
| |
| public static void change(int a){ |
| a = 10; |
| } |
| } |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| Person person = new Person(); |
| System.out.println(person.name); |
| Demo05.change(person); |
| System.out.println(person.name); |
| } |
| public static void change(Person person){ |
| |
| person.name = "admin"; |
| } |
| } |
| class Person{ |
| String name; |
| } |
三,类与对象的创建
1,类和对象的关系
- 类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物。
- 对象是抽象概念的具体事例
2,创建与初始化对象
| package oop.Demo02; |
| |
| public class Application { |
| |
| public static void main(String[] args) { |
| |
| Student stu = new Student(); |
| Student xm = new Student(); |
| Student xh = new Student(); |
| xm.name = "小明"; |
| xm.age = 16; |
| System.out.println(xm.name); |
| System.out.println(xm.age); |
| System.out.println(xh.name); |
| System.out.println(xh.age); |
| } |
| } |
| package oop.Demo02; |
| |
| public class Student { |
| |
| String name; |
| int age; |
| |
| public void study(){ |
| System.out.println(this.name+"在学习"); |
| } |
| } |
四,构造器详解
- 构造器的方法名和类名相同
- 默认没有返回值
- new本质是在调用构造器方法
- 用于初始化对象的值
- 如果定义了有参数的构造器方法,必须显式的定义一个无参数的构造器方法
| package oop.Demo02; |
| |
| public class Application { |
| |
| public static void main(String[] args) { |
| Person student = new Person(); |
| System.out.println(student.name); |
| Person teacher = new Person("teacher"); |
| System.out.println(teacher.name); |
| } |
| } |
| package oop.Demo02; |
| |
| public class Person { |
| |
| String name; |
| |
| public Person(){ |
| this.name = "student"; |
| } |
| |
| public Person(String name){ |
| this.name = name; |
| } |
| } |
五、封装
可以通过Alt+Insert快捷键,选择“Getter and Setter",可以快速生成get/set方法。
| package oop.Demo04; |
| |
| import oop.Demo04.Student; |
| |
| public class Application { |
| public static void main(String[] args) { |
| Student stu1 = new Student(); |
| stu1.setName("student"); |
| System.out.println(stu1.getName()); |
| } |
| } |
| package oop.Demo04; |
| |
| public class Student { |
| |
| private String name; |
| private int age; |
| private char sex; |
| |
| |
| 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 = 3; |
| } |
| this.age = age; |
| } |
| |
| public char getSex() { |
| return sex; |
| } |
| |
| public void setSex(char sex) { |
| this.sex = sex; |
| } |
| } |
六、继承
-
继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
-
extands的意思是扩展,子类是父类的扩展。
-
JAVA中类只有单继承,没有多继承!
-
继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
-
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
-
子类和父类之间,从意义上讲应该具有 is a 的关系。
| package oop.Demo05; |
| |
| public class Person { |
| public int money = 10_0000_0000; |
| public void say() { |
| System.out.println("说了一句话"); |
| } |
| } |
| package oop.Demo05; |
| |
| public class Student extends Person { |
| } |
| package oop.Demo05; |
| |
| public class Application { |
| public static void main(String[] args) { |
| Student stu = new Student(); |
| stu.say(); |
| System.out.println(stu.money); |
| } |
| } |
| package oop.Demo05; |
| |
| public class Application { |
| public static void main(String[] args) { |
| Student stu = new Student(); |
| |
| stu.test1(); |
| } |
| } |
| package oop.Demo05; |
| |
| public class Person { |
| protected String name = "person"; |
| public void print(){ |
| System.out.println("person"); |
| } |
| } |
| package oop.Demo05; |
| |
| public class Student extends Person { |
| private String name = "student"; |
| |
| public void test1(){ |
| print(); |
| this.print(); |
| super.print(); |
| } |
| 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 > protected > default > private
- 抛出的异常:范围可以被缩小,不能扩大
| ackage oop.Demo05; |
| |
| public class Application { |
| public static void main(String[] args) { |
| |
| A a = new A(); |
| a.test(); |
| |
| B b = new A(); |
| b.test(); |
| } |
| } |
| |
| package oop.Demo05; |
| |
| public class A extends B{ |
| public static void test(){ |
| System.out.println("A->test()"); |
| } |
| } |
| |
| package oop.Demo05; |
| |
| public class B { |
| public static void test(){ |
| System.out.println("B->test()"); |
| } |
| } |
| |
| package oop.Demo05; |
| |
| public class Application { |
| public static void main(String[] args) { |
| |
| A a = new A(); |
| a.test(); |
| |
| B b = new A(); |
| b.test(); |
| } |
| } |
| |
| package oop.Demo05; |
| |
| public class A extends B{ |
| public void test(){ |
| System.out.println("A->test()"); |
| } |
| } |
| |
| package oop.Demo05; |
| |
| public class B { |
| public void test(){ |
| System.out.println("B->test()"); |
| } |
| } |
| |
七、多态
| package oop.Demo06; |
| |
| public class Application { |
| public static void main(String[] args) { |
| |
| Student s1 = new Student(); |
| Person s2 = new Student(); |
| |
| s2.run(); |
| s1.run(); |
| s1.eat(); |
| |
| } |
| } |
| |
| package oop.Demo06; |
| |
| public class Person { |
| public void run(){ |
| System.out.println("run"); |
| } |
| } |
| |
| package oop.Demo06; |
| |
| public class Student extends Person{ |
| |
| public void run(){ |
| System.out.println("son"); |
| } |
| public void eat(){ |
| System.out.println("eat"); |
| } |
| } |
| |
1. instanceof
| package oop.Demo06; |
| |
| 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); |
| |
| |
| } |
| } |
| |
| package oop.Demo06; |
| |
| public class Person { |
| public void run(){ |
| System.out.println("run"); |
| } |
| } |
| |
| package oop.Demo06; |
| |
| public class Teacher extends Person{ |
| } |
| package oop.Demo06; |
| |
| public class Student extends Person{ |
| } |
2. 类型转换
- 父类引用指向子类的对象
- 把子类转换为父类,向下转换
- 把父类转换为子类,向下转换,强制转换
- 方便方法的调用,减少重复代码
| package oop.Demo06; |
| |
| public class Application { |
| public static void main(String[] args) { |
| |
| Person obj = new Student(); |
| |
| Student student = (Student)obj; |
| student.go(); |
| |
| ((Student)obj).go(); |
| } |
| } |
| package oop.Demo06; |
| |
| public class Person { |
| public void run(){ |
| System.out.println("run"); |
| } |
| } |
| package oop.Demo06; |
| |
| public class Student extends Person{ |
| public void go(){ |
| System.out.println("go"); |
| } |
| } |
3、static关键字
| package oop.Demo07; |
| |
| 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(age); |
| System.out.println(s1.score); |
| go(); |
| s1.run(); |
| } |
| } |
| package oop.Demo07; |
| |
| 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(); |
| } |
| } |
八、抽象类
-
abstract修饰符可以用来修饰方法,也可以修饰类,如果修饰方法,那么该方法就是抽象方法,如果修饰类,该方法就是抽象类。
-
抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类。
-
抽象类,不能使用 new 关键字来创建对象,它是用来让子类继承的。
-
抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的。
-
子类继承抽象类,那么就必须要实现抽象类,没有实现的抽象方法,否则该子类也要声明为抽象类。
-
特点:
- 不能new这个抽象类,只能靠子类去实现抽象类中的方法
- 抽象类中可以写普通的方法
- 抽象方法必须在抽象类中
| package oop.Demo08; |
| |
| public abstract class Action { |
| |
| public abstract void run(); |
| } |
| package oop.Demo08; |
| |
| public class A extends Action{ |
| public void run(){ |
| |
| } |
| } |
九、接口
声明类的关键字是 class
,声明接口的关键字是 interface
-
普通类:只有具体实现
-
抽象类:具体实现和规范(抽象方法)都有
-
接口:只有规范
-
接口就是规范,定义的是一组规则,体现了现实世界中“如果你是……则必须能……”的思想。如果你是汽车,则必须能跑。
-
接口的本质是契约,就像法律一样,指定好后大家都要遵守。
-
面向对象(OO)的精髓,是对对象的抽象,最能体现这一点的就是接口。
1.接口的作用:
- 约束
- 定义一些方法,让不同的人实现
- 接口中的方法默认都是 public abstract
- 接口中的变量默认都是 public static final
- 接口不能被实例化,接口中没有构造方法
- 接口可以实现多个继承,implements关键字
- 必须要重写接口中的方法
| package oop.Demo09; |
| |
| public interface User { |
| void run(String name); |
| void add(String name); |
| void update(String name); |
| void delete(String name); |
| } |
| package oop.Demo09; |
| |
| public interface Time { |
| void timer(); |
| } |
| package oop.Demo09; |
| |
| public class UserImpl implements User,Time{ |
| |
| |
| @Override |
| public void add(String name) { |
| |
| } |
| |
| @Override |
| public void delete(String name) { |
| |
| } |
| |
| @Override |
| public void run(String name) { |
| |
| } |
| |
| @Override |
| public void update(String name) { |
| |
| } |
| |
| @Override |
| public void timer() { |
| |
| } |
| } |
十、内部类
内部类就是在一个类的内部,再定义一个类,比如A类中定义一个B类,那么B类相对A类来说,就是内部类,而A类相对B类来说就是外部类。
| package oop.Demo10; |
| |
| public class Application { |
| public static void main(String[] args) { |
| Outer out = new Outer(); |
| Outer.Inner in = out.new Inner(); |
| in.getID(); |
| } |
| } |
| package 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 void method(){ |
| |
| class A{ |
| |
| } |
| } |
| } |
| |
| class B{ |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人