当前位置:网站首页>Abstract classes and interfaces
Abstract classes and interfaces
2022-07-22 06:35:00 【Xiao Wang is not confused】
List of articles
Preface
This article mainly records the understanding of abstract classes and interfaces .
One 、 What is an abstract class ?
1. Use abstract The method of modification is called abstract method , A class that contains abstract methods is called an abstract class ;
2. Classes that inherit abstract classes need to override all abstract methods in abstract classes , Otherwise, the compiler will report an error ;
3. Abstract classes inherit abstract classes and can temporarily not override abstract methods ;
4. The access rights of abstract classes cannot be used private And can't use static、final modification .
1. Example
The code is as follows ( Example ):
// abstract class
abstract class USB {
public abstract void use();
}
class Mouse extends USB {
// Classes that inherit abstract classes need to override all abstract methods in abstract classes
@Override
public void use() {
System.out.println("Mouse");
}
}
Two 、 Interface (interface)
1、 The member variables in the interface are... By default public static final modification ;
2、 The default method in an interface is an abstract method , use public abstract modification ;
3、 Ordinary member methods in interfaces cannot have specific implementation unless default;
4、 Interface cannot be instantiated , There cannot be static in the interface 、 Instance code block 、 Construction method ;
5、 The association between classes and interfaces is through implement Implemented .
1. example
The code is as follows ( Example ):
// Interface
interface Shape {
public static final int count = 3;// The member variable in the interface defaults to public static final modification
void draw();// The default method in an interface is an abstract method , Use public abstract modification
}
// For classes and interfaces implement Implement Association
class Rect implements Shape {
// The class that implements the interface needs to rewrite all abstract methods in the interface
@Override
public void draw() {
System.out.println("□");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("○");
}
}
class Flower implements Shape {
@Override
public void draw() {
System.out.println("*");
}
}
public class TestDemo2 {
public static void drawShape(Shape shape){
shape.draw();
}
public static void main(String[] args) {
drawShape(new Rect());
drawShape(new Circle());
drawShape(new Flower());
}
}
2. Implement multiple interfaces
A class inherits first and then implements , Only one class can be inherited to implement multiple interfaces .
The code is as follows ( Example ):
class Animal {
String name;// Member variables
int age;
// Construction method
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
}
// Interface
interface IEating {
void eat();
}
interface IFlying {
void fly();
}
interface IRunning {
void run();
}
interface ISwimming {
void swim();
}
// A class can only inherit one class , But you can implement multiple interfaces
class Dog extends Animal implements IEating,IRunning,ISwimming {
public Dog(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name+" "+age+" year , Is eating ~");
}
@Override
public void run() {
System.out.println(name+" "+age+" year , Running ~");
}
@Override
public void swim() {
System.out.println(name+" "+age+" year , I'm swimming ~");
}
}
class Bird extends Animal implements IEating,IFlying,IRunning {
public Bird(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(name+" "+age+" year , Is eating ~");
}
@Override
public void fly() {
System.out.println(name+" "+age+" year , Flying ~");
}
@Override
public void run() {
System.out.println(name+" "+age+" year , Running ~");
}
}
public class TestDemo3 {
public static void eat(IEating iEating) {
iEating.eat();
}
public static void fly(IFlying iFlying) {
iFlying.fly();
}
public static void run(IRunning iRunning) {
iRunning.run();
}
public static void swim(ISwimming iSwimming) {
iSwimming.swim();
}
public static void main(String[] args) {
eat(new Dog(" The small white ",1));
run(new Dog(" The small white ",1));
swim(new Dog(" The small white ",1));
System.out.println("===================");
eat(new Bird(" Little bird ",2));
fly(new Bird(" Little bird ",2));
run(new Bird(" Little bird ",2));
}
}
3、 ... and 、 How custom types compare sizes ?
Basic data types can directly compare their sizes , But custom types , for example : Student , How to compare the size of this class of students , Here are two methods :
1.Comparable
The code is as follows ( Example ):
//Comparable Interface
class Student implements Comparable<Student> {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
// rewrite compareTo Method
@Override
public int compareTo(Student o) {
return this.age-o.age;
}
}
public class TestDemo4 {
public static void main(String[] args) {
Student[] arr = {
new Student(" Li San ",20),new Student(" Zhang Si ",19)};
System.out.println(" Before ordering "+Arrays.toString(arr));
Arrays.sort(arr);
System.out.println(" After ordering "+Arrays.toString(arr));
}
}
2.Comparator
The code is as follows ( Example ):
// The second method : The comparator Comparator Interface
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class AgeComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.age - o2.age;
}
}
class NameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o2.name.compareTo(o1.name);
}
}
public class TestDemo4 {
// Simulated sort Sort
public static void sort(Comparable[] array) {
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-i-1; j++) {
if(array[j].compareTo(array[j+1])>0) {
Comparable tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
}
public static void main(String[] args) {
Student[] students = {
new Student(" Li San ",20),new Student(" Zhang Si ",19),new Student(" A Wei ",23)};
System.out.println(" Before ordering "+Arrays.toString(students));
Arrays.sort(students,new AgeComparator());
System.out.println(" After ordering "+Arrays.toString(students));
Arrays.sort(students,new NameComparator());
System.out.println(" After ordering "+Arrays.toString(students));
}
Four 、Clone Realize deep copy and shallow copy
1. Shallow copy
Changing one will affect the other
The code is as follows ( Example ):
// Shallow copy : Changing one will affect the other
class Money {
public double money = 11.1;
}
class Person implements Cloneable{
public int age;
public Money money = new Money();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class TestDemo5 {
public static void main(String[] args) throws CloneNotSupportedException{
Person person = new Person();
Person person1 = (Person) person.clone();// Shallow copy
System.out.println("person:"+person.money.money);
System.out.println("person:"+person1.money.money);
person.money.money = 12.12;
System.out.println("person:"+person.money.money);
System.out.println("person:"+person1.money.money);
}
}
2. Deep copy
Changing one will not affect the other
The code is as follows ( Example ):
class Money implements Cloneable{
public double money = 11.1;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{
public int age;
public Money money = new Money();
// Implement deep copy
@Override
protected Object clone() throws CloneNotSupportedException {
Person tmp = (Person) super.clone();
tmp.money = (Money) this.money.clone();
return tmp;
}
}
5、 ... and 、 abstract class 、 Differences and connections of interfaces
1. difference :
(1) There are ordinary classes and abstract methods in abstract classes , There are only abstract methods and public static final Modified global constant ;
(2) Use between abstract classes extends Keyword inheritance , Interface and class use implements Keyword implementation ;
(3) A class can only inherit one abstract class , But a class can implement multiple interfaces ;
(4) An abstract class can implement several interfaces , But interfaces can't inherit abstract classes , Only multiple parent class interfaces can be inherited .
2. contact :
Neither abstract classes nor interfaces can be instantiated .
边栏推荐
猜你喜欢
同城订单同城送,爆单依旧得心应手!
Redis: master-slave replication
Dynamics CRM 365 OP版的经典模式,选项卡是滚动查看的,是否可以调成和UCI界面一样,通过Tab展示呢
leetcode 931. Minimum sum of descent path
LeetCode刷题:对称二叉树与二叉树的最大深度
Leetcode brush question: circular linked list and circular linked list II
IDEA 搭建和环境变量
LeetCode刷题:链表中倒数第k个节点
抽象类与接口
Episode 1 VMware Virtual Machine installation Best B tutoriel (12 jours)
随机推荐
一键免费下载外文文献的方式
西农大 C plus
unity GetAxis(string axisName); axisName 值怎么来的
Document flow, box model, outer margin and default style
同城订单同城送,爆单依旧得心应手!
Building and using the embedznet Pro ZigBee host application in the cygwin environment
Style of table form
Xshell远程连接服务器
DOM event proxy (2)
Length unit, color unit, text style and font style
leetcode1588. Sum of all odd length subarrays
OSPF实验演示(Huawei路由器设备配置)
Idea setup and environment variables
leetcode 225. 用队列实现栈(简单)
Detailed explanation of oracle:pl/sql basic syntax (selection structure, loop structure and cursor)
β- Preparation of cyclodextrin derivative grafted hydroxypropyl chitosan hydrogel / carboxyl modified chitosan supported cyclodextrin hydrogel microspheres
安装与卸载MySQL的详细步骤
Leetcode skimming: symmetric binary tree and maximum depth of binary tree
unity 3D 修改默认编辑器
Leetcode skimming: balanced binary tree and flipped binary tree