当前位置:网站首页>Abstract classes and interfaces
Abstract classes and interfaces
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
Abstract classes and interfaces
Catalog :
List of articles
1. abstract class
1.1 The concept of abstract classes
Abstract classes are often used to represent the analysis of problem fields 、 Abstract concepts in design , It's about a series that looks different , But essentially the same abstraction of concrete concepts .
Usually used in programming statements abstract The decorated class is an abstract class . stay C++ in , Classes containing pure virtual functions are called abstract classes , It can't generate objects ; stay java in , A class containing abstract methods is called an abstract class , Similarly, objects cannot be generated .
Abstract classes are incomplete , It can only be used as a base class . stay Object oriented methods in , Abstract classes are mainly used for type hiding and acting as global variables .
1.2 Syntax of abstract classes
abstract class A
1.3 Abstract class features
- Use abstract The method defined by is called abstract method
- A class with abstract methods must be an abstract class , But abstract classes don't necessarily have abstract methods
- Abstract classes, like ordinary classes, can have member variables 、 Member method 、 Construction method 、 Code block
- Our abstract method cannot be private,static,final Embellished
- Abstract classes cannot instantiate objects , Can be inherited 【 That is to say, abstract classes are used to be inherited 】
- Ordinary classes inherit abstract classes , If there are abstract methods in an abstract class , Then you must override all the abstract methods in the abstract class
- When an abstract class A, Inherited the abstract class B You don't have to rewrite abstract classes B Abstract method of
- Abstract classes are equivalent to an extra layer of compiler validation
1.4 The role of abstract classes
Abstract classes are used to be inherited
In practice , The actual operation is not done in the parent class , When we compile with ordinary classes, if we misuse the parent class , The compiler does not report errors , But if it is an abstract class, it will remind us of errors when instantiating .
2. Interface
2.1 The concept of interface
An interface is a common code of conduct , When you realize , As long as it conforms to the standard , It can be universal . stay Java in , Interfaces can be seen as : Common specification for multiple classes , Is a reference data type .
2.2 grammar
- When creating an interface , Interfaces are generally named in uppercase I start
- Interfaces are usually named with adjective words
- The relationship between classes and interfaces is implementation (implements)
interface IRunning
class A implements IRunning
2.3 Interface to use
The interface cannot be used directly , There must be a class , To implement all the abstract methods in the interface
2.4 Interface features
- Use interface To represent the interface
- The member variables in the interface must be initialized , Because it is by default public static final Embellished
- Methods in the interface , The default is abstract methods , That is to say public abstract Embellished
- Ordinary member methods in interfaces cannot have specific implementations
- If the ordinary member method in the interface needs to have a specific implementation , Have to add default modification
- Interfaces can have static member methods ,default Methods are all default public Embellished
- Interfaces are also not instantiatable
- When a class implements an interface , All abstract methods within the interface must be implemented
- The relationship between classes and interfaces is implementation (implements)
2.5 Implement multiple interfaces
stay java Multiple inheritance is not supported in , But a class can implement multiple interfaces
abstract class Animal{
public String name;
public int age;
public Animal(String name,int age){
this.name = name;
this.age = age;
}
public abstract void eat();
}
interface IRunning{
void iRun();
}
interface IFlying{
void iFly();
}
interface ISwimming{
void ISwim();
}
// Can run , Can swim
class Dog extends Animal implements IRunning,ISwimming{
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name+" Dog food ");
}
@Override
public void iRun() {
System.out.println(name+" Running on four legs ");
}
@Override
public void ISwim() {
System.out.println(name+" I'm planing with my four legs ");
}
}
// Can run , Can fly
class Bird extends Animal implements IFlying,IRunning{
public Bird(String name,int age){
super(name,age);
}
@Override
public void eat() {
System.out.println(name + " Eat bird food ");
}
@Override
public void iFly() {
System.out.println(name + " Flying with wings ");
}
@Override
public void iRun() {
System.out.println(name+ " Running on two legs ");
}
}
class Duck extends Animal implements IRunning,IFlying,ISwimming{
public Duck(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name + " Eating duck food ");
}
@Override
public void iRun() {
System.out.println(name + " Running with big feet ");
}
@Override
public void iFly() {
System.out.println(name + " Flying with wings ");
}
@Override
public void ISwim() {
System.out.println(name + " Swimming with big feet ");
}
}
2.6 Inheritance between interfaces
interface A{
void funcA();
}
interface B{
void funcB();
}
// Extend the functionality
interface C extends A,B {
void funC();
}
class T implements C{
@Override
public void funcA() {
}
@Override
public void funcB() {
}
@Override
public void funC() {
}
}
2.7 The difference between interface and abstract class
| Interface | abstract class | |
|---|---|---|
| keyword | interface | abstract |
| Member variables | By default, the member variables in the interface are pubic static final modification | The member variables of abstract classes are the same as those of ordinary classes |
| Method | No construction method , Common methods must be implemented by default modification , Static methods can be concretely implemented | There's a way to structure , There are common ways to implement , There are static methods that can be implemented |
| Subclass Use | implements | extends |
| Relationship | Interfaces cannot inherit abstract classes , But the interface can be used extends Keyword inherits multiple parent interfaces | An abstract class can implement multiple interfaces |
| Whether you can inherit more | A class can implement multiple interfaces | A class can only inherit one abstract class |
2.8Cloneable Interface and deep copy
Code + Memory analysis chart
//cloneable And deep copy
class Money implements Cloneable{
public double money = 99.99;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{
public int age = 10;
public Money m = new Money();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test{
public static void main(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Money tmp = new Money();
tmp = (Money)person1.m.clone();
Person person2 = (Person)person1.clone();
person2.m = tmp;
System.out.println(person1.m.money);
System.out.println(person2.m.money);
person2.m.money = 9.9;
System.out.println("+======================+");
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
public static void main2(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person)person1.clone();
System.out.println(person1.m.money);
System.out.println(person2.m.money);
person2.m.money = 9.9;
System.out.println("+====================+");
System.out.println(person1.m.money);
System.out.println(person2.m.money);
}
public static void main1(String[] args) throws CloneNotSupportedException {
Person person1 = new Person();
Person person2 = (Person)person1.clone();
System.out.println(person1.age);
System.out.println(person2.age);
person2.age = 99;
System.out.println("+====================+");
System.out.println(person1.age);
System.out.println(person2.age);
}
}



边栏推荐
- 异常:IOException:Stream Closed
- 使用rtknavi进行RT-PPP测试
- 记录:下一不小心写了个递归
- [算法] 剑指offer2 golang 面试题1:整数除法
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- [算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
- Employment of cashier [differential constraint]
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- [rtklib 2.4.3 B34] version update introduction I
- Detailed explanation of balanced binary tree is easy to understand
猜你喜欢

How to ensure data consistency between MySQL and redis?

MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景

Dark chain lock (lca+ difference on tree)
![[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
![[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal](/img/11/ee0628a68542236fc641966579a31a.png)
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
![[算法] 剑指offer2 golang 面试题4:只出现一次的数字](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[算法] 剑指offer2 golang 面试题4:只出现一次的数字

Several high-frequency JVM interview questions
![[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K](/img/8c/1b6ba3b1830ad28176190170c98628.png)
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K

抽象类和接口

3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
随机推荐
What are the advantages of using SQL in Excel VBA
Redis介绍与使用
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
Shortest Hamilton path (pressure DP)
How do architects draw system architecture blueprints?
How to reduce the shutdown time of InnoDB database?
Excel导入,导出功能实现
121道分布式面试题和答案
[Chongqing Guangdong education] Shandong University College Physics reference materials
初识C语言(下)
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
rtklib单点定位spp使用抗差估计遇到的问题及解决
记录:动态Web项目servlet访问数据库404错误之解决
最短Hamilton路径 (状压DP)
121 distributed interview questions and answers
Usage differences between isempty and isblank
Heap sort [handwritten small root heap]
十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
堆排序【手写小根堆】