• JavaOOP-封装、继承、多态、接口


    目录

    封装

    封装的实现

    访问权限级别

    构造方法

    重载

    static关键字

    继承

    继承概述

    继承的特性

    子类如何使用父类

    重写

     重写的特性

    抽象类

     final

     final的特性

    多态

    多态存在的三个必要条件

    多态之类型转换

    接口

    1.接口概述

    Java中的接口

    类和类之间的关系

     接口与接口之间的关系

     接口和类之间的关系

    2.接口与抽象类

    接口和抽象类的异同点

    3.接口应用


    封装

     封装是面向对象的核心思想,将对象的属性和行为封装起来,不需要让外界知道具体实现细节,这就是封装思想

    封装的实现

    1.封装是指在定义一个类时,将类中的属性私有化,即使用private关键字来修饰。

    2.私有属性只能在它所在类中被访问,如果外界想要访问私有属性,需要提供一些使用public修饰的公有方法。

    访问权限级别

    构造方法

     说明:

    1.利用构造方法的重载可以实用多个构造方法

    2.没有写构造方法,java虚拟机默认会生成一个没有任何参数的公共的构造方法

    3.相同的名称的普通方法也可以有多个,这些方法的参数列表必须不一样

    重载

    在同一个类中,多个方法中拥有相同的名字,但方法的参数列表不同(参数个数不同,顺序不同、类型不同)

    1. package oop.entity;
    2. /**
    3. *企鹅类
    4. * @author zyt
    5. */
    6. public class Penguin {
    7. private String name;//昵称
    8. private int health;//健康值
    9. private String sex;//性别
    10. public Penguin() {
    11. }
    12. public Penguin(String name, int health) {
    13. this.name = name;
    14. this.health = health;
    15. }
    16. /**
    17. *构造方法--带三个参数
    18. * @param name
    19. * @param health
    20. * @param sex
    21. */
    22. public Penguin(String name, int health, String sex) {
    23. this.name = name;
    24. this.health = health;
    25. this.sex = sex;
    26. }
    27. public String getName() {
    28. return name;
    29. }
    30. public void setName(String name) {
    31. this.name = name;
    32. }
    33. public int getHealth() {
    34. return health;
    35. }
    36. public void setHealth(int health) {
    37. this.health = health;
    38. }
    39. public String getSex() {
    40. return sex;
    41. }
    42. public void setSex(String sex) {
    43. this.sex = sex;
    44. }
    45. }
    1. package oop.entity;
    2. public class PenguinText {
    3. public static void main(String[] args) {
    4. //通过构造方法,将对象的属性值初始化
    5. Penguin penguin = new Penguin("Q仔",100,"男");
    6. // penguin.setName("Q仔");
    7. // penguin.setHealth(100);
    8. // penguin.setSex("男");
    9. System.out.println(penguin.getName()+","+ penguin.getHealth()+","+ penguin.getSex());
    10. }
    11. }

    static关键字

     说明:

    static表示“静态”的意思,用来修饰成员变量和成员方法,也可以形参静态 static代码块等

    1. package oop.entity;
    2. public class Person {
    3. String name;
    4. static String classes;
    5. public static void print(){
    6. System.out.println("班级" +classes +"的同学");
    7. }
    8. public void show(){
    9. System.out.println(name);
    10. }
    11. public void show(String str){
    12. System.out.println(name + "");
    13. }
    14. //return 不能作为重载依据
    15. public static void main(String[] args) {
    16. Person p1 = new Person();
    17. p1.name= "张三";
    18. // p1.classes="1001";
    19. Person.classes="1001";
    20. Person p2= new Person();
    21. p2.name= "李四";
    22. // p2.classes="1001";
    23. Person p3= new Person();
    24. p3.name= "王五";
    25. // p3.classes="1001";
    26. p1.print();
    27. p2.print();
    28. p3.print();
    29. }
    30. }

    不管创建多少个对象,这个static修饰的变量,它只占一块内存

    注:如果是一个静态的方法,那么它只能访问静态的属性。


    继承

    继承概述

     格式 :

    class 子类 extends 父类{

      ....

    }

    1. public class Animal {
    2. private String name;
    3. private int id;
    4. public Animal(String name, int id) {
    5. this.name = name;
    6. this.id = id;
    7. }
    8. public void eat(){
    9. System.out.println(name+"正在吃");
    10. }
    11. public void sleep(){
    12. System.out.println(name+"正在睡");
    13. }
    14. public void introduction(){
    15. System.out.println("大家好!我是" + id +"号"+ name);
    16. }
    17. }
    1. package oop.entity;
    2. public class Mouse extends Animal{
    3. public Mouse(String myName,int myId){
    4. super(myName,myId);
    5. }
    6. }

    继承的特性

     单继承指一个子类只能继承一个父类,多继承指的是B类继承A类,A类是B的父类;C类继承B类,B类是C类的父类。

    子类如何使用父类

     

     父类:

    1. package oop.entity;
    2. public class Fu {
    3. private int num = 4;
    4. public int getNum(){
    5. return this.num;
    6. }
    7. }

     子类:

    1. package oop.entity;
    2. import java.sql.SQLOutput;
    3. public class Zi extends Fu{
    4. private int num= 5;
    5. public void show(){
    6. System.out.println(this.num+"...."+super.getNum());
    7. }
    8. }

    测试类:

    1. package oop;
    2. import oop.entity.Zi;
    3. public class ZiText {
    4. public static void main(String[] args) {
    5. Zi z = new Zi();
    6. z.show();
    7. }
    8. }

    重写

     

     重写的特性

    Animal类:(父类)

    1. package oop.entity;
    2. public class Animal {
    3. private String name;
    4. private int id;
    5. public Animal(String name, int id) {
    6. this.name = name;
    7. this.id = id;
    8. }
    9. public void eat(){
    10. System.out.println(name+"正在吃");
    11. }
    12. public void sleep(){
    13. System.out.println(name+"正在睡");
    14. }
    15. public void introduction(){
    16. System.out.println("大家好!我是" + id +"号"+ name);
    17. }
    18. }

    Mouth类:

    1. package oop.entity;
    2. public class Mouse extends Animal{
    3. public Mouse(String myName,int myId){
    4. super(myName,myId);
    5. }
    6. public void eat(){
    7. System.out.println("老鼠正在吃粮食");
    8. }
    9. }

    测试类:

    1. package oop;
    2. import oop.entity.Mouse;
    3. public class MouseText {
    4. public static void main(String[] args) {
    5. Mouse m = new Mouse("Jerry",01);
    6. m.eat();
    7. }
    8. }

    抽象类

    格式

    1. //抽象类
    2. 修饰符 abstract class 类名{
    3. //抽象方法
    4. 修饰符 abstract 返回值类型 方法名([参数列表]);
    5. }

    说明:

    abstract修饰方法,称为抽象方法,只有方法的定义,没有方法体并且只能定义在抽象类中;abstract 修饰类,称为抽象类,抽象类,抽象类不能直接实例化,需要借助子类重写类中全部的抽象方法,通过子类创建对象。

    父类:(抽象类)

    1. package oop.entity;
    2. public abstract class Animal01 {
    3. private String name;
    4. public abstract void eat();
    5. }

    子类:

    1. package oop.entity;
    2. public class Mouse01 extends Animal01{
    3. @Override
    4. public void eat() {
    5. System.out.println("老鼠在吃粮食");
    6. }
    7. }
    1. package oop.entity;
    2. public class Penguin01 extends Animal01{
    3. @Override
    4. public void eat() {
    5. System.out.println("企鹅正在吃鱼虾");
    6. }
    7. }

    常见问题一:

     常见问题二:

     目的:不让这个类去创建对象

    常见问题3:

    常见问题4:

     final

     final的特性

    1. package oop.entity;
    2. /**
    3. * final 修饰类不能被继承
    4. */
    5. public final class Msg {
    6. //定义常量(final+static 修饰属性)
    7. public static final String TITLE ="java";
    8. public final void show(){
    9. System.out.println(TITLE);
    10. }
    11. }
    1. package oop;
    2. import oop.entity.Msg;
    3. public class MsgText {
    4. public static void main(String[] args) {
    5. Msg m = new Msg();
    6. // m.TITLE; 会报错
    7. m.show();
    8. }
    9. }

    多态

    概念

    多态指允许不同类的对象对同一消息做出相应。

    即同一消息可以根据发生对象的不同而采取不同的行为方式 

     抽象类

    1. package oop.ob;
    2. public abstract class Animal {
    3. abstract void eat();
    4. }

    子类1:

    1. package oop.ob;
    2. public class Cat extends Animal {
    3. public void eat(){
    4. System.out.println("吃鱼");
    5. }
    6. public void catchMouse(){
    7. System.out.println("抓老鼠");
    8. }
    9. }

    子类2:

    1. package oop.ob;
    2. import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
    3. public class Dog extends Animal{
    4. @Override
    5. public void eat() {
    6. System.out.println("啃骨头");
    7. }
    8. public void lookHome(){
    9. System.out.println("看家");
    10. }
    11. }
    1. package oop.ob;
    2. public class PolymorphicDemo {
    3. /**
    4. * 使用父类作为方法的参数实现多态
    5. * @param a
    6. */
    7. public static void method(Animal a){
    8. a.eat();
    9. }
    10. /**
    11. * 使用父类作为方法的返回值实现多态
    12. * @return
    13. */
    14. public static Animal get()
    15. {
    16. return new Dog();
    17. }
    18. public static void main(String[] args) {
    19. // //调用
    20. // Cat c = new Cat();
    21. // method(c);
    22. // method(new Dog());//Dog d = new Dog;
    23. //使用父类的引用指向子类对象
    24. Animal c = new Cat();
    25. // Animal d = new Dog();
    26. method(c);
    27. method(new Dog());
    28. }
    29. }

    多态存在的三个必要条件

    1.继承

    2.重写

    3.父类引用指向子类对象[Animal c = new Cat()]

    当使用多态方法调用方式时,首先检查父类中是否有该方法,如果没有,则编译错误;如果有再去调用子类同名方法。

    多态之类型转换

     向上转型例:Animal a = new (Cat);

    向下转型例:Cat c = (Cat)a; c.__();

    1. package oop.ob;
    2. public class PolymorphicDemo {
    3. /**
    4. * 使用父类作为方法的参数实现多态
    5. * @param a
    6. */
    7. public static void method(Animal a){
    8. //调用eat()方法
    9. a.eat();
    10. //instanceof 判断对象的具体方法
    11. //原先的 a.__是找不到子类特有的方法 先判断,接着向下转型 就可以调用子类特有的方法了
    12. if(a instanceof Cat){
    13. Cat c =(Cat) a;
    14. c.catchMouse();
    15. }else if(a instanceof Dog){
    16. Dog d =(Dog) a;
    17. d.lookHome();
    18. }
    19. }

    接口

    1.接口概述

    接口的特点
    特点:统一标准,让大家知道是做什么的,但是不知道具体怎么做

    Java中的接口

     接口中的属性默认是使用 public static final修饰的 修饰的是常量

    接口中的默认方法是使用 public abstract 修饰的

    类和类之间的关系

     接口与接口之间的关系

     use 3.0继承了 usb2.0b并拓展了

     

     注:多继承

     接口和类之间的关系

     

     注:多实现

    2.接口与抽象类

    接口和抽象类的异同点

    3.接口应用

    接口1:

    1. package oop.service;
    2. /**
    3. * 接口:提供一种公共的功能
    4. */
    5. public interface MathService01 {
    6. public int sum(int a,int b);//+
    7. public int sub(int a,int b);//-
    8. }

    接口2:

    1. package oop.service;
    2. /**
    3. * 接口:提供一种公共的功能
    4. */
    5. public interface MathService02 {
    6. public int mul(int a,int b);//*
    7. public int div(int a,int b);// /
    8. }

     实现:

    1. package oop.service.impl;
    2. import oop.service.MathService01;
    3. import oop.service.MathService02;
    4. public class MathServiceImpl implements MathService01, MathService02 {
    5. @Override
    6. public int sub(int a, int b) {
    7. return a-b;
    8. }
    9. @Override
    10. public int sum(int a, int b) {
    11. return a+b;
    12. }
    13. @Override
    14. public int div(int a, int b) {
    15. return a/b;
    16. }
    17. @Override
    18. public int mul(int a, int b) {
    19. return a*b;
    20. }
    21. }

    测试类:

    1. package oop.service;
    2. import oop.service.impl.MathServiceImpl;
    3. public class MathText {
    4. public static void main(String[] args) {
    5. MathService01 ms1 = new MathServiceImpl();
    6. ms1.sub(10,10);
    7. ms1.sum(10,10);
    8. MathService02 ms2 = new MathServiceImpl();
    9. ms2.div(10,10);
    10. ms2.mul(10,10);
    11. }
    12. }

  • 相关阅读:
    第6讲 SQL语言之概述
    如何利用Flutter框架开发运行小程序
    开源模型 Zephyr-7B 发布——跨越三大洲的合作
    22-9-18学习笔记-MySQL和Redis
    sql server 设置字段自增
    在一张 24 GB 的消费级显卡上用 RLHF 微调 20B LLMs
    Node.js第一节:初识Node.js与内置模块(fs文件系统模板、path路径模板、http模板)
    在autodl搭建stable-diffusion-webui+sadTalker
    SpringBoot集成jjwt和使用
    分布式爬虫
  • 原文地址:https://blog.csdn.net/m0_64005381/article/details/127133406