[Java] abstract classes and interfaces

Li Liyu on flowers 2022-09-23 09:24:07 阅读数:546

javaabstractclassesinterfaces

Share the fourth motivational quote

人活一世,珍惜自己所拥有的,才能使自己幸福,追寻适合自己的,亦能使自己更幸福.Frustration mistakes misfortune and suffering,是成长的催长素,也是成熟的催熟剂.我们不希望有痛苦,但也绝不能惧怕痛苦.唯有征服自己的人,才有能力去征服一切.

目录

抽象方法和抽象类 

1、 Distinguish between ordinary methods and abstract methods

​编辑2、 Distinguish between ordinary classes and abstract classes

3、 定义一个抽象类

4、 Use abstract classes to describe abstract things

5、Advantages of abstract classes and abstract methods

6、 抽象类的局限性

接  口

1、生活中的接口

​编辑2、 Define and implement a simple interface

​编辑3、更复杂的接口

4、Advantages of using interfaces

5、面向对象设计的原则

Summary of interfaces and abstract classes 


 

抽象方法和抽象类 

1、 Distinguish between ordinary methods and abstract methods

(1)在Java中,当一个类的方法被abstract关键字修饰时,该方法称为抽象方法.

(2)The class in which the abstract method resides must be defined as an abstract class.

(3)When a method is defined as an abstract method,Means that the method will not have a concrete implementation(没有方法体),Instead, it is implemented through method overriding in a subclass of an abstract class.

(4)The syntax for defining an abstract method is as follows:

[访问修饰符] abstract <返回类型> <方法名>([参数列表]);

-->抽象方法需要使用abstract修饰,普通方法不需要.

-->抽象方法没有方法体,普通方法没有.

-->The class in which the abstract method resides must be defined as an abstract class.

-->抽象方法在子类中必须被实现(Subclasses override abstract methods in superclasses),如果子类不实现,The subclass should be positioned as an abstract class.

-->privateKeywords cannot be used to decorate abstract methods.

-->abstract修饰符不能和final修饰符一起使用.


2、 Distinguish between ordinary classes and abstract classes

 

 

(1)在Java中,当一个类被abstract关键字修饰时,该类称为抽象类.

(2)The syntax for defining an abstract class is as follows:

abstract class <类名>{

//代码

}

-->Abstract classes require modifiersabstract修饰,Common classes do not need.

-->普通类可以实例化,抽象类不能被实例化.

-->An abstract class may or may not have abstract methods,There may or may not be ordinary methods.

-->An abstract class can contain all the members of a normal class.


3、 定义一个抽象类

When a class is defined as abstract class,It can contain various types of members,包括属性、方法等.The methods can be divided into ordinary methods and abstract methods.

public abstract class 类名称{

修饰符 abstract 返回类型 方法名();

修饰符 返回类型 方法名(){

//方法体

}

}


4、 Use abstract classes to describe abstract things

When a class instantiation doesn't make sense,This class can be defined as an abstract class
 

5、Advantages of abstract classes and abstract methods

(1)Methods already implemented in an abstract class can be used by subclasses,Make the code reusable.

(2)抽象类中的抽象方法,Subclasses need to be overridden,It is guaranteed that the subclass has its own uniqueness.


6、 抽象类的局限性

(1)Abstract classes sometimes have the problem of code redundancy.

(2)Class inheritance is single root inheritance,一个类只能直接继承一个类.

接  口

1、生活中的接口

(1)JavaThe role of the interface is similar to the interface in life,It provides a convention,使得实现接口的类(或结构)在形式上保持一致.

(2)If all methods in an abstract class are abstract methods,It can be represented by an interface.

(3)接口可以看作是一种特殊的“抽象类”,但是采用与抽象类完全不同的语法来表示,The design concepts of the two are also different.


2、 Define and implement a simple interface

 

(1)An interface is a type that cannot be instantiated.The definition of an interface type is similar to the definition of a class,语法格式如下:

public interface 接口名{

//接口成员

        }

-->定义接口使用interface修饰符,访问修饰符只能是public,且可选.

-->Interface members can be all constants and public abstract methods.

-->Subclasses must be used to use interfaces,子类通过implements关键字实现接口.

(2)The syntax for implementing an interface is as follows:

                public 类名 implements 接口名 {

实现方法

普通方法

}

-->实现接口使用implements关键字.

-->实现接口的类必须实现接口中定义的所有抽象方法.The implementing class of the interface is allowed to contain ordinary methods.


3、更复杂的接口

 

(1)Interfaces themselves can also inherit from interfaces.

(2)The syntax of interface inheritance is as follows:

[修饰符] interface 接口名 extends 父接口1,父接口2,......{

常量定义

方法定义

}

(3)A normal class can only inherit from one parent class,But it can implement multiple interfaces at the same time,It is also possible to inherit abstract classes and implement interfaces at the same time.

(4)The syntax for implementing multiple interfaces is as follows:

class 类名 extends 父类名 implements 接口1,接口2,......{

类的成员

}

(5)About defining and implementing interfaces,需要注意以下几个方面的内容.

-->接口和类、An abstract class is a hierarchical concept,命名规则相同.

-->修饰符如果是public,则该接口在整个项目中可见.如果省略修饰符,This interface is only visible in the current package.

-->接口中可以定义常量,不能定义变量.The properties in the interface are used by default“public static final”修饰,即接口中的属性都是全局静态常量.接口中的常量必须在定义时指定初始值.

-->接口中的所有方法都是抽象方法,The methods in the interface all default to public.

-->和抽象类一样,Interfaces also cannot be instantiated,接口中不能有构造方法.

-->接口之间可以通过extends实现继承关系,一个接口可以继承多个接口,但接口不能继承类.

-->类只能继承一个父类,但可以通过implements实现多个接口.A class must implement all methods of the interface,否则必须定义为抽象类.If a class implements multiple interfaces while inheriting the parent class,extends必须位于implements之前.

 

4、Advantages of using interfaces

(1)接口可以解决JavaLimitations of Single Root Inheritance.

(2)Interfaces can be used as properties in abstract classes,The methods of this interface are called through properties.
 

 

5、面向对象设计的原则

(1)摘取代码中变化的行为,形成接口.

(2)多用组合,少用继承.

(3)针对接口编程,不依赖于具体实现.

If there is a dependency on a type,You should rely on interfaces as much as possible,Minimize reliance on subclasses.Because once the subclass changes,The possibility of code changes is high,And the interface is much more stable.

在具体的代码实现中,Reflected in the method parameters try to use the interface,The return value of the method should use the interface as much as possible,Attribute types try to use interfaces, etc.

(4)针对扩展开放,针对改变关闭.

If the requirements in the project have changed,A new interface or class should be added,Instead of modifying the original code.

 

Summary of interfaces and abstract classes 

1、 抽象方法使用abstract修饰符,没有方法体.
 

2、 抽象类使用abstract修饰符,不能实例化.

 

3、 类只能继承一个父类,但可以实现多个接口.A class implements all methods of an interface,否则必须定义为抽象类.JavaThe effect of multiple inheritance is achieved by implementing multiple interfaces.
 

4、 接口表示一种约定,Also means an ability,It reflects the principle of separation of convention and implementation.通过面向接口编程,可以降低代码间的耦合性,提高代码的可扩展性和可维护性.

 

 今天的分享就到这了,元气满满的一天,加油加油!!!

创作不易点赞评论互关三连

 

 

copyright:author[Li Liyu on flowers],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230907013002.html