当前位置:网站首页>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);
}
}
边栏推荐
- Code example of MATLAB reading GNSS observation value o file
- Heap sort [handwritten small root heap]
- Experience summary of autumn recruitment of state-owned enterprises
- TYUT太原理工大学2022软工导论考试题型大纲
- [算法] 劍指offer2 golang 面試題2:二進制加法
- Fundamentals of UD decomposition of KF UD decomposition [1]
- 记录:下一不小心写了个递归
- 记录:动态Web项目servlet访问数据库404错误之解决
- 记录:newInstance()过时的代替方法
- First acquaintance with C language (Part 2)
猜你喜欢
Basic DOS commands
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
What are the advantages of using SQL in Excel VBA
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
The port is occupied because the service is not shut down normally
平衡二叉树详解 通俗易懂
[Chongqing Guangdong education] Shandong University College Physics reference materials
What are the advantages of using SQL in Excel VBA
随机推荐
MySQL shutdown is slow
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
染色法判定二分图
图书管理系统小练习
基本Dos命令
Redis介绍与使用
[算法] 剑指offer2 golang 面试题1:整数除法
Record: Navicat premium can't connect to MySQL for the first time
XV Function definition and call
Answer to "software testing" exercise: Chapter 1
[GNSS] robust estimation (robust estimation) principle and program implementation
Itext 7 生成PDF总结
分支语句和循环语句
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
几道高频的JVM面试题
Knowledge system of digital IT practitioners | software development methods -- agile
Sharing ideas of on-chip transplantation based on rtklib source code