首页 > 编程语言 >Java官方笔记7接口

Java官方笔记7接口

时间:2023-06-05 23:35:14浏览次数:49  
标签:Java int double 笔记 public 接口 return method

接口

接口只能包含:constants, method signatures(abstract), default methods, static methods, and nested types

方法体只存在于:default methods and static methods

接口不能实例化,只能被类实现,或者被其他接口继承(接口可以多继承)。

定义接口:

public interface OperateCar {

   // constant declarations, if any

   // method signatures
   
   // An enum with values RIGHT, LEFT
   int turn(Direction direction,
            double radius,
            double startSpeed,
            double endSpeed);
   int changeLanes(Direction direction,
                   double startSpeed,
                   double endSpeed);
   int signalTurn(Direction direction,
                  boolean signalOn);
   int getRadarFront(double distanceToCar,
                     double speedOfCar);
   int getRadarRear(double distanceToCar,
                    double speedOfCar);
         ......
   // more method signatures
}

类实现接口:

public class OperateBMW760i implements OperateCar {

    // the OperateCar method signatures, with implementation --
    // for example:
    public int signalTurn(Direction direction, boolean signalOn) {
       // code to turn BMW's LEFT turn indicator lights on
       // code to turn BMW's LEFT turn indicator lights off
       // code to turn BMW's RIGHT turn indicator lights on
       // code to turn BMW's RIGHT turn indicator lights off
    }

    // other members, as needed -- for example, helper classes not 
    // visible to clients of the interface
}

如果接口不是public,那么只能被同一个包里面的类实现。

实现接口

接口:

public interface Relatable {

    // this (object calling isLargerThan())
    // and other must be instances of 
    // the same class returns 1, 0, -1 
    // if this is greater than, 
    // equal to, or less than other
    public int isLargerThan(Relatable other);
}

实现:

public class RectanglePlus 
    implements Relatable {
    public int width = 0;
    public int height = 0;
    public Point origin;

    // four constructors
    public RectanglePlus() {
        origin = new Point(0, 0);
    }
    public RectanglePlus(Point p) {
        origin = p;
    }
    public RectanglePlus(int w, int h) {
        origin = new Point(0, 0);
        width = w;
        height = h;
    }
    public RectanglePlus(Point p, int w, int h) {
        origin = p;
        width = w;
        height = h;
    }

    // a method for moving the rectangle
    public void move(int x, int y) {
        origin.x = x;
        origin.y = y;
    }

    // a method for computing
    // the area of the rectangle
    public int getArea() {
        return width * height;
    }
    
    // a method required to implement
    // the Relatable interface
    public int isLargerThan(Relatable other) {
        RectanglePlus otherRect 
            = (RectanglePlus)other;
        if (this.getArea() < otherRect.getArea())
            return -1;
        else if (this.getArea() > otherRect.getArea())
            return 1;
        else
            return 0;               
    }
}

对于接口:

public interface DoIt {
   void doSomething(int i, double x);
   int doSomethingElse(String s);
}

如果想添加新方法,为了避免影响已经实现了该接口的所有类,有以下几种方式:

①继承

public interface DoItPlus extends DoIt {

   boolean didItWork(int i, double x, String s);
   
}

②default method

public interface DoIt {

   void doSomething(int i, double x);
   int doSomethingElse(String s);
   default boolean didItWork(int i, double x, String s) {
       // Method body 
   }
}

如果继承了接口,那么子接口的默认方法有可能出现:

  1. 无声明,继承父接口的默认方法
  2. 声明,变为abstract method,实现类,必须实现该方法(也就是父类默认,但是子类不默认了)
  3. 声明并重写,以子类的默认方法为准

③static method

public interface TimeClient {
    // ...
    static public ZoneId getZoneId (String zoneString) {
        try {
            return ZoneId.of(zoneString);
        } catch (DateTimeException e) {
            System.err.println("Invalid time zone: " + zoneString +
                "; using default time zone instead.");
            return ZoneId.systemDefault();
        }
    }

    default public ZonedDateTime getZonedDateTime(String zoneString) {
        return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
    }    
}

接口的static method,就相当于把多个class的static method抽到了一个公共的地方,实现该接口的类,都具有static method。

接口类型

接口也可以作为类型:

public Object findLargest(Object object1, Object object2) {
   Relatable obj1 = (Relatable)object1;
   Relatable obj2 = (Relatable)object2;
   if ((obj1).isLargerThan(obj2) > 0)
      return object1;
   else 
      return object2;
}

接口类型的赋值对象,必须是实现该接口的类的实例。

参考资料:

https://dev.java/learn/interfaces/

标签:Java,int,double,笔记,public,接口,return,method
From: https://www.cnblogs.com/df888/p/17459290.html

相关文章

  • 接口幂等性
    任意多次执行所产生的影响均与一次执行的影响相同,这是幂等性的核心特点。其实在我们编程中主要操作就是CURD,其中读取(Retrieve)操作和删除(Delete)操作是天然幂等的,受影响的就是创建(Create)、更新(Update)一、接口幂等性概念接口调用存在的问题#现如今我们的系统大多拆分为分布式SOA,......
  • 【Java】再谈Springboot 策略模式
     第一次使用策略模式是一年前的一个项目:https://www.cnblogs.com/mindzone/p/16046538.html当时还不知道Spring支持集合类型的自动装配在最近一个项目,我发现很多业务需要频繁的使用这种模式去聚合代码 一、牛刀小试这是最开始的定义策略的业务接口/***业务推送管......
  • 【VUE】Vue 快速入门 笔记基础01
    一、vue相关了解1、概述Vue.js是一种流行的JavaScript框架,用于构建响应式、交互式的前端Web界面。它采用了基于组件的开发模式,允许在单个页面中使用多个可重用的组件,提高了代码的复用性和维护性。只关心视图层,自底向上.遵守SOC关注点分离原则(术有专攻,只关注一点)HTML+CSS......
  • TypeScript Vs JavaScript 区别
    一、观察1.JS平常的复制类型letval;val=123;val="123";val=true;val=[1,3,5];注意点:由于JS是弱类型的,所以只要定义了一个变量,就可以往这个变量中存储任意类型的数据也正是因为如此,所以会给我们带来一个问题2.假设a是一个数组,b是一个数值func......
  • C#中抽象类和接口的区别与使用
    抽象类是特殊的类,只是不能被实例化;除此以外,具有类的其他特性;重要的是抽象类可以包括抽象方法,这是普通类所不能的。抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们。 一、抽象类: 抽象类是特殊的类,只是不能被实例化;除此以外,具有类的其他特性;重要的是抽象......
  • 《深入理解Spring Cloud与微服务构建》学习笔记(二十)~配置中心Spring Cloud Config
    本例重新创建项目,构建一个空的mavan工程。一、ConfigServer从本地读取配置文件 新建一个moudleconfig_server,pom添加依赖1.2.<groupId>org.springframework.cloud</groupId>3.<artifactId>spring-cloud-config-server</artifactId>4.</dependency>启动类添加......
  • Elastic_Search 和java的入门结合
    1,pom文件添加依赖... 2,config配置文件  3,写接口文件 ......
  • windows笔记本极致省电指南
    用到了三个软件:parkcontrol,processlasso,quickCPUparkcontrol-调整CPU的运行核心和频率,可以设置离电的时候关闭一些CPU核心数,以达到省电的目的插电的时候是全核心运行,离电的时候只有一个核心两个线程在运行。在显示高级设置里,设置使用的核心数,为了极致省电,把电池模式下的......
  • Java如何实现去重?这是在炫技吗?
    大家好,我3y啊。由于去重逻辑重构了几次,好多股东直呼看不懂,于是我今天再安排一波对代码的解析吧。austin支持两种去重的类型:N分钟相同内容达到N次去重和一天内N次相同渠道频次去重。Java开源项目消息推送平台......
  • Java学习笔记(十五)
    1.请描述你理解的转换流 转换流(InputStreamReader和OutputStreamWriter)是字节流和字符流之间的一种桥梁,用于将字节流转换为字符流或将字符流转换为字节流。转换流可以解决字节流和字符流之间的编码转换问题,从而使得我们可以方便地在不同的字符集之间进行转换。2.请描述你理解......