• JAVA 面向对象(OOP)


    1.什么是面向对象?

    2.面向过程与面向对象的区别

    3.回顾方法的定义

    • break和return的区别

     跳出switch,结束循环和return的区别

    •  方法名: 注意规范。驼峰命名法。
    • 参数列表
    • 异常抛出

    4.回顾方法的调用

    • 静态方法
    • 非静态方法
    • 形参和实参
    • 值传递和引用传递
    • this 关键字

    5.类与对象的创建

     使用new关键字创建对象

    使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用。

    类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下两个特点:
    1、必须和类的名字相同
    2、必须没有返回类型,也不能写void

    1. package org.psmaxwell.oop.demo02;
    2. //学生类
    3. public class Student {
    4. //属性: 字段
    5. String name; //null
    6. int age; //0
    7. //方法
    8. public void study(){
    9. System.out.println(this.name +"在学习");
    10. }
    11. }
    12. /**
    13. * public static void main(String[] args) {
    14. *
    15. *
    16. * // 类: 抽象的, 实例化
    17. * // 类实例化后会返回一个自己的对象!
    18. * //student 对象就是一个Student类的具体实例!
    19. * Student xiaoming = new Student();
    20. * Student xh = new Student();
    21. *
    22. * xiaoming.name = "小明";
    23. * xiaoming.age = 3;
    24. *
    25. * System.out.println(xiaoming.name);
    26. * System.out.println(xiaoming.age);
    27. *
    28. * xh.name = "小红";
    29. * xh.age = 3;
    30. *
    31. * System.out.println(xh.name);
    32. * System.out.println(xh.age);
    33. *
    34. * }
    35. */

    6.构造器详解

    1. package org.psmaxwell.oop.demo03;
    2. public class Pet {
    3. public String name;
    4. public int age;
    5. //无参构造
    6. public void shout(){
    7. System.out.println("叫了一声");
    8. }
    9. }
    10. /**
    11. *
    12. Pet dog = new Pet();
    13. dog.name = "旺财";
    14. dog.age = 3;
    15. dog.shout();
    16. System.out.println(dog.name);
    17. System.out.println(dog.age);
    18. */

    7.简单小结类与对象

          
            1.类是一个模板,对象是一个具体的实例。
            2.方法
            定义、调用!
            3.对应的引用
            引用类型: 基本类型
            对象是通过引用来操作的,栈---》堆

            4.属性: 字段Field 成员变量
            默认初始化
            数字 : 0     0.0
            char : u0000
            boolean : false
            引用 : null

            修饰符 属性类型 属性名 = 属性值!

            5.对象的创建和使用
            - 必须使用new 关键字创造对象,构造器 Person Maxwell = new Person();
            - 对象的属性 Maxwell.name
            - 对象方法  Maxwell.sleep()

            6.类
               静态的属性    属性
               动态的行为    方法

               封装、继承、多态

     8.封装详解

        该露的露,该藏的藏
       我们程序设计要追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成。不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。     

    封装(数据的隐藏)
    通常,应禁止直接访问一个对象中数据的实际标识,而应通过操作接口来访问,这称为信息隐藏。

    1. 记住这句话就够了:属性私有,get/set
    2. package org.psmaxwell.oop.Demo04;
    3. //类 private: 私有
    4. public class Student {
    5. //属性私有
    6. private String name; //名字
    7. private int id; //学号
    8. private char sex; //性别
    9. private int age;
    10. //提供一些可以操作这个属性的方法
    11. // 提供一些public的get、set的方法
    12. //get 获得这个数据
    13. public String getName(){
    14. return this.name;
    15. }
    16. // set 给这个数据设置值
    17. public void setName(String name){
    18. this.name=name;
    19. }
    20. //alt + Insert
    21. public int getId() {
    22. return id;
    23. }
    24. public void setId(int id) {
    25. this.id = id;
    26. }
    27. public char getSex() {
    28. return sex;
    29. }
    30. public void setSex(char sex) {
    31. this.sex = sex;
    32. }
    33. public int getAge() {
    34. return age;
    35. }
    36. public void setAge(int age) {
    37. if(age>120 || age<0) {
    38. this.age = 3;
    39. }else{
    40. this.age = age;
    41. }
    42. }
    43. }
    44. /**
    45. *
    46. * Student s1 = new Student();
    47. *
    48. * s1.setName("Maxwell");
    49. *
    50. * System.out.println(s1.getName());
    51. *
    52. * s1.setAge(999); //不合法
    53. *
    54. * System.out.println(s1.getAge());
    55. */

    9.什么是继承?

     10.Super详解

    super注意点:
    1.调用父类的构造方法,必须在构造方法的第一个
    2.super必须只能出现在子类的方法或者构造方法中。
    super和this不能同时调用构造方法。


    Super Vs this:
    代表的对象不同:
    this : 本身调用者这个对象
    super:  代表父类对象的应用

    前提:
    this : 没有继承也可以使用
    super: 只能在继承条件下才可以使用

    构造方法:
    this() : 本类构造器。
    super() : 父类构造器!

    1. package org.psmaxwell.oop;
    2. import org.psmaxwell.oop.demo05.Person;
    3. import org.psmaxwell.oop.demo05.Student;
    4. // 一个项目应该只存在一个main方法
    5. public class Application {
    6. public static void main(String[] args) {
    7. Student student = new Student();
    8. // student.test("小山");
    9. // student.test1();
    10. }
    11. }
    1. package org.psmaxwell.oop.demo05;
    2. // 学生 is 人 子类 派生类
    3. // 子类继承了父类,就会拥有父类的全部方法
    4. public class Student extends Person {
    5. public Student() {
    6. //隐藏代码:调用了父类的无参构造
    7. super();//调用父类的构造器,必须要在子类构造器的第一行。
    8. System.out.println("Student无参构造执行了");
    9. }
    10. //Ctrl + H
    11. private String name = "xiaoshan";
    12. public void print(){
    13. System.out.println("Student");
    14. }
    15. public void test1(){
    16. print(); //Student
    17. this.print(); //Student
    18. super.print(); //Person
    19. }
    20. public void test2(String name){
    21. System.out.println(name); // 小山 main
    22. System.out.println(this.name); //调用子类
    23. System.out.println(super.name); //调用父类
    24. }
    25. }
    1. package org.psmaxwell.oop.demo05;
    2. //Person is 人 父类
    3. //在Java中,所有的类,都默认直接或者间接继承Object
    4. public class Person /*extends Object*/{
    5. public Person() {
    6. System.out.println("Person无参执行了");
    7. }
    8. protected String name = "Maxwell";
    9. //私有的东西无法被继承
    10. public void print(){
    11. System.out.println("Person");
    12. }
    13. }

    11.方法重写

    ---重写

    重写: 需要有继承关系,子类重写父类的方法!
    1.方法名必须相同
    2.参数列表必须相同
    3.修饰符:范围可以扩大; public> protected>Default>private
    4.抛出的异常:范围可以被缩小,但不能扩大;ClassNotFoundException ---> Exception(大)
    重写,子类的方法和父类必须一致;方法体不同

    为什么需要重写?
    1.父类的功能,子类不一定需要,或者不一定满足。
    Alt + Insert :overwrite;

    1. package org.psmaxwell.oop;
    2. import org.psmaxwell.oop.demo05.A;
    3. import org.psmaxwell.oop.demo05.B;
    4. //静态的方法和非静态方法区别很大!
    5. //非静态: 重写
    6. public class Application {
    7. public static void main(String[] args) {
    8. // 静态方法: //方法的调用只和左边,定义的数据类型有关
    9. A a = new A();
    10. a.test(); //A
    11. //父类的引用指向子类
    12. B b = new A(); //子类重写了父类的方法
    13. b.test(); //B
    14. }
    15. }
    1. package org.psmaxwell.oop.demo05;
    2. //继承
    3. public class A extends B{
    4. //重写
    5. @java.lang.Override //注解
    6. public void test() {
    7. System.out.println("A=>test()");
    8. }
    9. }
    1. package org.psmaxwell.oop.demo05;
    2. //重写都是方法的重写,与属性无关
    3. public class B {
    4. public void test(){
    5. System.out.println("B=>test()");
    6. }
    7. }

    12.instanceof和类型转换

    1. package org.psmaxwell.oop;
    2. import org.psmaxwell.oop.demo06.Person;
    3. import org.psmaxwell.oop.demo06.Student;
    4. import org.psmaxwell.oop.demo06.Teacher;
    5. public class Application {
    6. public static void main(String[] args) {
    7. //类型之间的转换 父 子
    8. //高 低
    9. Person obj = new Student();
    10. //student 将这个对象转换为Student类型,我们就可以使用Student类型的方法了。
    11. // 子类转父类可能丢失自己的本来的一些方法!
    12. // Student student = (Student) obj;
    13. // student.go();
    14. ((Student) obj).go();
    15. /**
    16. * 1.父类的引用执行子类的对象
    17. * 2.把子类转换为父类,向上转型
    18. * 3.把父类转换为子类,向下转型,强制转换。
    19. * 4.方便方法的调用,减少重复的代码,简洁
    20. *
    21. * 封装、继承、多态、接口
    22. * 多实践、多测试、实践出真知。
    23. *
    24. *
    25. */
    26. }
    27. }
    1. package org.psmaxwell.oop.demo06;
    2. public class Person {
    3. public void run(){
    4. System.out.println("run");
    5. }
    6. }
    1. package org.psmaxwell.oop.demo06;
    2. public class Student extends Person {
    3. public void go(){
    4. System.out.println("go");
    5. }
    6. }
    7. /**
    8. *
    9. * public class Student extends Person {
    10. *
    11. * @java.lang.Override
    12. * public void run() {
    13. * System.out.println("son");
    14. * }
    15. *
    16. * public void eat(){
    17. * System.out.println("eating");
    18. * }
    19. * }
    20. *
    21. *
    22. *
    23. */
    24. /**
    25. *
    26. * // Object > String
    27. * // Object > Person > Teacher
    28. * // Object > Person > Student
    29. * Object object = new Student();
    30. *
    31. * //System.out.println(x instanceof y); //能不能通过
    32. *
    33. * System.out.println(object instanceof Student); //true
    34. * System.out.println(object instanceof Person); //true
    35. * System.out.println(object instanceof Object); //true
    36. * System.out.println(object instanceof Teacher); //false
    37. * System.out.println(object instanceof String); //false
    38. *
    39. * System.out.println("==========================");
    40. *
    41. * Person person = new Student();
    42. *
    43. * System.out.println(person instanceof Student); //true
    44. * System.out.println(person instanceof Person); //true
    45. * System.out.println(person instanceof Object); //true
    46. * System.out.println(person instanceof Teacher); //false
    47. * // System.out.println(person instanceof String); //编译报错
    48. *
    49. *
    50. * System.out.println("==========================");
    51. *
    52. * Student student = new Student();
    53. *
    54. * System.out.println(student instanceof Student); //true
    55. * System.out.println(student instanceof Person); //true
    56. * System.out.println(student instanceof Object); //true
    57. * //System.out.println(student instanceof Teacher); //编译报错
    58. * //System.out.println(student instanceof String); //编译报错
    59. */

    13.static关键字详解

    1. package org.psmaxwell.oop.demo07;
    2. //静态导入包
    3. import static java.lang.Math.random;
    4. import static java.lang.Math.PI;
    5. public class Test {
    6. public static void main(String[] args) {
    7. System.out.println(Math.random());
    8. System.out.println(PI);
    9. }
    10. }
    1. package org.psmaxwell.oop;
    2. public class Application {
    3. public static void main(String[] args) {
    4. }
    5. }
    1. package org.psmaxwell.oop.demo07;
    2. //static
    3. public class Student {
    4. private static int age; //静态的变量
    5. private double score; //非静态变量
    6. public void run(){
    7. go();
    8. }
    9. public static void go(){
    10. }
    11. public static void main(String[] args) {
    12. Student.go();
    13. }
    14. }
    1. package org.psmaxwell.oop.demo07;
    2. public class Person {
    3. //2
    4. {
    5. //代码块(匿名代码块)
    6. System.out.println("匿名代码块");
    7. }
    8. // 1 只执行一次
    9. static {
    10. //静态代码块
    11. System.out.println("静态代码块");
    12. }
    13. //3
    14. public Person() {
    15. System.out.println("构造方法");
    16. }
    17. public static void main(String[] args) {
    18. Person person = new Person();
    19. System.out.println("==========================");
    20. Person person2 = new Person();
    21. }
    22. }

    14.抽象类

    • abstract 修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法,如果修饰类,那么该类就是抽象类。
    • 抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类。
    • 抽象类,不能使用new关键字来创建对象,它是用来让子类继承的。
    • 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的。
    • 子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类。
    1. package org.psmaxwell.oop.demo08;
    2. //abstract 抽象类 类 extends: 单继承 (接口可以多继承)
    3. public abstract class Action {
    4. // 约束 有人帮我们实现-
    5. // abstract ,抽象方法,只有方法名字,没有方法的实现。
    6. //约束- 有人帮我们实现 抽象方法,只有方法的名字,没有方法的实现
    7. public abstract void doSomething();
    8. //1.不能new这个抽象类, 只能靠子类去实现它,约束。
    9. //2.抽象类中可以写普通的方法
    10. //3.抽象方法必须在抽象类中
    11. //抽象的抽象 : 约束
    12. // 思考题 new , 存在构造器吗?
    13. //存在的意义 抽象出来, 提高开发效率
    14. }
    1. package org.psmaxwell.oop.demo08;
    2. //抽象类的所有方法,都继承了它的子类,都必须要实现它的方法 除非
    3. public class A extends Action{
    4. @java.lang.Override
    5. public void doSomething() {
    6. }
    7. }

    15.接口的定义与实现

    1. package org.psmaxwell.oop.demo09;
    2. //抽象的思维 Java
    3. //interface 定义的关键字, 接口都需要有实现类。
    4. public interface UserService {
    5. //常量
    6. public static final int AGE = 99;
    7. //接口中的所有定义其实都是抽象的public
    8. void run(String name);
    9. void delete(String name);
    10. void update(String name);
    11. void query(String name);
    12. }
    1. package org.psmaxwell.oop.demo09;
    2. //抽象类 : extends
    3. // 类 可以实现接口 implements 接口
    4. // 实现了接口的类, 就需要重写接口中的方法
    5. //利用接口实现多继承
    6. public class UserServiceImpl implements UserService,TimeService{
    7. @java.lang.Override
    8. public void run(java.lang.String name) {
    9. }
    10. @java.lang.Override
    11. public void delete(java.lang.String name) {
    12. }
    13. @java.lang.Override
    14. public void update(java.lang.String name) {
    15. }
    16. @java.lang.Override
    17. public void query(java.lang.String name) {
    18. }
    19. @java.lang.Override
    20. public void timer() {
    21. }
    22. }
    1. package org.psmaxwell.oop.demo09;
    2. public interface TimeService {
    3. void timer();
    4. }

    作用:
    1.约束
    2.定义一些方法,让不同的人实现
    3.public abstract
    4.public static final
    5.接口不能被实例化,接口中没有构造方法
    6.implements 可以实现多个接口
    7.必须要重写接口中的方法
    8.总结博客

    16.N种内部类

    1. package org.psmaxwell.oop.demo10;
    2. public class Outer {
    3. private int id=10;
    4. public void out(){
    5. System.out.println("这是外部类的方法");
    6. }
    7. public class Inner{
    8. public void in(){
    9. System.out.println("这是内部类的方法");
    10. }
    11. //获得外部类的私有属性
    12. public void getID(){
    13. System.out.println(id);
    14. }
    15. }
    16. }
    1. package org.psmaxwell.oop;
    2. import org.psmaxwell.oop.demo10.Outer;
    3. public class Application {
    4. public static void main(String[] args) {
    5. // new
    6. Outer outer = new Outer();
    7. //通过这个外部类来实例化内部类
    8. Outer.Inner inner = outer.new Inner();
    9. inner.in();
    10. inner.getID();
    11. }
    12. }
    1. package org.psmaxwell.oop.demo10;
    2. public class Outer {
    3. //局部内部类
    4. public void method(){
    5. class Inner{
    6. public void in(){
    7. }
    8. }
    9. }
    10. }
    11. //一个java类中可以有多个class类,但只能有一个public class
    12. //class A{
    13. // public static void main(String[] args) {
    14. //
    15. // }
    16. //}
    1. package org.psmaxwell.oop.demo10;
    2. public class Outer {
    3. //局部内部类
    4. public void method(){
    5. class Inner{
    6. public void in(){
    7. }
    8. }
    9. }
    10. }
    11. //一个java类中可以有多个class类,但只能有一个public class
    12. //class A{
    13. // public static void main(String[] args) {
    14. //
    15. // }
    16. //}
    1. package org.psmaxwell.oop.demo10;
    2. public class Test {
    3. public static void main(String[] args) {
    4. //没有名字初始化类,不用讲实例保存到变量
    5. new Apple().eat();
    6. UserService userService = new UserService(){
    7. @java.lang.Override
    8. public void hello() {
    9. }
    10. };
    11. }
    12. }
    13. class Apple{
    14. public void eat(){
    15. System.out.println("1");
    16. }
    17. }
    18. interface UserService{
    19. void hello();
    20. }
  • 相关阅读:
    C#获取http请求的JSON数据并解析
    2022前端面试题总结
    区块链模块化的破局之路
    android源码学习-android异常处理机制
    【预测模型-SVM预测】基于粒子群算法结合支持向量机SVM实现Covid-19风险预测附matlab代码
    mplfinance 一个堪称完美python量化金融可视化工具详析
    官宣!Sam Altman加入微软,OpenAI临时CEO曝光,回顾董事会‘’政变‘’始末
    web:[SUCTF 2019]EasySQL
    平面运动机器人的传感器外参标定
    springboot项目如何打包成.sh脚本形式运行|assemly插件打包自定义脚本参数
  • 原文地址:https://blog.csdn.net/u011868279/article/details/126296394