当前位置:网站首页>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);
}
}
边栏推荐
- [算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
- Usage differences between isempty and isblank
- Rt-ppp test using rtknavi
- 2年经验总结,告诉你如何做好项目管理
- 异常:IOException:Stream Closed
- 记录:newInstance()过时的代替方法
- 分支语句和循环语句
- [rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
- Shortest Hamilton path (pressure DP)
- 10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
猜你喜欢
How do architects draw system architecture blueprints?
Music playback (toggle & playerprefs)
[算法] 剑指offer2 golang 面试题1:整数除法
Interview Essentials: talk about the various implementations of distributed locks!
What are the advantages of using SQL in Excel VBA
基本Dos命令
[算法] 劍指offer2 golang 面試題2:二進制加法
如何保障 MySQL 和 Redis 的数据一致性?
【无标题】
[GNSS data processing] Helmert variance component estimation analysis and code implementation
随机推荐
All in one 1405: sum and product of prime numbers
Implementation of Excel import and export functions
Error: sorting and subscript out of bounds
【无标题】
【GNSS】抗差估计(稳健估计)原理及程序实现
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
The earth revolves around the sun
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
162. Find peak - binary search
WSL common commands
Record: I accidentally wrote a recursion next time
Heap sort [handwritten small root heap]
[算法] 劍指offer2 golang 面試題2:二進制加法
Fgui project packaging and Publishing & importing unity & the way to display the UI
MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列
Problems and solutions of robust estimation in rtklib single point location spp
Rt-ppp test using rtknavi
One article to get UDP and TCP high-frequency interview questions!
染色法判定二分图
How to reduce the shutdown time of InnoDB database?