search
HomeJavajavaTutorialjava - detailed introduction to object-oriented (3)
java - detailed introduction to object-oriented (3)Aug 23, 2019 am 10:58 AM
object-oriented

This article is continued from the above:java - Detailed introduction to object-oriented (2)

11. Interface

Introduction: An abstract class is a template abstracted from multiple classes. If you want to implement this abstraction more thoroughly, you have to use a special "abstract class" → interface;

Example:

The USB interfaces we hear in life are not actually the slots we see, but a specification that those slots follow; The slots we see are just instances designed according to the USB specification, which means that the slots are instances of USB;

corresponds to different models of USB devices, and their respective USB slots are There is a specification that needs to be followed. Complying with this specification can ensure that the device inserted into the slot can communicate with the motherboard normally;

For multiple USB slots on the motherboard of the same model, they have the same data exchange method. With the same implementation details, they can be considered to be different instances of the same class.

My summary:

The interface only defines the specifications that the class should follow, but does not care about the internal data of these classes. and the implementation details within its methods.

The interface only stipulates the methods that must be provided in these classes; thereby separating specification and implementation. It enhances the scalability and maintainability of the system;

The benefits of using interfaces are better scalability and maintainability, so we often use interfaces in development. (It is equivalent to defining a standard)

interface definition

Interface defines a specification that stipulates what a class must do, but it does not matter how it is done;

[modifier] interface interface name extends parent interface 1, parent interface 2....

There is no construction method and cannot be instantiated;

Interfaces can only inherit interfaces, not classes

There are no ordinary methods in the interface, and the methods are all abstract;

The default modifier of the method in the interface is public abstract;

The fields in the interface are all global constants, and the default modifier is public static final;

The members in the interface include (mainly The first two):

Global constants

Public abstract methods

Internal classes (including internal classes, internal interfaces, internal enumeration classes);

My summary:

The interface has no constructor and cannot be instantiated!

All methods in the interface are abstract. There are no ordinary methods. They have the default modifier public abstract and must be overridden!

12. Use of interfaces

Format:public class SubImpl extends Super implements IA,IB

Interfaces can be inherited from multiple sources , but you can only inherit interfaces, not classes.

Implementation interface (supports multiple implementations)

[Modifier] class class name implements interface 1, interface 2...

The implementation of the interface must be after extends;

The method to implement the interface must be of public type

The relationship between the interface and the class:

The implementation relationship or inheritance relationship.

Can be said to be a class Having implemented the methods of the interface, it can also be said that the class inherits the methods of the interface, with different understandings in different situations!

13. Establishing standards and simple factory model for interface-oriented programming

Set a standard and let others implement or satisfy it!

Eg:
interface USB{//定义USB标准
    void useUSB();//USB有使用USB的行为
}

Simple factory mode

Build a factory and produce in it. When using it, just use it

My summary:

Benefits: Shielding The differences in the implementation of different subclasses improve the scalability and maintainability of the code;

package reviewDemo;
 
//简单工厂模式
 
 
 
interface Phone{//制定标准,都要实现send()方法
 
   public void send();
 
}
 
 
 
class Iphone implements Phone{
 
   @Override
 
   public void send() {
 
      System.out.println("Iphone手机在发短信");
 
   }
 
}
 
 
 
class AndroidPhone implements Phone{
 
   @Override
 
   public void send() {
 
      System.out.println("AndroidPhone手机在发短信");
 
   }
 
}
 
 
 
class MyPhone implements Phone{
 
   @Override
 
   public void send() {
 
      System.out.println("MyPhone手机在发短信");
 
   }
 
}
 
 
 
class Factory{
 
   public static void show(String type){//传入参数,根据不同的类型个性化定制
 
      if(type.equals("")){//为空的情况,不用往下执行
 
         System.out.println("对不起,类型为空!,请重新输入!");
 
         return;
 
      }
 
      Phone p = null;
 
      if("Iphone".equals(type)){//判断类型
 
         p = new Iphone();
 
      }else if("AndroidPhone".equals(type)){
 
         p = new AndroidPhone();
 
      }else{
 
         p = new MyPhone();
 
      }
 
      p.send();
 
   }
 
}
 
 
 
public class FactoryDemo17 {
 
   public static void main(String[] args) {
 
     
 
      new Factory().show("Iphone");//调用方法
 
      new Factory().show("AndroidPhone");
 
      new Factory().show("MyPhone");
 
      new Factory().show("YourPhone");
 
      new Factory().show("");
 
   }
 
}

Output:

Iphone is sending text messages

AndroidPhone is sending text messages

MyPhone is sending text messages

MyPhone is sending text messages

Sorry, the type is empty!

14、面向接口编程之适配器模式

使用一个现成的类,但是它的接口不完全符合你的需求,我只想要它其中的一个方法,不想覆写其他的方法。

比如,窗体有变大,变小,关闭的行为,但是我现在只需要关闭行为;

package reviewDemo;
 
//适配器模式:只想用其中的某一个方法,用适配器作为中间的过渡
 
 
 
interface Windows{
 
   void max();
 
   void min();
 
   void close();
 
}
 
 
 
//适配器模式,实现接口所有的方法,但是不写方法体!
 
class AdapterWindows implements Windows{
 
 
 
   @Override
 
   public void max() {
 
   }
 
 
 
   @Override
 
   public void min() {
 
   }
 
 
 
   @Override
 
   public void close() {
 
   }
 
  
 
}
 
 
 
class MyWindows extends AdapterWindows{
 
   //覆写父类的方法
 
   public void close(){
 
      System.out.println("这个实现的是关闭功能!");
 
   }
 
}
 
 
 
public class Demo17 {
 
   public static void main(String[] args) {
 
      new MyWindows().close();
 
   }
 
}

接口和抽象类的比较

相同点:

都位于继承的顶端,用于被其他实现或继承;

都不能实例化;

都包含抽象方法,其子类都必须覆写这些抽象方法;

区别:

抽象类为部分方法提供实现,避免子类重复实现这些方法,提供代码重用性;接口只能包含抽象方法;

一个类只能继承一个直接父类(可能是抽象类),却可以实现多个接口;(接口弥补了Java的单继承)

二者的选用:

优先选用接口,尽量少用抽象类;

需要定义子类的行为,又要为子类提供共性功能时才选用抽象类;

总结:接口不能有构造函数,抽象类是可以有构造函数的,

abstract可以定义构造函数(包括带函数的构造函数),因为要保证其子类在创建的时候能够进行正确的初始化,但是Abstract类不能被实例化。

知识点:如果不可以或者没有创建对象,那么我们必须加上static修饰,不能用对象调用,就只好用类去调用。

16、匿名内部类


适合只使用一次的类

不能是抽象类,因为系统在创建匿名内部类的时候,会立即创建匿名内部类的对象。

匿名内部类不能定义构造器,因为匿名内部类没有类名。

格式:
new 父类构造器([实参列表]) 或 接口()
{
//匿名内部类的类体部分
}

17、枚举类

使用enum声明,默认直接继承了java.lang.Enum类,而不是Object类;

枚举类的对象是固定的,实例个数有限,不可以再new( ),枚举对象后可以跟()。

枚举元素必须位于枚举类体中的最开始部分,枚举元素后要有分号与其他成员分隔。

枚举类的构造方法的权限修饰符默认是private;

一旦枚举对象后面加上{},那么该对象实际是枚举匿名内部类对象;

所有枚举类都提供一个静态的values()方法(返回该枚举类所有对象组成的数组),便于遍历所有枚举对象;

所有枚举类都提供一个静态的valueOf(String name)方法, 返回枚举类中对象名等于 name的对象。

Eg:public enum Color{
 
        RED(), GREEN(){}, BLUE{};
 
}
 
 
 
 
 
package reviewDemo;
 
//枚举
 
 
 
enum Color{
 
   Green,Blue,Yellow;
 
  
 
   @Override
 
   public String toString() {
 
      String ret = super.toString();
 
      switch (this) {
 
      case Green:
 
         ret = "绿色";
 
         break;
 
        
 
      case Blue:
 
         ret = "蓝色";
 
         break;
 
        
 
      case Yellow:
 
         ret = "黄色";
 
         break;
 
 
 
      default:
 
         break;
 
      }
 
     
 
      return ret;
 
   }
 
  
 
}
 
 
 
class Personp{
 
   Color c = Color.Blue;
 
   void show(){
 
      System.out.println(c);
 
   }
 
}
 
 
 
public class Demo18 {
 
   public static void main(String[] args) {
 
      Color []color = Color.values();
 
      for (Color c : color) {
 
         System.out.println(c);
 
      }
 
      new Personp().show();
 
   }
 
}

输出:

绿色

蓝色

黄色

蓝色

枚举类覆写接口抽象方法的两种方式:

在枚举类中实现接口的抽象方法;

在枚举匿名内部类中实现接口的抽象方法;

interface I{
 
    void show();
 
}
 
 
 
enum Color implements I{
 
    RED(){
 
   public void show(){
 
        }
 
    }, GREEN{
 
   public void show(){
 
        }
 
    }, BLUE{
 
   public void show(){
 
        }
 
    };
 
}
 
 
 
enum Color implements I{
 
    RED(), GREEN, BLUE;
 
    public void show() {
 
    }
 
}

总结:枚举不可以new();即便是反射也不可以!

备注:一个类如果没有构造方法,那么一定有相对应的某个方法可以获取对象!

The above is everything I want to write about object-oriented. If there is anything wrong, please contact me for correction. Thanks!

For more related content, please visit the PHP Chinese website: JAVA Video Tutorial

The above is the detailed content of java - detailed introduction to object-oriented (3). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
如何使用Go语言实现面向对象的事件驱动编程如何使用Go语言实现面向对象的事件驱动编程Jul 20, 2023 pm 10:36 PM

如何使用Go语言实现面向对象的事件驱动编程引言:面向对象的编程范式被广泛应用于软件开发中,而事件驱动编程是一种常见的编程模式,它通过事件的触发和处理来实现程序的流程控制。本文将介绍如何使用Go语言实现面向对象的事件驱动编程,并提供代码示例。一、事件驱动编程的概念事件驱动编程是一种基于事件和消息的编程模式,它将程序的流程控制转移到事件的触发和处理上。在事件驱动

解析PHP面向对象编程中的享元模式解析PHP面向对象编程中的享元模式Aug 14, 2023 pm 05:25 PM

解析PHP面向对象编程中的享元模式在面向对象编程中,设计模式是一种常用的软件设计方法,它可以提高代码的可读性、可维护性和可扩展性。享元模式(Flyweightpattern)是设计模式中的一种,它通过共享对象来降低内存的开销。本文将探讨如何在PHP中使用享元模式来提高程序性能。什么是享元模式?享元模式是一种结构型设计模式,它的目的是在不同对象之间共享相同的

go语言是面向对象的吗go语言是面向对象的吗Mar 15, 2021 am 11:51 AM

go语言既不是面向对象,也不是面向过程,因为Golang并没有明显的倾向,而是更倾向于让编程者去考虑该怎么去用它,也许它的特色就是灵活,编程者可以用它实现面向对象,但它本身不支持面向对象的语义。

python是面向对象还是面向过程python是面向对象还是面向过程Jan 05, 2023 pm 04:54 PM

python是面向对象的。Python语言在设计之初,就定位为一门面向对象的编程语言,“Python中一切皆对象”就是对Pytho 这门编程语言的完美诠释。类和对象是Python的重要特征,相比其它面向对象语言,Python很容易就可以创建出一个类和对象;同时,Python也支持面向对象的三大特征:封装、继承和多态。

PHP面向对象编程入门指南PHP面向对象编程入门指南Jun 11, 2023 am 09:45 AM

PHP作为一种广泛使用的编程语言,已成为构建动态网站和网络应用程序的首选语言之一。其中,面向对象编程(OOP)的概念和技术越来越受到开发者的欢迎和推崇。本篇文章将为读者提供PHP面向对象编程的入门指南,介绍OOP的基本概念,语法和应用。什么是面向对象编程(OOP)?面向对象编程(Object-OrientedProgramming,简称OOP),是一种编程

如何使用Go语言实现面向对象的数据库访问如何使用Go语言实现面向对象的数据库访问Jul 25, 2023 pm 01:22 PM

如何使用Go语言实现面向对象的数据库访问引言:随着互联网的发展,大量的数据需要被存储和访问,数据库成为了现代应用开发中的重要组成部分。而作为一门现代化、高效性能的编程语言,Go语言很适合用来处理数据库操作。而本文将重点讨论如何使用Go语言实现面向对象的数据库访问。一、数据库访问的基本概念在开始讨论如何使用Go语言实现面向对象的数据库访问之前,我们先来了解一下

Python中的面向对象编程Python中的面向对象编程Jun 10, 2023 pm 05:19 PM

Python作为一种高级编程语言,在众多编程语言中占有举足轻重的地位。它的语法简单易学,拥有各种强大的编程库,被广泛应用于数据处理、机器学习、网络编程等领域。而其中最重要的一点便是Python完美支持面向对象编程,本文将重点阐述Python中的面向对象编程。一、面向对象编程的基本概念在面向对象的编程语言中,数据和方法被封装在对象的内部。这使得对象能够独立地进

面向对象是啥意思面向对象是啥意思Jul 17, 2023 pm 02:03 PM

面向对象是软件开发方法,一种编程范式。是一种将面向对象的思想应用于软件开发过程并指导开发活动的系统方法。这是一种基于“对象”概念的方法论。对象是由数据和允许的操作组成的包,它与目标实体有直接的对应关系。对象类定义了一组具有类似属性的对象。面向对象是基于对象的概念,以对象为中心,以类和继承为构建机制,认识、理解和描绘客观世界,设计和构建相应的软件系统。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!