当前位置:网站首页>抽象类和接口
抽象类和接口
2022-07-06 09:19:00 【犇犇犇犇犇犇】
抽象类和接口
目录:
文章目录
1.抽象类
1.1抽象类的概念
抽象类往往用来表征对问题领域进行分析、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。
通常在编程语句中用 abstract 修饰的类是抽象类。在C++中,含有纯虚拟函数的类称为抽象类,它不能生成对象;在java中,含有抽象方法的类称为抽象类,同样不能生成对象。
抽象类是不完整的,它只能用作基类。在面向对象方法中,抽象类主要用来进行类型隐藏和充当全局变量的角色。
1.2抽象类的语法
abstract class A
1.3抽象类特性
- 使用abstract来定义的方法叫做抽象方法
- 有抽象方法的类必须是抽象类,但是抽象类不一定有抽象方法
- 抽象类和普通类一样可以拥有成员变量、成员方法、构造方法、代码块
- 我们的抽象方法不能是private,static,final修饰的
- 抽象类不能实例化对象,可以被继承【也就是说抽象类就是用来被继承的】
- 普通类继承了抽象类,如果抽象类中有抽象方法,那么必须重写该抽象类中所有的抽象方法
- 当抽象类A,继承了抽象类B可以不用重写抽象类B的抽象方法
- 抽象类相当于多了一层编译器效验
1.4抽象类的作用
抽象类就是用来被继承的
在实际工作中,实际操作不是在父类中完成的,当我们用普通类进行编译时如果错用了父类,编译器是不会报错的,但是如果是抽象类在实例化的时候就会提醒我们错误。
2. 接口
2.1接口的概念
接口就是公共的行为规范标准,大家在实现时,只要符合规范标准,就可以通用。在Java中,接口可以看成是:多个类的公共规范,是一种引用数据类型。
2.2语法
- 创建接口时,接口的命名一般以大写I开头
- 接口的命名一般用形容词性的单词
- 类和接口之间的关系是实现(implements)
interface IRunning
class A implements IRunning
2.3接口使用
接口不能直接使用,必须有一个类,来实现接口中所有的抽象方法
2.4接口特性
- 使用interface来表示接口
- 在接口当中的成员变量必须初始化,因为它是默认被public static final修饰的
- 接口当中的方法,默认都是抽象方法,也就是public abstract修饰的
- 接口当中的普通成员方法是不能有具体实现的
- 接口当中的普通成员方法如果要有具体实现,必须加上default修饰
- 接口当中可以有静态的成员方法,default方法都是默认public修饰的
- 接口也是不可以实例化的
- 当一个类实现接口,必须实现接口内的所有抽象方法
- 类和接口之间的关系是实现(implements)
2.5实现多个接口
在java中不支持多继承,但是一个类可以实现多个接口
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();
}
//能跑,能游泳
class Dog extends Animal implements IRunning,ISwimming{
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name+" 吃狗粮");
}
@Override
public void iRun() {
System.out.println(name+"正在用四条腿跑");
}
@Override
public void ISwim() {
System.out.println(name+"正在用四条腿在狗刨");
}
}
//能跑,能飞
class Bird extends Animal implements IFlying,IRunning{
public Bird(String name,int age){
super(name,age);
}
@Override
public void eat() {
System.out.println(name + "吃鸟粮");
}
@Override
public void iFly() {
System.out.println(name + "正在用翅膀飞");
}
@Override
public void iRun() {
System.out.println(name+ "正在用两条腿跑");
}
}
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 + "正在吃鸭粮");
}
@Override
public void iRun() {
System.out.println(name + "正在用大脚掌跑");
}
@Override
public void iFly() {
System.out.println(name + "正在用翅膀飞");
}
@Override
public void ISwim() {
System.out.println(name + "正在用大脚掌游泳");
}
}
2.6接口之间的继承
interface A{
void funcA();
}
interface B{
void funcB();
}
//扩展功能
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接口和抽象类的区别
接口 | 抽象类 | |
---|---|---|
关键字 | interface | abstract |
成员变量 | 接口当中的成员变量默认被pubic static final修饰 | 抽象类的成员变量和普通类一样 |
方法 | 无构造方法,普通方法要想具体实现必须被default修饰,静态方法可以具体实现 | 有构造方法,有普通方法可以具体实现,有静态方法可以具体实现 |
子类 的使用 | implements | extends |
关系 | 接口不能继承抽象类,但是接口可以用extends关键字继承多个父接口 | 一个抽象类可以实现多个接口 |
是否可以多继承 | 一个类可以实现多个接口 | 一个类只能继承一个抽象类 |
2.8Cloneable接口和深拷贝
代码+内存分析图
//cloneable和深拷贝
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 面试题12:左右两边子数组的和相等
Fundamentals of UD decomposition of KF UD decomposition [1]
Several high-frequency JVM interview questions
Record: the solution of MySQL denial of access when CMD starts for the first time
闇の連鎖(LCA+树上差分)
Detailed explanation of balanced binary tree is easy to understand
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
10 minutes pour maîtriser complètement la rupture du cache, la pénétration du cache, l'avalanche du cache
FairyGUI增益BUFF數值改變的顯示
Novatel board oem617d configuration step record
随机推荐
FairyGUI循环列表
How to reduce the shutdown time of InnoDB database?
记录:初次cmd启动MySQL拒接访问之解决
The port is occupied because the service is not shut down normally
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
rtklib单点定位spp使用抗差估计遇到的问题及解决
What are the functions and features of helm or terrain
[algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
[GNSS] robust estimation (robust estimation) principle and program implementation
记录:Navicat Premium初次无法连接数据库MySQL之解决
Role movement in the first person perspective
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Exception: ioexception:stream closed
Record: the solution of MySQL denial of access when CMD starts for the first time
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
Detailed explanation of balanced binary tree is easy to understand
平衡二叉树详解 通俗易懂
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
IText 7 generate PDF summary
[算法] 劍指offer2 golang 面試題2:二進制加法