首页 > 编程语言 >Day11.面向对象编程OOP(3)

Day11.面向对象编程OOP(3)

时间:2024-09-16 19:46:16浏览次数:1  
标签:String void System Day11 面向对象编程 println OOP public out

多态

  • 动态编译:类型:可扩展性
  • 即同一种方法可以根据发送对象的不同而采用多种不同的行为方式
  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

多态注意事项:

  1. 多态是方法的多态,属性没有多态

  2. 父类和子类有联系 类型转换异常:ClassCastException

  3. 存在条件:继承关系;方法需要重写;父类引用指向子类对象 Father f1 = new Son( );

    不能重写的方法:

    1. static 属于类,不属于实例
    2. final 常量
    3. private

instanceof (类型转换) 引用类型

  • 父类引用指向子类的对象

  • 把子类转换为父类,向上转型;

  • 把父类转换为子类,向下转型;强制转换

  • 方便方法的调用,减少重复的代码

    /*
            //Object > String
            //Object > Person > Stdent
            //Object > Person > Teacher
            //System.out.println(X instanceof Y);//能不能编译通过
            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
            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);//编译报错!
            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);//编译报错!
            //System.out.println(student instanceof String);//编译报错!
    */
    
package com.dongfang.oop.Demo07;

import com.dongfang.oop.Demo07.Person;
import com.dongfang.oop.Demo07.Student;
import com.dongfang.oop.Demo07.Teacher;

public class Application {
    public static void main(String[] args) {
        //类型之间的转化:父  子

        //高             低
        Person obj = new Student();

        //student将这个对象转换为Student类型
        Student student = (Student) obj;
        student.go();
        //子类转换为父类可能丢失自己本来的一些方法
        Person person = student;
    }
}

static关键字

package com.dongfang.oop.Demo08;

//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) {
        new Student().run();
        Student.go();

        /*
        Student student = new Student();

        System.out.println(Student.age);
        //System.out.println(Student.score);//非静态的字段
        System.out.println(student.age);
        System.out.println(student.score);
         */
    }
}
package com.dongfang.oop.Demo08;

public class Person {

    {//代码块(匿名代码块)  执行顺序2
        System.out.println("匿名代码块");
    }
    
    static{//静态代码块  执行顺序1  只执行一次
        System.out.println("静态代码块");
    }
    
    public Person(){//  执行顺序3
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person1 = new Person();//静态  匿名  构造
        System.out.println("=============================================");
        Person person2 = new Person();//匿名  构造
    }
}
package com.dongfang.oop.Demo08;

//静态导入包
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);
    }
}

抽象类

abstract修饰符可以用来修饰方法也可以修饰类,修饰的方法就是抽象方法,修饰的类就是抽象类

抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类

抽象类:不能使用new关键字来创建对象,它是用来让子类继承的

抽象方法:只有方法的声明,没有方法的实现,它是用来让子类实现的

子类继承抽象类,就必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类

package com.dongfang.oop.Demo09;

//abstract  抽象类:类extends:  单继承
//接口可以多继承
public abstract class Action {

    //约束  有人帮我们实现
    //abstract  抽象方法  只有方法名字,没有方法的实现
    public abstract void doSomething();

    //1.不能new这个抽象类,只能靠子类去实现它:约束
    //抽象类中可以写普通的方法
    //抽象方法必须在抽象类中
    //抽象的抽象:约束
}

接口

  • 普通类:只有具体实现
  • 抽象类:具体实现和规范(抽象方法)都有
  • 接口:只有规范,自己无法写方法,专业的约束

接口就是规范,定义的是一组规则:“如果是...则必须...”

接口的本质是契约,就像法律一样,制定好后都要遵守

作用:

  1. 约束
  2. 定义一些方法,让不同的人实现
  3. public abstract
  4. public static final
  5. 接口不能被实例化,接口中没有构造方法
  6. implements可以实现多个接口
  7. 必须要重写接口中的方法
package com.dongfang.oop.Demo10;

//interface关键字,接口都需要有实现类
public interface UserService {
    //接口中的所有定义都是抽象的public adstract

    //常量  public static final
    int AGE = 99;

    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
package com.dongfang.oop.Demo10;

public interface TimeService {
    void timer();
}
package com.dongfang.oop.Demo10;

//抽象类:extends
//类 可以 实现接口
//实现了接口的类,就需要重写接口中的方法
//多继承~利用接口实现多继承
public class UserServiceImp1 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() {
    }
}

内部类

  • 成员内部类
  • 静态内部类
  • 局部内部类
  • 匿名内部类
package com.dongfang.oop.Demo11;

public class Outer {

    private int id;
    public void out(){
        System.out.println("这是外部类的方法");
    }

    class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }

        //可以获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }

    //局部内部类
    public void method(){
        class Inner{
            public void in(){
            }
        }
    }
}

//一个java中可以有多个class类,但是只能有一个public class
class A{
    public static void main(String[] args) {
    }
}
package com.dongfang.oop.Demo11;

import com.dongfang.oop.Demo11.Outer;

public class Application {
    public static void main(String[] args) {
        //new
        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}
package com.dongfang.oop.Demo11;

public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不用将实例保存到变量中
        new Apple().eat();

        UserService userService = new UserService(){

            @Override
            public void hello() {
            }
        };
    }
}

class Apple{
    public void eat(){
        System.out.println("Apple eat");
    }
}

interface UserService{
    void hello();
}

标签:String,void,System,Day11,面向对象编程,println,OOP,public,out
From: https://www.cnblogs.com/dongfangyulv/p/18416540

相关文章

  • 【背时咯】简单记录一下大数据技术的核心组件,包括Hadoop、Spark、Kafka等,并说明它们在
    大数据技术的核心组件包括Hadoop、Spark、Kafka等,它们在大数据生态系统中扮演着不可或缺的角色。以下是对这些核心组件的详细解释及它们在大数据生态系统中的作用:Hadoop核心组件:Hadoop分布式文件系统(HDFS):提供高可靠性的数据存储能力,能够将大规模的数据集分布式存储在多......
  • 【计算机毕设-大数据方向】基于Hadoop的在线教育平台数据分析可视化系统的设计与实现
    ......
  • hadoop中小文件问题的解决方案
    鱼弦:公众号:红尘灯塔,CSDN内容合伙人、CSDN新星导师、51CTO(Top红人+专家博主) 、github开源爱好者(go-zero源码二次开发、游戏后端架构https://github.com/Peakchen)Hadoop小文件问题解决方案Hadoop小文件问题是指在Hadoop中存储大量小文件时,会降低Hadoop的性能和效率。这是......
  • 企业网络项目调试系列-05核心网安全 DHCP SERVER与DHCP SNOOPING配置
    整体拓扑背景:生产环境中,最基础的网络安全配置就是DHCP动态IP获取的问题,由于个人家用路由器价格便宜,很多用户为了自身上网方便,在办公室的内网接入线路桥接一个家用路由,但是这种路由器的内网口配置自动开启DHCP功能,如果将有线网接入线路,接到了家用路由设备的内网口(LAN)口,则会......
  • Day10.面向对象编程OOP(2)
    封装该露的露,该藏的藏高内聚,低耦合:高内聚:类的内部数据操作细节自己完成,不允许外部干涉低耦合:仅暴露少量的方法给外部使用提高程序的安全性,保护数据隐藏代码的实现细节统一接口提高系统的可维护性packagecom.dongfang.oop.Demo04;//类publicclassDemo01......
  • chapter08 面向对象编程高级 知识点总结Note
    文章目录static修饰符单例设计模式main()方法类的成员代码块实例变量赋值位置顺序final关键字abstract关键字使用抽象应用模板方法设计接口语法应用(多态匿名实现类)jdk8jdk9接口新特性类的成员内部类枚举类(自定义enum方法实现接口)注解常用注解与JUnit单元测试......
  • hadoop+java基于大数据的电影推荐系统 (源码+文档+调试+可视化大屏)
    收藏关注不迷路!!......
  • Day09.面向对象编程OOP(1)
    面向对象编程OOP面向过程&面向对象面向过程思想步骤清晰简单,第一步做什么,第二步做什么......面对过程适合处理一些较为简单的问题面向对象思想物以类聚,分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考。最后才对某个分类下的细节进行面向过......
  • Java 与大数据:Hadoop 和 Spark 的完美集成
    ......
  • Hadoop(十)HDFS API操作
    API操作Shell操作是在集群内部,即hadoop102上进行操作,API操作是希望在Windows上能远程连接集群实现增删改查操作一、客户端环境准备1、找到资料包路径下的Windows依赖文件夹,拷贝hadoop-3.1.0到非中文路径2、在Windows上配置HADOOP_HOME环境变量3、配置Path环境变量4、验证H......