Home > Java > javaTutorial > java - detailed introduction to object-oriented (3)

java - detailed introduction to object-oriented (3)

王林
Release: 2019-08-23 10:58:50
forward
2418 people have browsed it

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的行为
}
Copy after login

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("");
 
   }
 
}
Copy after login

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();
 
   }
 
}
Copy after login

接口和抽象类的比较

相同点:

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

都不能实例化;

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

区别:

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

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

二者的选用:

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

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

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

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

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

16、匿名内部类


适合只使用一次的类

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

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

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

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();
 
   }
 
}
Copy after login

输出:

绿色

蓝色

黄色

蓝色

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

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

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

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() {
 
    }
 
}
Copy after login

总结:枚举不可以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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template